Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable mypy checking for astroid/interpreter/_import/ #2530

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still exclude testdata right? nvm, saw that everything happens in the pyproject.toml now.

- 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The identifier spec is used on line 410 for a different assignment, which mypy warned about:

astroid/interpreter/_import/spec.py:410: error: Incompatible types in assignment (expression has type "Optional[importlib.machinery.ModuleSpec]", variable has type "Optional[astroid.interpreter._import.spec.ModuleSpec]")  [assignment]

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
Loading