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

Function transform_import

mypyc/irbuild/statement.py:329–368  ·  view source on GitHub ↗
(builder: IRBuilder, node: Import)

Source from the content-addressed store, hash-verified

327
328
329def transform_import(builder: IRBuilder, node: Import) -> None:
330 if node.is_mypy_only:
331 return
332
333 # Imports (not from imports!) are processed in an odd way so they can be
334 # table-driven and compact. Here's how it works:
335 #
336 # Import nodes are divided in groups (in the prebuild visitor). Each group
337 # consists of consecutive Import nodes:
338 #
339 # import mod <| group #1
340 # import mod2 |
341 #
342 # def foo() -> None:
343 # import mod3 <- group #2 (*)
344 #
345 # import mod4 <| group #3
346 # import mod5 |
347 #
348 # Every time we encounter the first import of a group, build IR to import
349 # all modules in the group. Native same-group imports are handled individually,
350 # while non-native imports use a table-driven helper for compactness.
351
352 if not node.is_top_level:
353 # (*) Unless the import is within a function. In that case, prioritize
354 # speed over codesize when generating IR.
355 group = [(mod_id, as_id, node.line) for mod_id, as_id in node.ids]
356 transform_imports_without_grouping(builder, group)
357 return
358
359 if node not in builder.module_import_groups:
360 return
361
362 group_nodes = builder.module_import_groups[node]
363 subgroups = split_import_group_to_python_and_native(builder, group_nodes)
364 for subgroup, is_native in subgroups:
365 if is_native:
366 transform_imports_without_grouping(builder, subgroup)
367 else:
368 transform_non_native_import_group(builder, subgroup)
369
370
371def split_import_group_to_python_and_native(

Callers 1

visit_importMethod · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…