← Back to index
dataclasses_match_args.py
True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 1
FP: 1
FN: 0
Optional: 0 / 0
1
"""
2
Tests
the
match_args
feature
of
dataclass
added
in
Python
3.10
.
3
"""
4
5
# Specification: https://docs.python.org/3/library/dataclasses.html#module-contents
6
7
from
dataclasses
import
dataclass
,
KW_ONLY
8
from
typing
import
assert_type
,
Literal
9
10
# If true, the __match_args__ tuple will be created from the list of non keyword-only parameters to the generated __init__() method
11
12
@dataclass
(
match_args
=
True
)
13
class
DC1
:
14
x
:
int
15
_
:
KW_ONLY
16
y
:
str
17
18
assert_type
(
DC1
.
__match_args__
,
tuple
[
Literal
[
'x'
]])
19
20
# The match_args default is True
21
22
@dataclass
23
class
DC2
:
24
x
:
int
25
26
assert_type
(
DC2
.
__match_args__
,
tuple
[
Literal
[
'x'
]])
27
28
# __match_args__ is created even if __init__() is not generated
29
30
@dataclass
(
match_args
=
True
,
init
=
False
)
31
class
DC3
:
32
x
:
int
=
0
33
34
assert_type
(
DC3
.
__match_args__
,
tuple
[
Literal
[
'x'
]])
35
36
# If false, or if __match_args__ is already defined in the class, then __match_args__ will not be generated.
37
38
@dataclass
(
match_args
=
False
)
39
class
DC4
:
40
x
:
int
41
42
DC4
.
__match_args__
# E
[unresolved-attribute] Class `DC4` has no attribute `__match_args__`
43
44
@dataclass
(
match_args
=
True
)
45
class
DC5
:
46
__match_args__
=
()
47
x
:
int
48
49
assert_type
(
DC5
.
__match_args__
,
tuple
[()])
Unexpected error [type-assertion-failure] Type `tuple[()]` does not match asserted type `Unknown | tuple[()]`