From 288935e89afc7d7fdf8fb3379bcd06f792cda600 Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:02:09 -0400 Subject: [PATCH 1/3] Enable mypy checking for astroid/interpreter/_import/ This commit also removes unnecessary tuple -> list conversions in _find_spec. --- astroid/interpreter/_import/spec.py | 27 +++++++++++++-------------- pyproject.toml | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py index 0e6ddec927..de965e80ec 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -18,7 +18,7 @@ from collections.abc import Iterable, Iterator, Sequence from functools import lru_cache from pathlib import Path -from typing import Any, Literal, NamedTuple, Protocol +from typing import Literal, NamedTuple, Protocol from astroid.const import PY310_PLUS from astroid.modutils import EXT_LIB_DIRS, cached_os_path_isfile @@ -91,7 +91,7 @@ def __init__(self, path: Sequence[str] | None = None) -> None: def find_module( modname: str, module_parts: tuple[str], - processed: tuple[str], + processed: tuple[str, ...], submodule_path: Sequence[str] | None, ) -> ModuleSpec | None: """Find the given module. @@ -130,7 +130,7 @@ class ImportlibFinder(Finder): def find_module( modname: str, module_parts: tuple[str], - processed: tuple[str], + processed: tuple[str, ...], submodule_path: Sequence[str] | None, ) -> ModuleSpec | None: if submodule_path is not None: @@ -225,7 +225,7 @@ class ExplicitNamespacePackageFinder(ImportlibFinder): def find_module( modname: str, module_parts: tuple[str], - processed: tuple[str], + processed: tuple[str, ...], submodule_path: Sequence[str] | None, ) -> ModuleSpec | None: if processed: @@ -265,7 +265,7 @@ def __init__(self, path: Sequence[str]) -> None: def find_module( modname: str, module_parts: tuple[str], - processed: tuple[str], + processed: tuple[str, ...], submodule_path: Sequence[str] | None, ) -> ModuleSpec | None: try: @@ -289,7 +289,7 @@ class PathSpecFinder(Finder): def find_module( modname: str, module_parts: tuple[str], - processed: tuple[str], + processed: tuple[str, ...], submodule_path: Sequence[str] | None, ) -> ModuleSpec | None: spec = importlib.machinery.PathFinder.find_spec(modname, path=submodule_path) @@ -346,7 +346,7 @@ def _search_zip( ) -> tuple[Literal[ModuleType.PY_ZIPMODULE], str, str]: for filepath, importer in _get_zipimporters(): if PY310_PLUS: - found: Any = importer.find_spec(modpath[0]) + found = importer.find_spec(modpath[0]) else: found = importer.find_module(modpath[0]) if found: @@ -373,15 +373,15 @@ def _find_spec_with_path( search_path: Sequence[str], modname: str, module_parts: tuple[str], - processed: tuple[str], + processed: tuple[str, ...], submodule_path: Sequence[str] | None, ) -> tuple[Finder | _MetaPathFinder, ModuleSpec]: for finder in _SPEC_FINDERS: finder_instance = finder(search_path) - spec = finder.find_module(modname, module_parts, processed, submodule_path) - if spec is None: + mod_spec = finder.find_module(modname, module_parts, processed, submodule_path) + if mod_spec is None: continue - return finder_instance, spec + return finder_instance, mod_spec # Support for custom finders for meta_finder in sys.meta_path: @@ -444,20 +444,19 @@ def find_spec(modpath: Iterable[str], path: Iterable[str] | None = None) -> Modu @lru_cache(maxsize=1024) -def _find_spec(module_path: tuple, path: tuple) -> ModuleSpec: +def _find_spec(module_path: tuple[str], path: tuple[str, ...]) -> ModuleSpec: _path = path or sys.path # Need a copy for not mutating the argument. modpath = list(module_path) submodule_path = None - module_parts = tuple(modpath) processed: list[str] = [] while modpath: modname = modpath.pop(0) finder, spec = _find_spec_with_path( - _path, modname, module_parts, tuple(processed), submodule_path or path + _path, modname, module_path, tuple(processed), submodule_path or path ) processed.append(modname) if modpath: diff --git a/pyproject.toml b/pyproject.toml index b0078e813e..55c7ca9412 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,11 +62,23 @@ testpaths = ["tests"] filterwarnings = "error" [tool.mypy] -enable_error_code = "ignore-without-code" -no_implicit_optional = true +python_version = "3.9" +files = [ + "astroid/interpreter/_import/", +] +always_false = [ + "PY310_PLUS", + "PY311_PLUS", + "PY312_PLUS", + "PY313_PLUS", +] +disallow_any_decorated = true +disallow_any_explicit = true scripts_are_modules = true show_error_codes = true +strict = true warn_redundant_casts = true +warn_unreachable = true [[tool.mypy.overrides]] # Importlib typeshed stubs do not include the private functions we use From 0f88d1e079bd33995c9907e1c4d6060e209ae414 Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:19:53 -0400 Subject: [PATCH 2/3] Remove blanket mypy exclusion from pre-commit config --- .pre-commit-config.yaml | 4 ++-- pyproject.toml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7764c44a0d..d8c49754d8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -62,10 +62,10 @@ repos: entry: mypy language: python types: [python] - args: [] + args: ["--config-file=pyproject.toml"] require_serial: true additional_dependencies: ["types-typed-ast"] - exclude: tests/testdata| # exclude everything, we're not ready + exclude: tests/testdata - repo: https://github.com/pre-commit/mirrors-prettier rev: v4.0.0-alpha.8 hooks: diff --git a/pyproject.toml b/pyproject.toml index 55c7ca9412..b3243a36b3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -74,6 +74,7 @@ always_false = [ ] disallow_any_decorated = true disallow_any_explicit = true +follow_imports = "silent" scripts_are_modules = true show_error_codes = true strict = true From 4f59d99b9c76b5fc2dc99e9cfe0a2133f20da958 Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:14:16 -0400 Subject: [PATCH 3/3] Try to get pre-commit CI checks to pass --- .pre-commit-config.yaml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d8c49754d8..3b0d9bb726 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -58,14 +58,10 @@ repos: rev: v1.11.2 hooks: - id: mypy - name: mypy - entry: mypy language: python - types: [python] - args: ["--config-file=pyproject.toml"] + pass_filenames: false require_serial: true additional_dependencies: ["types-typed-ast"] - exclude: tests/testdata - repo: https://github.com/pre-commit/mirrors-prettier rev: v4.0.0-alpha.8 hooks: