MCPcopy Index your code
hub / github.com/python/mypy / all_imported_modules_in_file

Method all_imported_modules_in_file

mypy/build.py:1209–1260  ·  view source on GitHub ↗

Find all reachable import statements in a file. Return list of tuples (priority, module id, import line number) for all modules imported in file; lower numbers == higher priority. Can generate blocking errors on bogus relative imports.

(self, file: MypyFile)

Source from the content-addressed store, hash-verified

1207 return new_id
1208
1209 def all_imported_modules_in_file(self, file: MypyFile) -> list[tuple[int, str, int]]:
1210 """Find all reachable import statements in a file.
1211
1212 Return list of tuples (priority, module id, import line number)
1213 for all modules imported in file; lower numbers == higher priority.
1214
1215 Can generate blocking errors on bogus relative imports.
1216 """
1217 res: list[tuple[int, str, int]] = []
1218 for imp in file.imports:
1219 if not imp.is_unreachable:
1220 if isinstance(imp, Import):
1221 pri = import_priority(imp, PRI_MED)
1222 ancestor_pri = import_priority(imp, PRI_LOW)
1223 for id, _ in imp.ids:
1224 res.append((pri, id, imp.line))
1225 ancestor_parts = id.split(".")[:-1]
1226 ancestors = []
1227 for part in ancestor_parts:
1228 ancestors.append(part)
1229 res.append((ancestor_pri, ".".join(ancestors), imp.line))
1230 elif isinstance(imp, ImportFrom):
1231 cur_id = self.correct_rel_imp(file, imp)
1232 all_are_submodules = True
1233 # Also add any imported names that are submodules.
1234 pri = import_priority(imp, PRI_MED)
1235 for name, __ in imp.names:
1236 sub_id = cur_id + "." + name
1237 if self.is_module(sub_id):
1238 res.append((pri, sub_id, imp.line))
1239 else:
1240 all_are_submodules = False
1241 # Add cur_id as a dependency, even if all the
1242 # imports are submodules. Processing import from will try
1243 # to look through cur_id, so we should depend on it.
1244 # As a workaround for some bugs in cycle handling (#4498),
1245 # if all the imports are submodules, do the import at a lower
1246 # priority.
1247 pri = import_priority(imp, PRI_HIGH if not all_are_submodules else PRI_LOW)
1248 res.append((pri, cur_id, imp.line))
1249 elif isinstance(imp, ImportAll):
1250 pri = import_priority(imp, PRI_HIGH)
1251 res.append((pri, self.correct_rel_imp(file, imp), imp.line))
1252
1253 # Sort such that module (e.g. foo.bar.baz) comes before its ancestors (e.g. foo
1254 # and foo.bar) so that, if FindModuleCache finds the target module in a
1255 # package marked with py.typed underneath a namespace package installed in
1256 # site-packages, (gasp), that cache's knowledge of the ancestors
1257 # (aka FindModuleCache.ns_ancestors) can be primed when it is asked to find
1258 # the parent.
1259 res.sort(key=lambda x: -x[1].count("."))
1260 return res
1261
1262 def is_module(self, id: str) -> bool:
1263 """Does the given fullname refer to a module?

Callers 1

compute_dependenciesMethod · 0.80

Calls 9

correct_rel_impMethod · 0.95
is_moduleMethod · 0.95
isinstanceFunction · 0.85
import_priorityFunction · 0.85
appendMethod · 0.80
splitMethod · 0.80
sortMethod · 0.80
joinMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected