Skip to content

Add conformance tests for the 'match_args' parameter to dataclass. #2056

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions conformance/results/mypy/dataclasses_match_args.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
conformant = "Pass"
conformance_automated = "Pass"
errors_diff = """
"""
output = """
dataclasses_match_args.py:42: error: "type[DC4]" has no attribute "__match_args__" [attr-defined]
"""
2 changes: 1 addition & 1 deletion conformance/results/mypy/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "mypy 1.17.0"
test_duration = 2.7
test_duration = 2.8
12 changes: 12 additions & 0 deletions conformance/results/pyre/dataclasses_match_args.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
conformant = "Partial"
notes = """
Adds kw-only fields to __match_args__.
"""
conformance_automated = "Fail"
errors_diff = """
Line 18: Unexpected errors ["dataclasses_match_args.py:18:0 Assert type [70]: Expected `Tuple[typing_extensions.Literal['x']]` but got `Tuple[typing_extensions.Literal['x'], typing_extensions.Literal['*'], typing_extensions.Literal['y']]`."]
"""
output = """
dataclasses_match_args.py:18:0 Assert type [70]: Expected `Tuple[typing_extensions.Literal['x']]` but got `Tuple[typing_extensions.Literal['x'], typing_extensions.Literal['*'], typing_extensions.Literal['y']]`.
dataclasses_match_args.py:42:0 Undefined attribute [16]: `DC4` has no attribute `__match_args__`.
"""
10 changes: 10 additions & 0 deletions conformance/results/pyright/dataclasses_match_args.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
conformant = "Partial"
notes = """
Generates __match_args__ when match_args=False.
"""
conformance_automated = "Fail"
errors_diff = """
Line 42: Expected 1 errors
"""
output = """
"""
2 changes: 1 addition & 1 deletion conformance/results/pyright/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyright 1.1.403"
test_duration = 5.6
test_duration = 5.7
9 changes: 7 additions & 2 deletions conformance/results/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ <h3>Python Type System Conformance Test Results</h3>
<div class="table_container"><table><tbody>
<tr><th class="col1">&nbsp;</th>
<th class='tc-header'><div class='tc-name'>mypy 1.17.0</div>
<div class='tc-time'>2.7sec</div>
<div class='tc-time'>2.8sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyright 1.1.403</div>
<div class='tc-time'>5.6sec</div>
<div class='tc-time'>5.7sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyre 0.9.25</div>
<div class='tc-time'>3.9sec</div>
Expand Down Expand Up @@ -652,6 +652,11 @@ <h3>Python Type System Conformance Test Results</h3>
<th class="column col2 conformant">Pass</th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Rejects legitimate use of dataclass field with `kw_only=True`.</p></span></div></th>
</tr>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataclasses_match_args</th>
<th class="column col2 conformant">Pass</th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Generates __match_args__ when match_args=False.</p></span></div></th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Adds kw-only fields to __match_args__.</p></span></div></th>
</tr>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataclasses_order</th>
<th class="column col2 conformant">Pass</th>
<th class="column col2 conformant">Pass</th>
Expand Down
49 changes: 49 additions & 0 deletions conformance/tests/dataclasses_match_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Tests the match_args feature of dataclass added in Python 3.10.
"""

# Specification: https://docs.python.org/3/library/dataclasses.html#module-contents

from dataclasses import dataclass, KW_ONLY
from typing import assert_type, Literal

# If true, the __match_args__ tuple will be created from the list of non keyword-only parameters to the generated __init__() method

@dataclass(match_args=True)
class DC1:
x: int
_: KW_ONLY
y: str

assert_type(DC1.__match_args__, tuple[Literal['x']])

# The match_args default is True

@dataclass
class DC2:
x: int

assert_type(DC2.__match_args__, tuple[Literal['x']])

# __match_args__ is created even if __init__() is not generated

@dataclass(match_args=True, init=False)
class DC3:
x: int = 0

assert_type(DC3.__match_args__, tuple[Literal['x']])

# If false, or if __match_args__ is already defined in the class, then __match_args__ will not be generated.

@dataclass(match_args=False)
class DC4:
x: int

DC4.__match_args__ # E

@dataclass(match_args=True)
class DC5:
__match_args__ = ()
x: int

assert_type(DC5.__match_args__, tuple[()])