Skip to content
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

Fix some pattern matching bugs #766

Merged
merged 5 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Recognize exhaustive pattern matching (#766)
- Narrow the types of variables assigned within complex patterns (#766)
- New error code `generator_return` is raised when a generator does not
return an iterable type, or an async generator does not return an async
iterable type (#756)
Expand Down
18 changes: 14 additions & 4 deletions pyanalyze/name_check_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5649,10 +5649,20 @@ def visit_Match(self, node: ast.Match) -> None:

self.yield_checker.reset_yield_checks()

with self.scopes.subscope() as else_scope:
for constraint in constraints_to_apply:
self.add_constraint(node, constraint)
subscopes.append(else_scope)
self.match_subject = self.match_subject._replace(
value=constrain_value(
self.match_subject.value,
AndConstraint.make(constraints_to_apply),
)
)

if self.match_subject.value is NO_RETURN_VALUE:
self._set_name_in_scope(LEAVES_SCOPE, node, NO_RETURN_VALUE)
else:
with self.scopes.subscope() as else_scope:
for constraint in constraints_to_apply:
self.add_constraint(node, constraint)
subscopes.append(else_scope)
self.scopes.combine_subscopes(subscopes)

# Attribute checking
Expand Down
19 changes: 14 additions & 5 deletions pyanalyze/patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ def __call__(self, value: Value, positive: bool) -> Optional[Value]:
return value


@dataclass
class AlwaysMatching:
def __call__(self, value: Value, positive: bool) -> Optional[Value]:
if positive:
return value
else:
return None


@dataclass
class PatmaVisitor(ast.NodeVisitor):
visitor: "pyanalyze.name_check_visitor.NameCheckVisitor"
Expand Down Expand Up @@ -379,12 +388,14 @@ def visit_MatchStar(self, node: MatchStar) -> AbstractConstraint:
self.visitor._set_name_in_scope(
node.name, node, self.visitor.match_subject.value
)
return NULL_CONSTRAINT
return self.make_constraint(ConstraintType.predicate, AlwaysMatching())

def visit_MatchAs(self, node: MatchAs) -> AbstractConstraint:
val = self.visitor.match_subject.value
if node.pattern is None:
constraint = NULL_CONSTRAINT
constraint = self.make_constraint(
ConstraintType.predicate, AlwaysMatching()
)
else:
constraint = self.visit(node.pattern)

Expand All @@ -405,12 +416,10 @@ def visit_MatchOr(self, node: MatchOr) -> AbstractConstraint:
return OrConstraint.make(constraints)

def generic_visit(self, node: ast.AST) -> AbstractConstraint:
return NULL_CONSTRAINT
raise NotImplementedError(f"Unsupported pattern node: {node}")

def make_constraint(self, typ: ConstraintType, value: object) -> AbstractConstraint:
varname = self.visitor.match_subject.varname
if varname is None:
return NULL_CONSTRAINT
return Constraint(varname, typ, True, value)

def check_impossible_pattern(self, node: ast.AST, value: Value) -> None:
Expand Down
8 changes: 6 additions & 2 deletions pyanalyze/stacked_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def f(x: Optional[int]) -> None:

"""

varname: VarnameWithOrigin
varname: Optional[VarnameWithOrigin]
"""The :term:`varname` that the constraint applies to."""
constraint_type: ConstraintType
"""Type of constraint. Determines the meaning of :attr:`value`."""
Expand All @@ -313,7 +313,9 @@ def f(x: Optional[int]) -> None:
)

def __post_init__(self) -> None:
assert isinstance(self.varname, VarnameWithOrigin), self.varname
assert self.varname is None or isinstance(
self.varname, VarnameWithOrigin
), self.varname

def apply(self) -> Iterable["Constraint"]:
yield self
Expand Down Expand Up @@ -1069,6 +1071,8 @@ def add_constraint(
def _add_single_constraint(
self, constraint: Constraint, node: Node, state: VisitorState
) -> None:
if constraint.varname is None:
return
for parent_varname, constraint_origin in constraint.varname.get_all_varnames():
current_origin = self.get_origin(parent_varname, node, state)
current_set = self._resolve_origin(current_origin)
Expand Down
44 changes: 44 additions & 0 deletions pyanalyze/test_patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,47 @@ def capybara(p: Planet):
assert_is_value(p, KnownValue(Planet.earth))
"""
)

@skip_before((3, 10))
def test_exhaustive(self):
self.assert_passes(
"""
def f(x: object) -> int:
match x:
case _:
return 1

def g(x: object) -> int: # E: missing_return
match x:
case _ if x == 2:
return 1

def some_func() -> object:
return 1

def h() -> int:
match some_func():
case _:
return 1

def i(x: bool) -> int:
match x:
case True:
return 1
case False:
return 2
"""
)

@skip_before((3, 10))
def test_reassign_in_tuple(self):
self.assert_passes(
"""
from typing_extensions import assert_type

def f(x: int | str) -> None:
match (x,):
case (int() as x,):
assert_type(x, int)
"""
)
Loading