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

Method visit_import_from

mypy/semanal.py:2971–3063  ·  view source on GitHub ↗
(self, imp: ImportFrom)

Source from the content-addressed store, hash-verified

2969 )
2970
2971 def visit_import_from(self, imp: ImportFrom) -> None:
2972 self.statement = imp
2973 module_id = self.correct_relative_import(imp)
2974 module = self.modules.get(module_id)
2975 for id, as_id in imp.names:
2976 fullname = module_id + "." + id
2977 self.set_future_import_flags(fullname)
2978 if module is None:
2979 node = None
2980 elif module_id == self.cur_mod_id and fullname in self.modules:
2981 # Submodule takes precedence over definition in surround package, for
2982 # compatibility with runtime semantics in typical use cases. This
2983 # could more precisely model runtime semantics by taking into account
2984 # the line number beyond which the local definition should take
2985 # precedence, but doesn't seem to be important in most use cases.
2986 node = SymbolTableNode(GDEF, self.modules[fullname])
2987 else:
2988 if id == as_id == "__all__":
2989 # For modules with __all__ public status of symbols is determined uniquely
2990 # by contents of __all__, so we can recover the latter here, and avoid
2991 # serializing this (redundant) information in MypyFile.
2992 self.all_exports[:] = [
2993 name for name, sym in module.names.items() if sym.module_public
2994 ]
2995 node = module.names.get(id)
2996
2997 missing_submodule = False
2998 imported_id = as_id or id
2999
3000 # Modules imported in a stub file without using 'from Y import X as X' will
3001 # not get exported.
3002 # When implicit re-exporting is disabled, we have the same behavior as stubs.
3003 use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
3004 module_public = use_implicit_reexport or (as_id is not None and id == as_id)
3005
3006 # If the module does not contain a symbol with the name 'id',
3007 # try checking if it's a module instead.
3008 if not node:
3009 mod = self.modules.get(fullname)
3010 if mod is not None:
3011 kind = self.current_symbol_kind()
3012 node = SymbolTableNode(kind, mod)
3013 elif fullname in self.missing_modules:
3014 missing_submodule = True
3015 # If it is still not resolved, check for a module level __getattr__
3016 if module and not node and "__getattr__" in module.names:
3017 # We store the fullname of the original definition so that we can
3018 # detect whether two imported names refer to the same thing.
3019 fullname = module_id + "." + id
3020 gvar = self.create_getattr_var(module.names["__getattr__"], imported_id, fullname)
3021 if gvar:
3022 self.add_symbol(
3023 imported_id,
3024 gvar,
3025 imp,
3026 module_public=module_public,
3027 module_hidden=not module_public,
3028 )

Callers

nothing calls this directly

Calls 11

current_symbol_kindMethod · 0.95
create_getattr_varMethod · 0.95
add_symbolMethod · 0.95
SymbolTableNodeClass · 0.90
getMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected