Skip to content

Add --pep561-override option to declare package names that are always considered typed #8512

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def split_and_match_files(paths: str) -> List[str]:
'package_root': lambda s: [p.strip() for p in s.split(',')],
'cache_dir': expand_path,
'python_executable': expand_path,
'pep561_override': lambda s: [p.strip() for p in s.split(',')],
} # type: Final


Expand Down
4 changes: 4 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ def add_invertible_flag(flag: str,
imports_group.add_argument(
'--no-silence-site-packages', action='store_true',
help="Do not silence errors in PEP 561 compliant installed packages")
imports_group.add_argument(
'--pep561-override', action='append', default=[],
help="Consider the given package typed even if it does not claim to be "
"PEP 561 compatible (may be repeated)")

platform_group = parser.add_argument_group(
title='Platform configuration',
Expand Down
26 changes: 19 additions & 7 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,31 @@ def find_module(self, id: str) -> ModuleSearchResult:

def _find_module_non_stub_helper(self, components: List[str],
pkg_dir: str) -> Union[OnePackageDir, ModuleNotFoundReason]:
plausible_match = False
plausible_match = None # type: Optional[Tuple[str, OnePackageDir]]
plausible_match_is_ns = False
dir_path = pkg_dir
for index, component in enumerate(components):
dir_path = os.path.join(dir_path, component)
if self.fscache.isfile(os.path.join(dir_path, 'py.typed')):
return os.path.join(pkg_dir, *components[:-1]), index == 0
elif not plausible_match and (self.fscache.isdir(dir_path)
or self.fscache.isfile(dir_path + ".py")):
plausible_match = True
if plausible_match:
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
else:
if not plausible_match or plausible_match_is_ns:
have_package_or_mod = self.fscache.isfile(os.path.join(dir_path, '__init__.py')) \
or self.fscache.isfile(dir_path + ".py")
if not have_package_or_mod:
have_ns_package = self.fscache.isdir(dir_path)

if have_package_or_mod or (not plausible_match_is_ns and have_ns_package):
plausible_match = (
'.'.join(components[:(index + 1)]),
(os.path.join(pkg_dir, *components[:-1]), index == 0)
)
plausible_match_is_ns = not have_package_or_mod
if not plausible_match:
return ModuleNotFoundReason.NOT_FOUND
elif self.options and plausible_match[0] in self.options.pep561_override:
return plausible_match[1]
else:
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS

def _update_ns_ancestors(self, components: List[str], match: Tuple[str, bool]) -> None:
path, verify = match
Expand Down
2 changes: 2 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def __init__(self) -> None:
self.transform_source = None # type: Optional[Callable[[Any], Any]]
# Print full path to each file in the report.
self.show_absolute_path = False # type: bool
# Consider packages typed even if they do not declare PEP-561 compatiblity
self.pep561_override = [] # type: List[str]

# To avoid breaking plugin compatibility, keep providing new_semantic_analyzer
@property
Expand Down