MCPcopy
hub / github.com/python/mypy / file_context

Method file_context

mypy/semanal.py:902–953  ·  view source on GitHub ↗

Configure analyzer for analyzing targets within a file/class. Args: file_node: target file options: options specific to the file active_type: must be the surrounding class to analyze method targets

(
        self, file_node: MypyFile, options: Options, active_type: TypeInfo | None = None
    )

Source from the content-addressed store, hash-verified

900
901 @contextmanager
902 def file_context(
903 self, file_node: MypyFile, options: Options, active_type: TypeInfo | None = None
904 ) -> Iterator[None]:
905 """Configure analyzer for analyzing targets within a file/class.
906
907 Args:
908 file_node: target file
909 options: options specific to the file
910 active_type: must be the surrounding class to analyze method targets
911 """
912 scope = self.scope
913 self.options = options
914 self.errors.set_file(file_node.path, file_node.fullname, scope=scope, options=options)
915 self.cur_mod_node = file_node
916 self.cur_mod_id = file_node.fullname
917 with scope.module_scope(self.cur_mod_id):
918 self._is_stub_file = file_node.path.lower().endswith(".pyi")
919 self._is_typeshed_stub_file = file_node.is_typeshed_file(options)
920 self.globals = file_node.names
921 self.tvar_scope = TypeVarLikeScope()
922
923 self.named_tuple_analyzer = NamedTupleAnalyzer(options, self, self.msg)
924 self.typed_dict_analyzer = TypedDictAnalyzer(options, self, self.msg)
925 self.enum_call_analyzer = EnumCallAnalyzer(options, self)
926 self.newtype_analyzer = NewTypeAnalyzer(options, self, self.msg)
927
928 # Counter that keeps track of references to undefined things potentially caused by
929 # incomplete namespaces.
930 self.num_incomplete_refs = 0
931
932 if active_type:
933 enclosing_fullname = active_type.fullname.rsplit(".", 1)[0]
934 if "." in enclosing_fullname:
935 enclosing_node = self.lookup_fully_qualified_or_none(enclosing_fullname)
936 if enclosing_node and isinstance(enclosing_node.node, TypeInfo):
937 self._type = enclosing_node.node
938 self.push_type_args(active_type.defn.type_args, active_type.defn)
939 self.incomplete_type_stack.append(False)
940 scope.enter_class(active_type)
941 self.enter_class(active_type.defn.info)
942 for tvar in active_type.defn.type_vars:
943 self.tvar_scope.bind_existing(tvar)
944
945 yield
946
947 if active_type:
948 scope.leave_class()
949 self.leave_class()
950 self._type = None
951 self.incomplete_type_stack.pop()
952 self.pop_type_args(active_type.defn.type_args)
953 del self.options
954
955 #
956 # Functions

Callers 5

refresh_partialMethod · 0.95
process_top_levelsFunction · 0.80
apply_hooks_to_classFunction · 0.80

Calls 15

push_type_argsMethod · 0.95
enter_classMethod · 0.95
leave_classMethod · 0.95
pop_type_argsMethod · 0.95
TypeVarLikeScopeClass · 0.90
NamedTupleAnalyzerClass · 0.90
TypedDictAnalyzerClass · 0.90
EnumCallAnalyzerClass · 0.90
NewTypeAnalyzerClass · 0.90
isinstanceFunction · 0.85
set_fileMethod · 0.80

Tested by

no test coverage detected