Skip to content

Commit

Permalink
Enable mypy checking for astroid/interpreter/_import/ (pylint-dev#2530)
Browse files Browse the repository at this point in the history
This commit also removes unnecessary tuple -> list conversions in
_find_spec.
  • Loading branch information
correctmost authored Aug 30, 2024
1 parent a389ef7 commit 5210e61
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
6 changes: 1 addition & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ repos:
rev: v1.11.2
hooks:
- id: mypy
name: mypy
entry: mypy
language: python
types: [python]
args: []
pass_filenames: false
require_serial: true
additional_dependencies: ["types-typed-ast"]
exclude: tests/testdata| # exclude everything, we're not ready
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
Expand Down
27 changes: 13 additions & 14 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
17 changes: 15 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,24 @@ 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
follow_imports = "silent"
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
Expand Down

0 comments on commit 5210e61

Please sign in to comment.