Skip to content

Commit 5210e61

Browse files
authored
Enable mypy checking for astroid/interpreter/_import/ (#2530)
This commit also removes unnecessary tuple -> list conversions in _find_spec.
1 parent a389ef7 commit 5210e61

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,10 @@ repos:
5858
rev: v1.11.2
5959
hooks:
6060
- id: mypy
61-
name: mypy
62-
entry: mypy
6361
language: python
64-
types: [python]
65-
args: []
62+
pass_filenames: false
6663
require_serial: true
6764
additional_dependencies: ["types-typed-ast"]
68-
exclude: tests/testdata| # exclude everything, we're not ready
6965
- repo: https://github.com/pre-commit/mirrors-prettier
7066
rev: v4.0.0-alpha.8
7167
hooks:

astroid/interpreter/_import/spec.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from collections.abc import Iterable, Iterator, Sequence
1919
from functools import lru_cache
2020
from pathlib import Path
21-
from typing import Any, Literal, NamedTuple, Protocol
21+
from typing import Literal, NamedTuple, Protocol
2222

2323
from astroid.const import PY310_PLUS
2424
from astroid.modutils import EXT_LIB_DIRS, cached_os_path_isfile
@@ -91,7 +91,7 @@ def __init__(self, path: Sequence[str] | None = None) -> None:
9191
def find_module(
9292
modname: str,
9393
module_parts: tuple[str],
94-
processed: tuple[str],
94+
processed: tuple[str, ...],
9595
submodule_path: Sequence[str] | None,
9696
) -> ModuleSpec | None:
9797
"""Find the given module.
@@ -130,7 +130,7 @@ class ImportlibFinder(Finder):
130130
def find_module(
131131
modname: str,
132132
module_parts: tuple[str],
133-
processed: tuple[str],
133+
processed: tuple[str, ...],
134134
submodule_path: Sequence[str] | None,
135135
) -> ModuleSpec | None:
136136
if submodule_path is not None:
@@ -225,7 +225,7 @@ class ExplicitNamespacePackageFinder(ImportlibFinder):
225225
def find_module(
226226
modname: str,
227227
module_parts: tuple[str],
228-
processed: tuple[str],
228+
processed: tuple[str, ...],
229229
submodule_path: Sequence[str] | None,
230230
) -> ModuleSpec | None:
231231
if processed:
@@ -265,7 +265,7 @@ def __init__(self, path: Sequence[str]) -> None:
265265
def find_module(
266266
modname: str,
267267
module_parts: tuple[str],
268-
processed: tuple[str],
268+
processed: tuple[str, ...],
269269
submodule_path: Sequence[str] | None,
270270
) -> ModuleSpec | None:
271271
try:
@@ -289,7 +289,7 @@ class PathSpecFinder(Finder):
289289
def find_module(
290290
modname: str,
291291
module_parts: tuple[str],
292-
processed: tuple[str],
292+
processed: tuple[str, ...],
293293
submodule_path: Sequence[str] | None,
294294
) -> ModuleSpec | None:
295295
spec = importlib.machinery.PathFinder.find_spec(modname, path=submodule_path)
@@ -346,7 +346,7 @@ def _search_zip(
346346
) -> tuple[Literal[ModuleType.PY_ZIPMODULE], str, str]:
347347
for filepath, importer in _get_zipimporters():
348348
if PY310_PLUS:
349-
found: Any = importer.find_spec(modpath[0])
349+
found = importer.find_spec(modpath[0])
350350
else:
351351
found = importer.find_module(modpath[0])
352352
if found:
@@ -373,15 +373,15 @@ def _find_spec_with_path(
373373
search_path: Sequence[str],
374374
modname: str,
375375
module_parts: tuple[str],
376-
processed: tuple[str],
376+
processed: tuple[str, ...],
377377
submodule_path: Sequence[str] | None,
378378
) -> tuple[Finder | _MetaPathFinder, ModuleSpec]:
379379
for finder in _SPEC_FINDERS:
380380
finder_instance = finder(search_path)
381-
spec = finder.find_module(modname, module_parts, processed, submodule_path)
382-
if spec is None:
381+
mod_spec = finder.find_module(modname, module_parts, processed, submodule_path)
382+
if mod_spec is None:
383383
continue
384-
return finder_instance, spec
384+
return finder_instance, mod_spec
385385

386386
# Support for custom finders
387387
for meta_finder in sys.meta_path:
@@ -444,20 +444,19 @@ def find_spec(modpath: Iterable[str], path: Iterable[str] | None = None) -> Modu
444444

445445

446446
@lru_cache(maxsize=1024)
447-
def _find_spec(module_path: tuple, path: tuple) -> ModuleSpec:
447+
def _find_spec(module_path: tuple[str], path: tuple[str, ...]) -> ModuleSpec:
448448
_path = path or sys.path
449449

450450
# Need a copy for not mutating the argument.
451451
modpath = list(module_path)
452452

453453
submodule_path = None
454-
module_parts = tuple(modpath)
455454
processed: list[str] = []
456455

457456
while modpath:
458457
modname = modpath.pop(0)
459458
finder, spec = _find_spec_with_path(
460-
_path, modname, module_parts, tuple(processed), submodule_path or path
459+
_path, modname, module_path, tuple(processed), submodule_path or path
461460
)
462461
processed.append(modname)
463462
if modpath:

pyproject.toml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,24 @@ testpaths = ["tests"]
6262
filterwarnings = "error"
6363

6464
[tool.mypy]
65-
enable_error_code = "ignore-without-code"
66-
no_implicit_optional = true
65+
python_version = "3.9"
66+
files = [
67+
"astroid/interpreter/_import/",
68+
]
69+
always_false = [
70+
"PY310_PLUS",
71+
"PY311_PLUS",
72+
"PY312_PLUS",
73+
"PY313_PLUS",
74+
]
75+
disallow_any_decorated = true
76+
disallow_any_explicit = true
77+
follow_imports = "silent"
6778
scripts_are_modules = true
6879
show_error_codes = true
80+
strict = true
6981
warn_redundant_casts = true
82+
warn_unreachable = true
7083

7184
[[tool.mypy.overrides]]
7285
# Importlib typeshed stubs do not include the private functions we use

0 commit comments

Comments
 (0)