MCPcopy Create free account
hub / github.com/python/mypy / analyze_alias

Method analyze_alias

mypy/semanal.py:3979–4046  ·  view source on GitHub ↗

Check if 'rvalue' is a valid type allowed for aliasing (e.g. not a type variable). If yes, return the corresponding type, a list of type variables for generic aliases, a set of names the alias depends on, and True if the original type has empty tuple index. An example for th

(
        self,
        name: str,
        rvalue: Expression,
        allow_placeholder: bool = False,
        declared_type_vars: TypeVarLikeList | None = None,
        all_declared_type_params_names: list[str] | None = None,
        python_3_12_type_alias: bool = False,
    )

Source from the content-addressed store, hash-verified

3977 return typ
3978
3979 def analyze_alias(
3980 self,
3981 name: str,
3982 rvalue: Expression,
3983 allow_placeholder: bool = False,
3984 declared_type_vars: TypeVarLikeList | None = None,
3985 all_declared_type_params_names: list[str] | None = None,
3986 python_3_12_type_alias: bool = False,
3987 ) -> tuple[Type | None, list[TypeVarLikeType], set[str], bool]:
3988 """Check if 'rvalue' is a valid type allowed for aliasing (e.g. not a type variable).
3989
3990 If yes, return the corresponding type, a list of type variables for generic aliases,
3991 a set of names the alias depends on, and True if the original type has empty tuple index.
3992 An example for the dependencies:
3993 A = int
3994 B = str
3995 analyze_alias(dict[A, B])[2] == {'__main__.A', '__main__.B'}
3996 """
3997 dynamic = bool(self.function_stack and self.function_stack[-1].is_dynamic())
3998 global_scope = not self.type and not self.function_stack
3999 try:
4000 typ = expr_to_unanalyzed_type(
4001 rvalue, self.options, self.is_stub_file, lookup_qualified=self.lookup_qualified
4002 )
4003 except TypeTranslationError:
4004 self.fail(
4005 "Invalid type alias: expression is not a valid type", rvalue, code=codes.VALID_TYPE
4006 )
4007 return None, [], set(), False
4008
4009 found_type_vars = self.find_type_var_likes(typ)
4010 namespace = self.qualified_name(name)
4011 alias_type_vars = found_type_vars if declared_type_vars is None else declared_type_vars
4012 with self.tvar_scope_frame(self.tvar_scope.class_frame(namespace)):
4013 tvar_defs = self.tvar_defs_from_tvars(alias_type_vars, typ)
4014
4015 if python_3_12_type_alias:
4016 with self.allow_unbound_tvars_set():
4017 rvalue.accept(self)
4018
4019 analyzed, depends_on = analyze_type_alias(
4020 typ,
4021 self,
4022 self.tvar_scope,
4023 self.plugin,
4024 self.options,
4025 self.cur_mod_node,
4026 self.is_typeshed_stub_file,
4027 allow_placeholder=allow_placeholder,
4028 in_dynamic_func=dynamic,
4029 global_scope=global_scope,
4030 allowed_alias_tvars=tvar_defs,
4031 alias_type_params_names=all_declared_type_params_names,
4032 python_3_12_type_alias=python_3_12_type_alias,
4033 )
4034
4035 # There can be only one variadic variable at most, the error is reported elsewhere.
4036 new_tvar_defs = []

Callers 2

visit_type_alias_stmtMethod · 0.95

Calls 15

failMethod · 0.95
find_type_var_likesMethod · 0.95
qualified_nameMethod · 0.95
tvar_scope_frameMethod · 0.95
tvar_defs_from_tvarsMethod · 0.95
expr_to_unanalyzed_typeFunction · 0.90
analyze_type_aliasFunction · 0.90
boolClass · 0.85
setClass · 0.85
isinstanceFunction · 0.85
class_frameMethod · 0.80

Tested by

no test coverage detected