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

Function transform_import_from_buckets

mypyc/irbuild/statement.py:525–585  ·  view source on GitHub ↗

Handle 'from module_id import names' by dispatching each bucket to the right strategy.

(
    builder: IRBuilder,
    module_id: str,
    names: list[str],
    as_names: list[str],
    line: int,
    parent_is_native: bool,
)

Source from the content-addressed store, hash-verified

523
524
525def transform_import_from_buckets(
526 builder: IRBuilder,
527 module_id: str,
528 names: list[str],
529 as_names: list[str],
530 line: int,
531 parent_is_native: bool,
532) -> None:
533 """Handle 'from module_id import names' by dispatching each bucket to the right strategy."""
534 buckets = classify_import_from(builder, module_id, names, as_names, parent_is_native)
535 module = None
536 for bucket in buckets:
537 if bucket.kind == IMPORT_NATIVE_SUBMODULE:
538 group: list[tuple[str, str | None, int]] = [
539 (f"{module_id}.{name}", as_name, line)
540 for name, as_name in zip(bucket.names, bucket.as_names)
541 ]
542 transform_imports_without_grouping(builder, group)
543 elif bucket.kind == IMPORT_NATIVE_ATTR:
544 builder.gen_import(module_id, line)
545 names_literal = builder.add(LoadLiteral(tuple(bucket.names), object_rprimitive))
546 if bucket.as_names == bucket.names:
547 as_names_literal = names_literal
548 else:
549 as_names_literal = builder.add(
550 LoadLiteral(tuple(bucket.as_names), object_rprimitive)
551 )
552 builder.call_c(
553 get_native_attrs_op,
554 [
555 builder.load_str(module_id),
556 names_literal,
557 as_names_literal,
558 builder.load_globals_dict(),
559 ],
560 line,
561 )
562 else:
563 assert bucket.kind == IMPORT_NON_NATIVE
564 # Note that we miscompile import from inside of functions here,
565 # since that case *shouldn't* load everything into the globals dict.
566 # This probably doesn't matter much and the code runs basically right.
567 names_literal = builder.add(LoadLiteral(tuple(bucket.names), object_rprimitive))
568 if bucket.as_names == bucket.names:
569 as_names_literal = names_literal
570 else:
571 as_names_literal = builder.add(
572 LoadLiteral(tuple(bucket.as_names), object_rprimitive)
573 )
574 module = builder.call_c(
575 import_from_many_op,
576 [
577 builder.load_str(module_id),
578 names_literal,
579 as_names_literal,
580 builder.load_globals_dict(),
581 ],
582 line,

Callers 1

transform_import_fromFunction · 0.85

Calls 11

LoadLiteralClass · 0.90
InitStaticClass · 0.90
classify_import_fromFunction · 0.85
zipFunction · 0.85
tupleClass · 0.85
gen_importMethod · 0.80
load_globals_dictMethod · 0.80
addMethod · 0.45
call_cMethod · 0.45
load_strMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…