Skip to content

Commit 6491313

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

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import subprocess
1414
import sys
1515
from enum import Enum, unique
16-
from typing import Dict, Final, List, NamedTuple, Optional, Tuple, Union
16+
from typing import cast, Dict, Final, List, NamedTuple, Optional, Tuple, Union
1717
from typing_extensions import TypeAlias as _TypeAlias
1818

1919
from mypy import pyinfo
@@ -334,6 +334,19 @@ 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+
if self.options:
338+
enable_installed_packages = self.options.enable_installed_packages
339+
try:
340+
enable_installed_packages = cast(
341+
bool,
342+
self.options.per_module_options[components[0]][
343+
"enable_installed_packages"
344+
],
345+
)
346+
except KeyError:
347+
pass
348+
if enable_installed_packages:
349+
return os.path.join(pkg_dir, *components[:-1]), False
337350
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
338351
else:
339352
return ModuleNotFoundReason.NOT_FOUND

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)