Skip to content

Commit

Permalink
Infer returns from match cases (#2042)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored Mar 3, 2023
1 parent 47430fd commit 925910a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Release date: TBA

Refs PyCQA/pylint#7306

* ``astroid`` now infers return values from match cases.

Refs PyCQA/pylint#5288

* ``Astroid`` now retrieves the default values of keyword only arguments and sets them on
``Arguments.kw_defaults``.

Expand Down
3 changes: 2 additions & 1 deletion astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4982,7 +4982,7 @@ def _infer(
# Pattern matching #######################################################


class Match(_base_nodes.Statement):
class Match(_base_nodes.Statement, _base_nodes.MultiLineBlockNode):
"""Class representing a :class:`ast.Match` node.
>>> import astroid
Expand All @@ -4998,6 +4998,7 @@ class Match(_base_nodes.Statement):
"""

_astroid_fields = ("subject", "cases")
_multi_line_block_fields = ("cases",)

def __init__(
self,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1947,3 +1947,22 @@ def test_match_class():
case1.pattern.cls,
*case1.pattern.kwd_patterns,
]

@staticmethod
def test_return_from_match():
code = textwrap.dedent(
"""
def return_from_match(x):
match x:
case 10:
return 10
case _:
return -1
return_from_match(10) #@
"""
).strip()
node = builder.extract_node(code)
inferred = node.inferred()
assert len(inferred) == 2
assert [inf.value for inf in inferred] == [10, -1]

0 comments on commit 925910a

Please sign in to comment.