Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add __match_args__ to support match case destructuring in Python 3.10 (…
…#815) * Add support to generate __match_args__ for Python 3.10. * Add versionadded directive. * Update stubs. * Update changelog and add a test to typing examples. * Fix error regarding new-style classes in Python 2. * Fix lint error regarding line length. * Fix lint error regarding trailing whitespace. * Add docstrings for interrogate. * Use _has_own_attribute instead of cls.__dict__ contains check. * Update docs as per review comments. * Revert mistaken changelog update. * Add Python 3.10 pattern matching syntax test cases. * Update define signature with match_args. * Fix conftest formatting. * Fix isort formatting. * Bump to Python 3.10 to parse syntax. * Bump basepython of lint to Python 3.10 for parsing. * Move lint to py310 Co-authored-by: Hynek Schlawack <hs@ox.cx>
- Loading branch information
Showing
12 changed files
with
265 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
``__match_args__`` are now generated to support Python 3.10's | ||
`Structural Pattern Matching <https://docs.python.org/3.10/whatsnew/3.10.html#pep-634-structural-pattern-matching>`_. | ||
This can be controlled by ``match_args`` argument to the class decorators. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# flake8: noqa | ||
# Python 3.10 issue in flake8 : https://github.com/PyCQA/pyflakes/issues/634 | ||
import pytest | ||
|
||
import attr | ||
|
||
from attr._make import make_class | ||
|
||
|
||
class TestPatternMatching(object): | ||
""" | ||
Pattern matching syntax test cases. | ||
""" | ||
|
||
def test_simple_match_case(self): | ||
""" | ||
Simple match case statement | ||
""" | ||
|
||
@attr.s() | ||
class C(object): | ||
a = attr.ib() | ||
|
||
assert C.__match_args__ == ("a",) | ||
|
||
matched = False | ||
c = C(a=1) | ||
match c: | ||
case C(a): | ||
matched = True | ||
|
||
assert matched | ||
|
||
def test_explicit_match_args(self): | ||
""" | ||
Manually set empty __match_args__ will not match. | ||
""" | ||
|
||
ma = () | ||
|
||
@attr.s() | ||
class C(object): | ||
a = attr.ib() | ||
__match_args__ = ma | ||
|
||
c = C(a=1) | ||
|
||
msg = r"C\(\) accepts 0 positional sub-patterns \(1 given\)" | ||
with pytest.raises(TypeError, match=msg): | ||
match c: | ||
case C(a): | ||
pass | ||
|
||
def test_match_args_kw_only(self): | ||
""" | ||
kw_only being set doesn't generate __match_args__ | ||
kw_only field is not included in __match_args__ | ||
""" | ||
|
||
@attr.s() | ||
class C(object): | ||
a = attr.ib(kw_only=True) | ||
b = attr.ib() | ||
|
||
assert C.__match_args__ == ("b",) | ||
|
||
c = C(a=1, b=1) | ||
msg = r"C\(\) accepts 1 positional sub-pattern \(2 given\)" | ||
with pytest.raises(TypeError, match=msg): | ||
match c: | ||
case C(a, b): | ||
pass | ||
|
||
found = False | ||
match c: | ||
case C(b, a=a): | ||
found = True | ||
|
||
assert found | ||
|
||
@attr.s(match_args=True, kw_only=True) | ||
class C(object): | ||
a = attr.ib() | ||
b = attr.ib() | ||
|
||
c = C(a=1, b=1) | ||
msg = r"C\(\) accepts 0 positional sub-patterns \(2 given\)" | ||
with pytest.raises(TypeError, match=msg): | ||
match c: | ||
case C(a, b): | ||
pass | ||
|
||
found = False | ||
match c: | ||
case C(a=a, b=b): | ||
found = True | ||
|
||
assert found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.