Skip to content

Commit 5763e64

Browse files
committed
Implement flag to allow typechecking of untyped modules
Fixes #8545
1 parent fe15ee6 commit 5763e64

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

mypy/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ def add_invertible_flag(
572572
action="store_true",
573573
help="Silently ignore imports of missing modules",
574574
)
575+
imports_group.add_argument(
576+
"--enable-installed-packages",
577+
action="store_true",
578+
help="Typecheck modules without stubs or py.typed marker",
579+
)
575580
imports_group.add_argument(
576581
"--follow-imports",
577582
choices=["normal", "silent", "skip", "error"],

mypy/modulefinder.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,17 @@ def _find_module_non_stub_helper(
334334
if approved_stub_package_exists(".".join(components[:i])):
335335
return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
336336
if plausible_match:
337-
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
337+
enable_installed_packages = self.options.enable_installed_packages
338+
try:
339+
enable_installed_packages = self.options.per_module_options[components[0]][
340+
"enable_installed_packages"
341+
]
342+
except KeyError:
343+
pass
344+
if enable_installed_packages:
345+
return os.path.join(pkg_dir, *components[:-1]), False
346+
else:
347+
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
338348
else:
339349
return ModuleNotFoundReason.NOT_FOUND
340350

mypy/options.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class BuildType:
3939
"disallow_untyped_defs",
4040
"enable_error_code",
4141
"enabled_error_codes",
42+
"enable_installed_packages",
4243
"extra_checks",
4344
"follow_imports_for_stubs",
4445
"follow_imports",
@@ -113,6 +114,8 @@ def __init__(self) -> None:
113114
self.ignore_missing_imports = False
114115
# Is ignore_missing_imports set in a per-module section
115116
self.ignore_missing_imports_per_module = False
117+
# Typecheck modules without stubs or py.typed marker
118+
self.enable_installed_packages = False
116119
self.follow_imports = "normal" # normal|silent|skip|error
117120
# Whether to respect the follow_imports setting even for stub files.
118121
# Intended to be used for disabling specific stubs.

test-data/unit/pep561.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ b.bf(1)
187187
testNamespacePkgWStubsWithNamespacePackagesFlag.py:7: error: Argument 1 to "bf" has incompatible type "int"; expected "bool"
188188
testNamespacePkgWStubsWithNamespacePackagesFlag.py:8: error: Argument 1 to "bf" has incompatible type "int"; expected "bool"
189189

190+
[case testMissingPytypedFlag]
191+
# pkgs: typedpkg_ns_b
192+
# flags: --namespace-packages --enable-installed-packages
193+
import typedpkg_ns.b.bbb as b
194+
b.bf("foo", "bar")
195+
[out]
196+
testMissingPytypedFlag.py:4: error: Too many arguments for "bf"
190197

191198
[case testTypedPkgNamespaceRegFromImportTwiceMissing]
192199
# pkgs: typedpkg_ns_a

0 commit comments

Comments
 (0)