Skip to content

Commit

Permalink
Implement flag to allow typechecking of untyped modules
Browse files Browse the repository at this point in the history
Fixes #8545
  • Loading branch information
DeinAlptraum committed Aug 27, 2024
1 parent fe15ee6 commit 6491313
Show file tree
Hide file tree
Showing 4 changed files with 29 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(
"--enable-installed-packages",
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
15 changes: 14 additions & 1 deletion mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import subprocess
import sys
from enum import Enum, unique
from typing import Dict, Final, List, NamedTuple, Optional, Tuple, Union
from typing import cast, Dict, Final, List, NamedTuple, Optional, Tuple, Union
from typing_extensions import TypeAlias as _TypeAlias

from mypy import pyinfo
Expand Down Expand Up @@ -334,6 +334,19 @@ def _find_module_non_stub_helper(
if approved_stub_package_exists(".".join(components[:i])):
return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
if plausible_match:
if self.options:
enable_installed_packages = self.options.enable_installed_packages
try:
enable_installed_packages = cast(
bool,
self.options.per_module_options[components[0]][
"enable_installed_packages"
],
)
except KeyError:
pass
if enable_installed_packages:
return os.path.join(pkg_dir, *components[:-1]), False
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 @@ -39,6 +39,7 @@ class BuildType:
"disallow_untyped_defs",
"enable_error_code",
"enabled_error_codes",
"enable_installed_packages",
"extra_checks",
"follow_imports_for_stubs",
"follow_imports",
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.enable_installed_packages = 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 --enable-installed-packages
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 6491313

Please sign in to comment.