From 64913137ae47cfb7c5a3fcf2e840f32c7e7db09e Mon Sep 17 00:00:00 2001 From: Jannick Kremer Date: Tue, 27 Aug 2024 13:46:55 +0200 Subject: [PATCH 1/2] Implement flag to allow typechecking of untyped modules Fixes #8545 --- mypy/main.py | 5 +++++ mypy/modulefinder.py | 15 ++++++++++++++- mypy/options.py | 3 +++ test-data/unit/pep561.test | 7 +++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/mypy/main.py b/mypy/main.py index f177bb1c2062..79ac530dcd94 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -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"], diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index 452cfef20f4c..4d37f26f00bc 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -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 @@ -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 diff --git a/mypy/options.py b/mypy/options.py index 5e64d5e40035..0a1676ccc7ae 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -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", @@ -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. diff --git a/test-data/unit/pep561.test b/test-data/unit/pep561.test index 9969c2894c36..acde01d0e891 100644 --- a/test-data/unit/pep561.test +++ b/test-data/unit/pep561.test @@ -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 From 05466a3269caf02d23102228d6f70aed217537de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:14:33 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/modulefinder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index 4d37f26f00bc..f3ed955dcc5a 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -13,7 +13,7 @@ import subprocess import sys from enum import Enum, unique -from typing import cast, Dict, Final, List, NamedTuple, Optional, Tuple, Union +from typing import Dict, Final, List, NamedTuple, Optional, Tuple, Union, cast from typing_extensions import TypeAlias as _TypeAlias from mypy import pyinfo