Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Introduce temporary named expressions formatch subjects#18446

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
b3ec408
Create fake named expressions for `match` subject in more cases
sterliakovJan 11, 2025
2da85ea
Merge remote-tracking branch 'upstream/master' into bugfix/st-synthet…
sterliakovJan 13, 2025
0160d1d
Merge remote-tracking branch 'upstream/master' into bugfix/st-synthet…
sterliakovJan 13, 2025
01b92c9
Merge branch 'master' into bugfix/st-synthetic-named-expr-in-match
sterliakovJan 20, 2025
9967f27
Merge remote-tracking branch 'upstream/master' into bugfix/st-synthet…
sterliakovFeb 1, 2025
6e09fd0
Merge remote-tracking branch 'upstream/master' into bugfix/st-synthet…
sterliakovMar 29, 2025
5e0b259
Add the original subject to typemap for inference
sterliakovApr 4, 2025
968a8bd
Merge remote-tracking branch 'upstream/master' into bugfix/st-synthet…
sterliakovApr 4, 2025
85d16bd
Add comment
sterliakovApr 4, 2025
ded98cb
And we can keep inline collection literals too now since we infer both
sterliakovApr 5, 2025
a772dc9
Merge branch 'master' into bugfix/st-synthetic-named-expr-in-match
sterliakovJun 21, 2025
a24af26
Merge branch 'master' into bugfix/st-synthetic-named-expr-in-match
sterliakovJun 28, 2025
a9f206b
Merge branch 'master' into bugfix/st-synthetic-named-expr-in-match
sterliakovJul 14, 2025
8f8cf42
Merge branch 'master' into bugfix/st-synthetic-named-expr-in-match
sterliakovJul 21, 2025
0bf5dc9
Merge branch 'master' into bugfix/st-synthetic-named-expr-in-match
ilevkivskyiAug 3, 2025
914f6c8
Simplify the check: we can name primitive literals just as well, it s…
sterliakovAug 3, 2025
4773a38
Move this logic closer to binder, stop putting AssignmentExpr directly
sterliakovAug 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
Move this logic closer to binder, stop putting AssignmentExpr directly
  • Loading branch information
@sterliakov
sterliakov committedAug 3, 2025
commit4773a38cce373efdb805d8b574d3e30858e672ac
20 changes: 19 additions & 1 deletionmypy/binder.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,16 @@

from mypy.erasetype import remove_instance_last_known_values
from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash, subkeys
from mypy.nodes import Expression, IndexExpr, MemberExpr, NameExpr, RefExpr, TypeInfo, Var
from mypy.nodes import (
LITERAL_NO,
Expression,
IndexExpr,
MemberExpr,
NameExpr,
RefExpr,
TypeInfo,
Var,
)
from mypy.options import Options
from mypy.subtypes import is_same_type, is_subtype
from mypy.typeops import make_simplified_union
Expand DownExpand Up@@ -173,6 +182,15 @@ def _get(self, key: Key, index: int = -1) -> CurrentType | None:
return self.frames[i].types[key]
return None

@classmethod
def can_put_directly(cls, expr: Expression) -> bool:
"""Will `.put()` on this expression be successful?

This is inlined in `.put()` because the logic is rather hot and must be kept
in sync.
"""
return isinstance(expr, (IndexExpr, MemberExpr, NameExpr)) and literal(expr) > LITERAL_NO

def put(self, expr: Expression, typ: Type, *, from_assignment: bool = True) -> None:
"""Directly set the narrowed type of expression (if it supports it).

Expand Down
9 changes: 6 additions & 3 deletionsmypy/checker.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5478,6 +5478,9 @@ def visit_continue_stmt(self, s: ContinueStmt) -> None:

def visit_match_stmt(self, s: MatchStmt) -> None:
named_subject = self._make_named_statement_for_match(s)
# In sync with similar actions elsewhere, narrow the target if
# we are matching an AssignmentExpr
unwrapped_subject = collapse_walrus(s.subject)
with self.binder.frame_context(can_skip=False, fall_through=0):
subject_type = get_proper_type(self.expr_checker.accept(s.subject))

Expand DownExpand Up@@ -5513,9 +5516,9 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
# Maybe the subject type can be inferred from constraints on
# its attribute/item?
if pattern_map and named_subject in pattern_map:
pattern_map[s.subject] = pattern_map[named_subject]
pattern_map[unwrapped_subject] = pattern_map[named_subject]
if else_map and named_subject in else_map:
else_map[s.subject] = else_map[named_subject]
else_map[unwrapped_subject] = else_map[named_subject]
pattern_map = self.propagate_up_typemap_info(pattern_map)
else_map = self.propagate_up_typemap_info(else_map)
self.remove_capture_conflicts(pattern_type.captures, inferred_types)
Expand DownExpand Up@@ -5571,7 +5574,7 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
def _make_named_statement_for_match(self, s: MatchStmt) -> Expression:
"""Construct a fake NameExpr for inference if a match clause is complex."""
subject = s.subject
ifisinstance(subject, (NameExpr, AssignmentExpr)):
ifself.binder.can_put_directly(subject):
# Already named - we should infer type of it as given
return subject
elif s.subject_dummy is not None:
Expand Down
2 changes: 2 additions & 0 deletionstest-data/unit/check-python310.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2179,9 +2179,11 @@ def f() -> None:
match x := returns_a_or_none():
case A():
reveal_type(x.a) # N: Revealed type is "builtins.int"
reveal_type(x) # N: Revealed type is "Union[__main__.A, None]"
match x := returns_a():
case A():
reveal_type(x.a) # N: Revealed type is "builtins.int"
reveal_type(x) # N: Revealed type is "__main__.A"
y = returns_a_or_none()
match y:
case A():
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp