Skip to content

Commit

Permalink
Implement flag to allow typechecking of untyped modules
Browse files Browse the repository at this point in the history
  • Loading branch information
DeinAlptraum committed Aug 27, 2024
1 parent fe15ee6 commit 65253dc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ def add_invertible_flag(
action="store_true",
help="Silently ignore imports of missing modules",
)
imports_group.add_argument(
"--allow-missing-pytyped",
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
12 changes: 11 additions & 1 deletion mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,17 @@ def _find_module_non_stub_helper(
if approved_stub_package_exists(".".join(components[:i])):
return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
if plausible_match:
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
allow_missing_pytyped = self.options.allow_missing_pytyped
try:
allow_missing_pytyped = self.options.per_module_options[components[0]][
"allow_missing_pytyped"
]
except KeyError:
pass
if allow_missing_pytyped:
return os.path.join(pkg_dir, *components[:-1]), False
else:
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
else:
return ModuleNotFoundReason.NOT_FOUND

Expand Down
3 changes: 3 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BuildType:

PER_MODULE_OPTIONS: Final = {
# Please keep this list sorted
"allow_missing_pytyped",
"allow_redefinition",
"allow_untyped_globals",
"always_false",
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.allow_missing_pytyped = 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 --allow-missing-pytyped
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

0 comments on commit 65253dc

Please sign in to comment.