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

Method analyze_static_reference

mypy/checkexpr.py:399–445  ·  view source on GitHub ↗

This is the version of analyze_ref_expr() that doesn't do any deferrals. This function can be used by member access to "static" attributes. For example, when accessing module attributes in protocol checks, or accessing attributes of special kinds (like TypeAlias, Ty

(
        self,
        node: SymbolNode,
        ctx: Context,
        is_lvalue: bool,
        *,
        include_modules: bool = True,
        suppress_errors: bool = False,
    )

Source from the content-addressed store, hash-verified

397 return result
398
399 def analyze_static_reference(
400 self,
401 node: SymbolNode,
402 ctx: Context,
403 is_lvalue: bool,
404 *,
405 include_modules: bool = True,
406 suppress_errors: bool = False,
407 ) -> Type:
408 """
409 This is the version of analyze_ref_expr() that doesn't do any deferrals.
410
411 This function can be used by member access to "static" attributes. For example,
412 when accessing module attributes in protocol checks, or accessing attributes of
413 special kinds (like TypeAlias, TypeInfo, etc.) on an instance or class object.
414 # TODO: merge with analyze_ref_expr() when we are confident about performance.
415 """
416 if isinstance(node, (Var, Decorator, OverloadedFuncDef)):
417 return node.type or AnyType(TypeOfAny.special_form)
418 elif isinstance(node, FuncDef):
419 return function_type(node, self.named_type("builtins.function"))
420 elif isinstance(node, TypeInfo):
421 # Reference to a type object.
422 if node.typeddict_type:
423 # We special-case TypedDict, because they don't define any constructor.
424 return self.typeddict_callable(node)
425 elif node.fullname == "types.NoneType":
426 # We special case NoneType, because its stub definition is not related to None.
427 return TypeType(NoneType())
428 else:
429 return type_object_type(node)
430 elif isinstance(node, TypeAlias):
431 # Something that refers to a type alias appears in runtime context.
432 # Note that we suppress bogus errors for alias redefinitions,
433 # they are already reported in semanal.py.
434 with self.msg.filter_errors() if suppress_errors else nullcontext():
435 return self.alias_type_in_runtime_context(
436 node, ctx=ctx, alias_definition=is_lvalue
437 )
438 elif isinstance(node, TypeVarExpr):
439 return self.named_type("typing.TypeVar")
440 elif isinstance(node, (ParamSpecExpr, TypeVarTupleExpr)):
441 return self.object_type()
442 elif isinstance(node, MypyFile):
443 # Reference to a module object.
444 return self.module_type(node) if include_modules else AnyType(TypeOfAny.special_form)
445 return AnyType(TypeOfAny.from_error)
446
447 def analyze_var_ref(self, var: Var, context: Context) -> Type:
448 if var.type:

Callers 4

analyze_ref_exprMethod · 0.95
module_typeMethod · 0.95

Calls 12

named_typeMethod · 0.95
typeddict_callableMethod · 0.95
object_typeMethod · 0.95
module_typeMethod · 0.95
AnyTypeClass · 0.90
function_typeFunction · 0.90
TypeTypeClass · 0.90
NoneTypeClass · 0.90
type_object_typeFunction · 0.90
isinstanceFunction · 0.85
filter_errorsMethod · 0.80

Tested by

no test coverage detected