(builder: IRBuilder, typ: TypeInfo, unbounded_type: Type | None, line: int)
| 918 | |
| 919 | |
| 920 | def load_type(builder: IRBuilder, typ: TypeInfo, unbounded_type: Type | None, line: int) -> Value: |
| 921 | # typ.fullname contains the module where the class object was defined. However, it is possible |
| 922 | # that the class object's module was not imported in the file currently being compiled. So, we |
| 923 | # use unbounded_type.name (if provided by caller) to load the class object through one of the |
| 924 | # imported modules. |
| 925 | # Example: for `json.JSONDecoder`, typ.fullname is `json.decoder.JSONDecoder` but the Python |
| 926 | # file may import `json` not `json.decoder`. |
| 927 | # Another corner case: The Python file being compiled imports mod1 and has a type hint |
| 928 | # `mod1.OuterClass.InnerClass`. But, mod1/__init__.py might import OuterClass like this: |
| 929 | # `from mod2.mod3 import OuterClass`. In this case, typ.fullname is |
| 930 | # `mod2.mod3.OuterClass.InnerClass` and `unbounded_type.name` is `mod1.OuterClass.InnerClass`. |
| 931 | # So, we must use unbounded_type.name to load the class object. |
| 932 | # See issue mypyc/mypyc#1087. |
| 933 | if typ in builder.mapper.type_to_ir: |
| 934 | class_ir = builder.mapper.type_to_ir[typ] |
| 935 | class_obj = builder.builder.get_native_type(class_ir) |
| 936 | elif builtin := builder.load_builtin(typ.fullname, line): |
| 937 | class_obj = builtin |
| 938 | elif isinstance(unbounded_type, UnboundType): |
| 939 | path_parts = unbounded_type.name.split(".") |
| 940 | class_obj = builder.load_global_str(path_parts[0], line) |
| 941 | for attr in path_parts[1:]: |
| 942 | class_obj = builder.py_get_attr(class_obj, attr, line) |
| 943 | else: |
| 944 | class_obj = builder.load_global_str(typ.name, line) |
| 945 | |
| 946 | return class_obj |
| 947 | |
| 948 | |
| 949 | def load_func(builder: IRBuilder, func_name: str, fullname: str | None, line: int) -> Value: |
no test coverage detected
searching dependent graphs…