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

Class TypeVarLikeScope

mypy/tvar_scope.py:79–216  ·  view source on GitHub ↗

Scope that holds bindings for type variables and parameter specifications. Node fullname -> TypeVarLikeType.

Source from the content-addressed store, hash-verified

77
78
79class TypeVarLikeScope:
80 """Scope that holds bindings for type variables and parameter specifications.
81
82 Node fullname -> TypeVarLikeType.
83 """
84
85 def __init__(
86 self,
87 parent: TypeVarLikeScope | None = None,
88 is_class_scope: bool = False,
89 prohibited: TypeVarLikeScope | None = None,
90 namespace: str = "",
91 ) -> None:
92 """Initializer for TypeVarLikeScope
93
94 Parameters:
95 parent: the outer scope for this scope
96 is_class_scope: True if this represents a generic class
97 prohibited: Type variables that aren't strictly in scope exactly,
98 but can't be bound because they're part of an outer class's scope.
99 """
100 self.scope: dict[str, TypeVarLikeType] = {}
101 self.parent = parent
102 self.func_id = 0
103 self.class_id = 0
104 self.is_class_scope = is_class_scope
105 self.prohibited = prohibited
106 self.namespace = namespace
107 if parent is not None:
108 self.func_id = parent.func_id
109 self.class_id = parent.class_id
110
111 def get_function_scope(self) -> TypeVarLikeScope | None:
112 """Get the nearest parent that's a function scope, not a class scope"""
113 it: TypeVarLikeScope | None = self
114 while it is not None and it.is_class_scope:
115 it = it.parent
116 return it
117
118 def allow_binding(self, fullname: str) -> bool:
119 if fullname in self.scope:
120 return False
121 elif self.parent and not self.parent.allow_binding(fullname):
122 return False
123 elif self.prohibited and not self.prohibited.allow_binding(fullname):
124 return False
125 return True
126
127 def method_frame(self, namespace: str) -> TypeVarLikeScope:
128 """A new scope frame for binding a method"""
129 return TypeVarLikeScope(self, False, None, namespace=namespace)
130
131 def class_frame(self, namespace: str) -> TypeVarLikeScope:
132 """A new scope frame for binding a class. Prohibits *this* class's tvars"""
133 return TypeVarLikeScope(self.get_function_scope(), True, self, namespace=namespace)
134
135 def new_unique_func_id(self) -> TypeVarId:
136 """Used by plugin-like code that needs to make synthetic generic functions."""

Callers 6

__init__Method · 0.90
file_contextMethod · 0.90
analyze_type_exprMethod · 0.90
method_frameMethod · 0.85
class_frameMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…