3
3
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
4
4
5
5
import abc
6
- import collections
7
6
import enum
8
7
import importlib
9
8
import importlib .machinery
13
12
import zipimport
14
13
from functools import lru_cache
15
14
from pathlib import Path
16
- from typing import List , Optional
15
+ from typing import List , NamedTuple , Optional , Sequence , Tuple
17
16
18
17
from . import util
19
18
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
- )
26
19
20
+ class ModuleType (enum .Enum ):
21
+ """Python module types used for ModuleSpec."""
27
22
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 ()
31
33
32
34
33
- class ModuleSpec (_ModuleSpec ):
35
+ class ModuleSpec (NamedTuple ):
34
36
"""Defines a class similar to PEP 420's ModuleSpec
35
37
36
38
A module spec defines a name of a module, its type, location
37
39
and where submodules can be found, if the module is a package.
38
40
"""
39
41
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
56
47
57
48
58
49
class Finder :
@@ -93,7 +84,7 @@ def contribute_to_path(self, spec, processed):
93
84
class ImportlibFinder (Finder ):
94
85
"""A finder based on the importlib module."""
95
86
96
- _SUFFIXES = (
87
+ _SUFFIXES : Sequence [ Tuple [ str , ModuleType ]] = (
97
88
[(s , ModuleType .C_EXTENSION ) for s in importlib .machinery .EXTENSION_SUFFIXES ]
98
89
+ [(s , ModuleType .PY_SOURCE ) for s in importlib .machinery .SOURCE_SUFFIXES ]
99
90
+ [(s , ModuleType .PY_COMPILED ) for s in importlib .machinery .BYTECODE_SUFFIXES ]
@@ -116,13 +107,13 @@ def find_module(
116
107
return ModuleSpec (
117
108
name = modname ,
118
109
location = None ,
119
- module_type = ModuleType .C_BUILTIN ,
110
+ type = ModuleType .C_BUILTIN ,
120
111
)
121
112
if spec .loader is importlib .machinery .FrozenImporter :
122
113
return ModuleSpec (
123
114
name = modname ,
124
115
location = getattr (spec .loader_state , "filename" , None ),
125
- module_type = ModuleType .PY_FROZEN ,
116
+ type = ModuleType .PY_FROZEN ,
126
117
)
127
118
except ValueError :
128
119
pass
@@ -137,15 +128,13 @@ def find_module(
137
128
return ModuleSpec (
138
129
name = modname ,
139
130
location = package_directory ,
140
- module_type = ModuleType .PKG_DIRECTORY ,
131
+ type = ModuleType .PKG_DIRECTORY ,
141
132
)
142
133
for suffix , type_ in ImportlibFinder ._SUFFIXES :
143
134
file_name = modname + suffix
144
135
file_path = os .path .join (entry , file_name )
145
136
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_ )
149
138
return None
150
139
151
140
def contribute_to_path (self , spec , processed ):
@@ -193,7 +182,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
193
182
name = modname ,
194
183
location = "" ,
195
184
origin = "namespace" ,
196
- module_type = ModuleType .PY_NAMESPACE ,
185
+ type = ModuleType .PY_NAMESPACE ,
197
186
submodule_search_locations = submodule_path ,
198
187
)
199
188
return None
@@ -219,7 +208,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
219
208
name = modname ,
220
209
location = filename ,
221
210
origin = "egg" ,
222
- module_type = file_type ,
211
+ type = file_type ,
223
212
submodule_search_locations = path ,
224
213
)
225
214
@@ -240,7 +229,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
240
229
name = spec .name ,
241
230
location = location ,
242
231
origin = spec .origin ,
243
- module_type = module_type ,
232
+ type = module_type ,
244
233
submodule_search_locations = list (spec .submodule_search_locations or []),
245
234
)
246
235
return spec
0 commit comments