Skip to content

Allow user to specify custom typeshed directory #1588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ flag (or its long form ``--help``)::
[--strict-optional]
[--strict-optional-whitelist [GLOB [GLOB ...]]] [--pdb]
[--show-traceback] [--stats] [--inferstats]
[--custom-typing MODULE] [--scripts-are-modules]
[--config-file CONFIG_FILE] [--show-column-numbers]
[--html-report DIR] [--linecount-report DIR]
[--linecoverage-report DIR] [--memory-xml-report DIR]
[--old-html-report DIR] [--txt-report DIR] [--xml-report DIR]
[--xslt-html-report DIR] [--xslt-txt-report DIR]
[-m MODULE] [-c PROGRAM_TEXT] [-p PACKAGE]
[--custom-typing MODULE] [--custom-typeshed-dir DIR]
[--scripts-are-modules] [--config-file CONFIG_FILE]
[--show-column-numbers] [--html-report DIR]
[--linecount-report DIR] [--linecoverage-report DIR]
[--memory-xml-report DIR] [--old-html-report DIR]
[--txt-report DIR] [--xml-report DIR] [--xslt-html-report DIR]
[--xslt-txt-report DIR] [-m MODULE] [-c PROGRAM_TEXT] [-p PACKAGE]
[files [files ...]]

(etc., too long to show everything here)
Expand Down Expand Up @@ -326,6 +326,12 @@ Here are some more useful flags:
package, the behavior enabled by this flag is often more
convenient.)

- ``--custom-typeshed-dir DIR`` specifies the directory where mypy looks for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update config_file.rst?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

typeshed stubs, instead of the typeshed that ships with mypy. This is
primarily intended to make it easier to test typeshed changes before
submitting them upstream, but also allows you to use a forked version of
typeshed.

.. _config-file-flag:

- ``--config-file CONFIG_FILE`` causes configuration settings to be
Expand Down
4 changes: 4 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ The following global flags may only be set in the global section
alternative module which is to be considered equivalent to the
``typing`` module.

- ``custom_typeshed_dir`` (string) specifies the name of an
alternative directory which is used to look for stubs instead of the
default ``typeshed`` directory.

- ``warn_incomplete_stub`` (Boolean, default False) warns for missing
type annotation in typeshed. This is only relevant in combination
with ``check_untyped_defs``.
Expand Down
21 changes: 14 additions & 7 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def build(sources: List[BuildSource],
find_module_clear_caches()

# Determine the default module search path.
lib_path = default_lib_path(data_dir, options.python_version)
lib_path = default_lib_path(data_dir,
options.python_version,
custom_typeshed_dir=options.custom_typeshed_dir)

if options.use_builtins_fixtures:
# Use stub builtins (to speed up test cases and to make them easier to
Expand Down Expand Up @@ -252,15 +254,20 @@ def mypy_path() -> List[str]:
return path_env.split(os.pathsep)


def default_lib_path(data_dir: str, pyversion: Tuple[int, int]) -> List[str]:
def default_lib_path(data_dir: str,
pyversion: Tuple[int, int],
custom_typeshed_dir: Optional[str]) -> List[str]:
"""Return default standard library search paths."""
# IDEA: Make this more portable.
path = [] # type: List[str]

auto = os.path.join(data_dir, 'stubs-auto')
if os.path.isdir(auto):
data_dir = auto

if custom_typeshed_dir:
typeshed_dir = custom_typeshed_dir
else:
auto = os.path.join(data_dir, 'stubs-auto')
if os.path.isdir(auto):
data_dir = auto
typeshed_dir = os.path.join(data_dir, "typeshed")
# We allow a module for e.g. version 3.5 to be in 3.4/. The assumption
# is that a module added with 3.4 will still be present in Python 3.5.
versions = ["%d.%d" % (pyversion[0], minor)
Expand All @@ -269,7 +276,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int]) -> List[str]:
# (Note that 3.1 and 3.0 aren't really supported, but we don't care.)
for v in versions + [str(pyversion[0]), '2and3']:
for lib_type in ['stdlib', 'third_party']:
stubdir = os.path.join(data_dir, 'typeshed', lib_type, v)
stubdir = os.path.join(typeshed_dir, lib_type, v)
if os.path.isdir(stubdir):
path.append(stubdir)

Expand Down
3 changes: 3 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ def process_options(args: List[str],
help="dump type inference stats")
parser.add_argument('--custom-typing', metavar='MODULE', dest='custom_typing_module',
help="use a custom typing module")
parser.add_argument('--custom-typeshed-dir', metavar='DIR',
help="use the custom typeshed in DIR")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Jukka had suggested to change this to "custom typeshed package in DIR" ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking through the old comments, we noted that typeshed wasn't really a package -- that's why I left that out here.

parser.add_argument('--scripts-are-modules', action='store_true',
help="Script x becomes module x instead of __main__")
parser.add_argument('--config-file',
Expand Down Expand Up @@ -451,6 +453,7 @@ def get_init_file(dir: str) -> Optional[str]:
'python_version': lambda s: tuple(map(int, s.split('.'))),
'strict_optional_whitelist': lambda s: s.split(),
'custom_typing_module': str,
'custom_typeshed_dir': str,
'junit_xml': str,
}

Expand Down
3 changes: 2 additions & 1 deletion mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def __init__(self) -> None:
self.build_type = BuildType.STANDARD
self.python_version = defaults.PYTHON3_VERSION
self.platform = sys.platform
self.custom_typing_module = None # type: str
self.custom_typing_module = None # type: Optional[str]
self.custom_typeshed_dir = None # type: Optional[str]
self.report_dirs = {} # type: Dict[str, str]
self.silent_imports = False
self.almost_silent = False
Expand Down