8000 Do not consider bare TypeVar not overlapping with None for reachability analysis by sterliakov · Pull Request #18138 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Do not consider bare TypeVar not overlapping with None for reachability analysis #18138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension < 8000 /fieldset>
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Avoid passing boolean flags positionally
  • Loading branch information
sterliakov committed Nov 15, 2024
commit 70aa977fbb17527e218a6e0f475dca455422114f
4 changes: 2 additions & 2 deletions mypy/binder.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def _get(self, key: Key, index: int = -1) -> CurrentType | None:
return self.frames[i].types[key]
return None

def put(self, expr: Expression, typ: Type, from_assignment: bool = True) -> None:
def put(self, expr: Expression, typ: Type, *, from_assignment: bool = True) -> None:
if not isinstance(expr, (IndexExpr, MemberExpr, NameExpr)):
return
if not literal(expr):
Expand Down Expand Up @@ -258,7 +258,7 @@ def update_from_options(self, frames: list[Frame]) -> bool:
if simplified == self.declarations[key]:
type = simplified
if current_value is None or not is_same_type(type, current_value[0]):
self._put(key, type, True)
self._put(key, type, from_assignment=True)
changed = True

self.frames[-1].unreachable = not frames
Expand Down
21 changes: 11 additions & 10 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4725,11 +4725,11 @@ def visit_if_stmt(self, s: IfStmt) -> None:

# XXX Issue a warning if condition is always False?
with self.binder.frame_context(can_skip=True, fall_through=2):
self.push_type_map(if_map, False)
self.push_type_map(if_map, from_assignment=False)
self.accept(b)

# XXX Issue a warning if condition is always True?
self.push_type_map(else_map, False)
self.push_type_map(else_map, from_assignment=False)

with self.binder.frame_context(can_skip=False, fall_through=2):
if s.else_body:
Expand Down Expand Up @@ -5310,20 +5310,21 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
if b.is_unreachable or isinstance(
get_proper_type(pattern_type.type), UninhabitedType
):
self.push_type_map(None, False)
self.push_type_map(None, from_assignment=False)
else_map: TypeMap = {}
else:
pattern_map, else_map = conditional_types_to_typemaps(
named_subject, pattern_type.type, pattern_type.rest_type
)
self.remove_capture_conflicts(pattern_type.captures, inferred_types)
self.push_type_map(pattern_map, False)
self.push_type_map(pattern_map, from_assignment=False)
if pattern_map:
for expr, typ in pattern_map.items():
self.push_type_map(
self._get_recursive_sub_patterns_map(expr, typ), False
self._get_recursive_sub_patterns_map(expr, typ),
from_assignment=False,
)
self.push_type_map(pattern_type.captures, False)
self.push_type_map(pattern_type.captures, from_assignment=False)
if g is not None:
with self.binder.frame_context(can_skip=False, fall_through=3):
gt = get_proper_type(self.expr_checker.accept(g))
Expand All @@ -5349,11 +5350,11 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
continue
type_map[named_subject] = type_map[expr]

self.push_type_map(guard_map, False)
self.push_type_map(guard_map, from_assignment=False)
self.accept(b)
else:
self.accept(b)
self.push_type_map(else_map, False)
self.push_type_map(else_map, from_assignment=False)

# This is needed due to a quirk in frame_context. Without it types will stay narrowed
# after the match.
Expand Down Expand Up @@ -7367,12 +7368,12 @@ def iterable_item_type(
def function_type(self, func: FuncBase) -> FunctionLike:
return function_type(func, self.named_type("builtins.function"))

def push_type_map(self, type_map: TypeMap, from_assignment: bool = True) -> None:
def push_type_map(self, type_map: TypeMap, *, from_assignment: bool = True) -> None:
if type_map is None:
self.binder.unreachable()
else:
for expr, type in type_map.items():
self.binder.put(expr, type, from_assignment)
self.binder.put(expr, type, from_assignment=from_assignment)

def infer_issubclass_maps(self, node: CallExpr, expr: Expression) -> tuple[TypeMap, TypeMap]:
"""Infer type restrictions for an expression in issubclass call."""
Expand Down
Loading
0