Skip to content
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

Implement flag to allow typechecking of untyped modules #17712

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ def add_invertible_flag(
action="store_true",
help="Silently ignore imports of missing modules",
)
imports_group.add_argument(
"--follow-untyped-imports",
action="store_true",
help="Typecheck modules without stubs or py.typed marker",
)
imports_group.add_argument(
"--follow-imports",
choices=["normal", "silent", "skip", "error"],
Expand Down
9 changes: 7 additions & 2 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,11 @@ def _typeshed_has_version(self, module: str) -> bool:
return version >= min_version and (max_version is None or version <= max_version)

def _find_module_non_stub_helper(
self, components: list[str], pkg_dir: str
self, id: str, pkg_dir: str
) -> OnePackageDir | ModuleNotFoundReason:
plausible_match = False
dir_path = pkg_dir
components = id.split(".")
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")):
Expand All @@ -350,6 +351,10 @@ def _find_module_non_stub_helper(
if not self.fscache.isdir(dir_path):
break
if plausible_match:
if self.options:
module_specific_options = self.options.clone_for_module(id)
if module_specific_options.follow_untyped_imports:
return os.path.join(pkg_dir, *components[:-1]), False
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
else:
return ModuleNotFoundReason.NOT_FOUND
Expand Down Expand Up @@ -463,7 +468,7 @@ def _find_module(self, id: str, use_typeshed: bool) -> ModuleSearchResult:
third_party_stubs_dirs.append((path, True))
else:
third_party_stubs_dirs.append((path, True))
non_stub_match = self._find_module_non_stub_helper(components, pkg_dir)
non_stub_match = self._find_module_non_stub_helper(id, pkg_dir)
if isinstance(non_stub_match, ModuleNotFoundReason):
if non_stub_match is ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS:
found_possible_third_party_missing_type_hints = True
Expand Down
3 changes: 3 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class BuildType:
"extra_checks",
"follow_imports_for_stubs",
"follow_imports",
"follow_untyped_imports",
"ignore_errors",
"ignore_missing_imports",
"implicit_optional",
Expand Down Expand Up @@ -113,6 +114,8 @@ def __init__(self) -> None:
self.ignore_missing_imports = False
# Is ignore_missing_imports set in a per-module section
self.ignore_missing_imports_per_module = False
# Typecheck modules without stubs or py.typed marker
self.follow_untyped_imports = False
self.follow_imports = "normal" # normal|silent|skip|error
# Whether to respect the follow_imports setting even for stub files.
# Intended to be used for disabling specific stubs.
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/pep561.test
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ b.bf(1)
testNamespacePkgWStubsWithNamespacePackagesFlag.py:7: error: Argument 1 to "bf" has incompatible type "int"; expected "bool"
testNamespacePkgWStubsWithNamespacePackagesFlag.py:8: error: Argument 1 to "bf" has incompatible type "int"; expected "bool"

[case testMissingPytypedFlag]
# pkgs: typedpkg_ns_b
# flags: --namespace-packages --follow-untyped-imports
import typedpkg_ns.b.bbb as b
b.bf("foo", "bar")
[out]
testMissingPytypedFlag.py:4: error: Too many arguments for "bf"

[case testTypedPkgNamespaceRegFromImportTwiceMissing]
# pkgs: typedpkg_ns_a
Expand Down
Loading