Skip to content

Commit 23cb69a

Browse files
authored
Add annotations for ModuleType (#1525)
1 parent c6293b4 commit 23cb69a

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Release date: TBA
1313

1414
Closes #1512
1515

16+
* Rename ``ModuleSpec`` -> ``module_type`` constructor parameter to match attribute
17+
name and improve typing. Use ``type`` instead.
18+
1619

1720
What's New in astroid 2.11.4?
1821
=============================

astroid/interpreter/_import/spec.py

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
44

55
import abc
6-
import collections
76
import enum
87
import importlib
98
import importlib.machinery
@@ -13,46 +12,38 @@
1312
import zipimport
1413
from functools import lru_cache
1514
from pathlib import Path
16-
from typing import List, Optional
15+
from typing import List, NamedTuple, Optional, Sequence, Tuple
1716

1817
from . import util
1918

20-
ModuleType = enum.Enum(
21-
"ModuleType",
22-
"C_BUILTIN C_EXTENSION PKG_DIRECTORY "
23-
"PY_CODERESOURCE PY_COMPILED PY_FROZEN PY_RESOURCE "
24-
"PY_SOURCE PY_ZIPMODULE PY_NAMESPACE",
25-
)
2619

20+
class ModuleType(enum.Enum):
21+
"""Python module types used for ModuleSpec."""
2722

28-
_ModuleSpec = collections.namedtuple(
29-
"_ModuleSpec", "name type location " "origin submodule_search_locations"
30-
)
23+
C_BUILTIN = enum.auto()
24+
C_EXTENSION = enum.auto()
25+
PKG_DIRECTORY = enum.auto()
26+
PY_CODERESOURCE = enum.auto()
27+
PY_COMPILED = enum.auto()
28+
PY_FROZEN = enum.auto()
29+
PY_RESOURCE = enum.auto()
30+
PY_SOURCE = enum.auto()
31+
PY_ZIPMODULE = enum.auto()
32+
PY_NAMESPACE = enum.auto()
3133

3234

33-
class ModuleSpec(_ModuleSpec):
35+
class ModuleSpec(NamedTuple):
3436
"""Defines a class similar to PEP 420's ModuleSpec
3537
3638
A module spec defines a name of a module, its type, location
3739
and where submodules can be found, if the module is a package.
3840
"""
3941

40-
def __new__(
41-
cls,
42-
name,
43-
module_type,
44-
location=None,
45-
origin=None,
46-
submodule_search_locations=None,
47-
):
48-
return _ModuleSpec.__new__(
49-
cls,
50-
name=name,
51-
type=module_type,
52-
location=location,
53-
origin=origin,
54-
submodule_search_locations=submodule_search_locations,
55-
)
42+
name: str
43+
type: ModuleType
44+
location: "str | None" = None
45+
origin: "str | None" = None
46+
submodule_search_locations: "Sequence[str] | None" = None
5647

5748

5849
class Finder:
@@ -93,7 +84,7 @@ def contribute_to_path(self, spec, processed):
9384
class ImportlibFinder(Finder):
9485
"""A finder based on the importlib module."""
9586

96-
_SUFFIXES = (
87+
_SUFFIXES: Sequence[Tuple[str, ModuleType]] = (
9788
[(s, ModuleType.C_EXTENSION) for s in importlib.machinery.EXTENSION_SUFFIXES]
9889
+ [(s, ModuleType.PY_SOURCE) for s in importlib.machinery.SOURCE_SUFFIXES]
9990
+ [(s, ModuleType.PY_COMPILED) for s in importlib.machinery.BYTECODE_SUFFIXES]
@@ -116,13 +107,13 @@ def find_module(
116107
return ModuleSpec(
117108
name=modname,
118109
location=None,
119-
module_type=ModuleType.C_BUILTIN,
110+
type=ModuleType.C_BUILTIN,
120111
)
121112
if spec.loader is importlib.machinery.FrozenImporter:
122113
return ModuleSpec(
123114
name=modname,
124115
location=getattr(spec.loader_state, "filename", None),
125-
module_type=ModuleType.PY_FROZEN,
116+
type=ModuleType.PY_FROZEN,
126117
)
127118
except ValueError:
128119
pass
@@ -137,15 +128,13 @@ def find_module(
137128
return ModuleSpec(
138129
name=modname,
139130
location=package_directory,
140-
module_type=ModuleType.PKG_DIRECTORY,
131+
type=ModuleType.PKG_DIRECTORY,
141132
)
142133
for suffix, type_ in ImportlibFinder._SUFFIXES:
143134
file_name = modname + suffix
144135
file_path = os.path.join(entry, file_name)
145136
if os.path.isfile(file_path):
146-
return ModuleSpec(
147-
name=modname, location=file_path, module_type=type_
148-
)
137+
return ModuleSpec(name=modname, location=file_path, type=type_)
149138
return None
150139

151140
def contribute_to_path(self, spec, processed):
@@ -193,7 +182,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
193182
name=modname,
194183
location="",
195184
origin="namespace",
196-
module_type=ModuleType.PY_NAMESPACE,
185+
type=ModuleType.PY_NAMESPACE,
197186
submodule_search_locations=submodule_path,
198187
)
199188
return None
@@ -219,7 +208,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
219208
name=modname,
220209
location=filename,
221210
origin="egg",
222-
module_type=file_type,
211+
type=file_type,
223212
submodule_search_locations=path,
224213
)
225214

@@ -240,7 +229,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
240229
name=spec.name,
241230
location=location,
242231
origin=spec.origin,
243-
module_type=module_type,
232+
type=module_type,
244233
submodule_search_locations=list(spec.submodule_search_locations or []),
245234
)
246235
return spec

astroid/modutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def file_info_from_modpath(modpath, path=None, context_file=None):
345345
return spec.ModuleSpec(
346346
name="os.path",
347347
location=os.path.__file__,
348-
module_type=spec.ModuleType.PY_SOURCE,
348+
type=spec.ModuleType.PY_SOURCE,
349349
)
350350
return _spec_from_modpath(modpath, path, context)
351351

0 commit comments

Comments
 (0)