← Back to index

dataclasses_kwonly.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 3
FP: 0
FN: 0
Optional: 0 / 0
1"""
2Tests the keyword-only feature of dataclass added in Python 3.10.
3"""
4
5# Specification: https://docs.python.org/3/library/dataclasses.html#module-contents
6
7from dataclasses import dataclass, KW_ONLY, field
8
9
10@dataclass
11class DC1:
12 a: str
13 _: KW_ONLY
14 b: int = 0
17DC1("hi")
18DC1(a="hi")
19DC1(a="hi", b=1)
20DC1("hi", b=1)
22# This should generate an error because "b" is keyword-only.
23DC1("hi", 1) # E
[too-many-positional-arguments] Too many positional arguments: expected 1, got 2
26@dataclass
27class DC2:
28 b: int = field(kw_only=True, default=3)
29 a: str
32DC2("hi")
33DC2(a="hi")
34DC2(a="hi", b=1)
35DC2("hi", b=1)
37# This should generate an error because "b" is keyword-only.
38DC2("hi", 1) # E
[too-many-positional-arguments] Too many positional arguments: expected 1, got 2
41@dataclass(kw_only=True)
42class DC3:
43 a: str = field(kw_only=False)
44 b: int = 0
47DC3("hi")
48DC3(a="hi")
49DC3(a="hi", b=1)
50DC3("hi", b=1)
52# This should generate an error because "b" is keyword-only.
53DC3("hi", 1) # E
[too-many-positional-arguments] Too many positional arguments: expected 1, got 2
56@dataclass
57class DC4(DC3):
58 c: float
61DC4("", 0.2, b=3)
62DC4(a="", b=3, c=0.2)