Debian Codegraph @

mypy/tvar_scope.py

raw
1 from __future__ import annotations
2
3 from collections.abc import Callable
4 from typing import TypeAlias as _TypeAlias
5
6 from mypy.nodes import (
7 Context,
8 ParamSpecExpr,
9 SymbolTableNode,
10 TypeVarExpr,
11 TypeVarLikeExpr,
12 TypeVarTupleExpr,
13 )
14 from mypy.types import (
15 ParamSpecFlavor,
16 ParamSpecType,
17 TypeVarId,
18 TypeVarLikeType,
19 TypeVarTupleType,
20 TypeVarType,
21 )
22
23 FailFunc: _TypeAlias = Callable[[str, Context], None]
24
25
26 class TypeVarLikeScope:
27 """Scope that holds bindings for type variables and parameter specifications.
28
29 Node fullname -> TypeVarLikeType.
30 """
31
32 def __init__(
33 self,
34 parent: TypeVarLikeScope | None = None,
35 is_class_scope: bool = False,
36 prohibited: TypeVarLikeScope | None = None,
37 namespace: str = "",
38 ) -> None:
39 """Initializer for TypeVarLikeScope
40
41 Parameters:
42 parent: the outer scope for this scope
43 is_class_scope: True if this represents a generic class
44 prohibited: Type variables that aren't strictly in scope exactly,
45 but can't be bound because they're part of an outer class's scope.
46 """
47 self.scope: dict[str, TypeVarLikeType] = {}
48 self.parent = parent
49 self.func_id = 0
50 self.class_id = 0
51 self.is_class_scope = is_class_scope
52 self.prohibited = prohibited
53 self.namespace = namespace
54 if parent is not None:
55 self.func_id = parent.func_id
56 self.class_id = parent.class_id
57
58 def get_function_scope(self) -> TypeVarLikeScope | None:
59 """Get the nearest parent that's a function scope, not a class scope"""
60 it: TypeVarLikeScope | None = self
61 while it is not None and it.is_class_scope:
62 it = it.parent
63 return it
64
65 def allow_binding(self, fullname: str) -> bool:
66 if fullname in self.scope:
67 return False
68 elif self.parent and not self.parent.allow_binding(fullname):
69 return False
70 elif self.prohibited and not self.prohibited.allow_binding(fullname):
71 return False
72 return True
73
74 def method_frame(self, namespace: str) -> TypeVarLikeScope:
75 """A new scope frame for binding a method"""
76 return TypeVarLikeScope(self, False, None, namespace=namespace)
77
78 def class_frame(self, namespace: str) -> TypeVarLikeScope:
79 """A new scope frame for binding a class. Prohibits *this* class's tvars"""
80 return TypeVarLikeScope(self.get_function_scope(), True, self, namespace=namespace)
81
82 def new_unique_func_id(self) -> TypeVarId:
83 """Used by plugin-like code that needs to make synthetic generic functions."""
84 self.func_id -= 1
85 return TypeVarId(self.func_id)
86
87 def bind_new(
88 self, name: str, tvar_expr: TypeVarLikeExpr, fail_func: FailFunc, context: Context
89 ) -> TypeVarLikeType:
90 if self.is_class_scope:
91 self.class_id += 1
92 i = self.class_id
93 else:
94 self.func_id -= 1
95 i = self.func_id
96 namespace = self.namespace
97
98 if isinstance(tvar_expr, TypeVarExpr):
99 tvar_def: TypeVarLikeType = TypeVarType(
100 name=name,
101 fullname=tvar_expr.fullname,
102 id=TypeVarId(i, namespace=namespace),
103 values=tvar_expr.values,
104 upper_bound=tvar_expr.upper_bound,
105 default=tvar_expr.default,
106 variance=tvar_expr.variance,
107 line=tvar_expr.line,
108 column=tvar_expr.column,
109 )
110 elif isinstance(tvar_expr, ParamSpecExpr):
111 tvar_def = ParamSpecType(
112 name=name,
113 fullname=tvar_expr.fullname,
114 id=TypeVarId(i, namespace=namespace),
115 flavor=ParamSpecFlavor.BARE,
116 upper_bound=tvar_expr.upper_bound,
117 default=tvar_expr.default,
118 line=tvar_expr.line,
119 column=tvar_expr.column,
120 )
121 elif isinstance(tvar_expr, TypeVarTupleExpr):
122 tvar_def = TypeVarTupleType(
123 name=name,
124 fullname=tvar_expr.fullname,
125 id=TypeVarId(i, namespace=namespace),
126 upper_bound=tvar_expr.upper_bound,
127 tuple_fallback=tvar_expr.tuple_fallback,
128 default=tvar_expr.default,
129 line=tvar_expr.line,
130 column=tvar_expr.column,
131 )
132 else:
133 assert False
134 self.scope[tvar_expr.fullname] = tvar_def
135 return tvar_def
136
137 def bind_existing(self, tvar_def: TypeVarLikeType) -> None:
138 self.scope[tvar_def.fullname] = tvar_def
139
140 def get_binding(self, item: str | SymbolTableNode) -> TypeVarLikeType | None:
141 fullname = item.fullname if isinstance(item, SymbolTableNode) else item
142 assert fullname
143 if fullname in self.scope:
144 return self.scope[fullname]
145 elif self.parent is not None:
146 return self.parent.get_binding(fullname)
147 else:
148 return None
149
150 def __str__(self) -> str:
151 me = ", ".join(f"{k}: {v.name}`{v.id}" for k, v in self.scope.items())
152 if self.parent is None:
153 return me
154 return f"{self.parent} <- {me}"