Skip to content

Commit d868a52

Browse files
committed
Address feedback
1 parent 2371e2c commit d868a52

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

mypy/binder.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ def _get(self, key: Key, index: int = -1) -> CurrentType | None:
162162
return None
163163

164164
def put(self, expr: Expression, typ: Type, *, from_assignment: bool = True) -> None:
165+
"""Directly set the narrowed type of expression (if it supports it).
166+
167+
This is used for isinstance() etc. Assignments should go through assign_type().
168+
"""
165169
if not isinstance(expr, (IndexExpr, MemberExpr, NameExpr)):
166170
return
167171
if not literal(expr):
@@ -325,6 +329,13 @@ def accumulate_type_assignments(self) -> Iterator[Assigns]:
325329
self.type_assignments = old_assignments
326330

327331
def assign_type(self, expr: Expression, type: Type, declared_type: Type | None) -> None:
332+
"""Narrow type of expression through an assignment.
333+
334+
Do nothing if the expression doesn't support narrowing.
335+
336+
When not narrowing though an assignment (isinstance() etc.), use put()
337+
directly. This omits some special-casing logic for assignments.
338+
"""
328339
# We should erase last known value in binder, because if we are using it,
329340
# it means that the target is not final, and therefore can't hold a literal.
330341
type = remove_instance_last_known_values(type)

0 commit comments

Comments
 (0)