| 1 |
[case testDataclassesBasic] |
| 2 |
from dataclasses import dataclass |
| 3 |
|
| 4 |
@dataclass |
| 5 |
class Person: |
| 6 |
name: str |
| 7 |
age: int |
| 8 |
|
| 9 |
def summary(self): |
| 10 |
return "%s is %d years old." % (self.name, self.age) |
| 11 |
|
| 12 |
reveal_type(Person) # N: Revealed type is "def (name: builtins.str, age: builtins.int) -> __main__.Person" |
| 13 |
Person('John', 32) |
| 14 |
Person('Jonh', 21, None) # E: Too many arguments for "Person" |
| 15 |
|
| 16 |
[builtins fixtures/dataclasses.pyi] |
| 17 |
[typing fixtures/typing-medium.pyi] |
| 18 |
|
| 19 |
[case testDataclassesCustomInit] |
| 20 |
from dataclasses import dataclass |
| 21 |
|
| 22 |
@dataclass |
| 23 |
class A: |
| 24 |
a: int |
| 25 |
|
| 26 |
def __init__(self, a: str) -> None: |
| 27 |
pass |
| 28 |
|
| 29 |
A('1') |
| 30 |
|
| 31 |
[builtins fixtures/dataclasses.pyi] |
| 32 |
|
| 33 |
[case testDataclassesBasicInheritance] |
| 34 |
from dataclasses import dataclass |
| 35 |
|
| 36 |
@dataclass |
| 37 |
class Mammal: |
| 38 |
age: int |
| 39 |
|
| 40 |
@dataclass |
| 41 |
class Person(Mammal): |
| 42 |
name: str |
| 43 |
|
| 44 |
def summary(self): |
| 45 |
return "%s is %d years old." % (self.name, self.age) |
| 46 |
|
| 47 |
reveal_type(Person) # N: Revealed type is "def (age: builtins.int, name: builtins.str) -> __main__.Person" |
| 48 |
Mammal(10) |
| 49 |
Person(32, 'John') |
| 50 |
Person(21, 'Jonh', None) # E: Too many arguments for "Person" |
| 51 |
|
| 52 |
[builtins fixtures/dataclasses.pyi] |
| 53 |
[typing fixtures/typing-medium.pyi] |
| 54 |
|
| 55 |
[case testDataclassesDeepInheritance] |
| 56 |
from dataclasses import dataclass |
| 57 |
|
| 58 |
@dataclass |
| 59 |
class A: |
| 60 |
a: int |
| 61 |
|
| 62 |
@dataclass |
| 63 |
class B(A): |
| 64 |
b: int |
| 65 |
|
| 66 |
@dataclass |
| 67 |
class C(B): |
| 68 |
c: int |
| 69 |
|
| 70 |
@dataclass |
| 71 |
class D(C): |
| 72 |
d: int |
| 73 |
|
| 74 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int) -> __main__.A" |
| 75 |
reveal_type(B) # N: Revealed type is "def (a: builtins.int, b: builtins.int) -> __main__.B" |
| 76 |
reveal_type(C) # N: Revealed type is "def (a: builtins.int, b: builtins.int, c: builtins.int) -> __main__.C" |
| 77 |
reveal_type(D) # N: Revealed type is "def (a: builtins.int, b: builtins.int, c: builtins.int, d: builtins.int) -> __main__.D" |
| 78 |
|
| 79 |
[builtins fixtures/dataclasses.pyi] |
| 80 |
|
| 81 |
[case testDataclassesMultipleInheritance] |
| 82 |
from dataclasses import dataclass, field, InitVar |
| 83 |
@dataclass |
| 84 |
class A: |
| 85 |
a: bool |
| 86 |
|
| 87 |
@dataclass |
| 88 |
class B: |
| 89 |
b: InitVar[bool] |
| 90 |
_b: bool = field(init=False) |
| 91 |
|
| 92 |
def __post_init__(self, b: bool): |
| 93 |
self._b = b |
| 94 |
|
| 95 |
@dataclass |
| 96 |
class C(A, B): |
| 97 |
pass |
| 98 |
|
| 99 |
reveal_type(C) # N: Revealed type is "def (b: builtins.bool, a: builtins.bool) -> __main__.C" |
| 100 |
|
| 101 |
[builtins fixtures/dataclasses.pyi] |
| 102 |
|
| 103 |
[case testDataclassesDeepInitVarInheritance] |
| 104 |
from dataclasses import dataclass, field, InitVar |
| 105 |
@dataclass |
| 106 |
class A: |
| 107 |
a: bool |
| 108 |
|
| 109 |
@dataclass |
| 110 |
class B: |
| 111 |
b: InitVar[bool] |
| 112 |
_b: bool = field(init=False) |
| 113 |
|
| 114 |
def __post_init__(self, b: bool): |
| 115 |
self._b = b |
| 116 |
|
| 117 |
@dataclass(init=False) |
| 118 |
class C(B): |
| 119 |
def __init__(self): |
| 120 |
super().__init__(True) |
| 121 |
|
| 122 |
@dataclass |
| 123 |
class D(C): |
| 124 |
pass |
| 125 |
|
| 126 |
reveal_type(C) # N: Revealed type is "def () -> __main__.C" |
| 127 |
reveal_type(D) # N: Revealed type is "def (b: builtins.bool) -> __main__.D" |
| 128 |
|
| 129 |
[builtins fixtures/dataclasses.pyi] |
| 130 |
|
| 131 |
[case testDataclassesOverriding] |
| 132 |
from dataclasses import dataclass |
| 133 |
|
| 134 |
@dataclass |
| 135 |
class Mammal: |
| 136 |
age: int |
| 137 |
|
| 138 |
@dataclass |
| 139 |
class Person(Mammal): |
| 140 |
name: str |
| 141 |
age: int |
| 142 |
|
| 143 |
@dataclass |
| 144 |
class SpecialPerson(Person): |
| 145 |
special_factor: float |
| 146 |
|
| 147 |
@dataclass |
| 148 |
class ExtraSpecialPerson(SpecialPerson): |
| 149 |
age: int |
| 150 |
special_factor: float |
| 151 |
name: str |
| 152 |
|
| 153 |
reveal_type(Person) # N: Revealed type is "def (age: builtins.int, name: builtins.str) -> __main__.Person" |
| 154 |
reveal_type(SpecialPerson) # N: Revealed type is "def (age: builtins.int, name: builtins.str, special_factor: builtins.float) -> __main__.SpecialPerson" |
| 155 |
reveal_type(ExtraSpecialPerson) # N: Revealed type is "def (age: builtins.int, name: builtins.str, special_factor: builtins.float) -> __main__.ExtraSpecialPerson" |
| 156 |
Person(32, 'John') |
| 157 |
Person(21, 'John', None) # E: Too many arguments for "Person" |
| 158 |
SpecialPerson(21, 'John', 0.5) |
| 159 |
ExtraSpecialPerson(21, 'John', 0.5) |
| 160 |
|
| 161 |
[builtins fixtures/dataclasses.pyi] |
| 162 |
|
| 163 |
[case testDataclassesOverridingWithDefaults] |
| 164 |
# Issue #5681 https://github.com/python/mypy/issues/5681 |
| 165 |
from dataclasses import dataclass |
| 166 |
from typing import Any |
| 167 |
|
| 168 |
@dataclass |
| 169 |
class Base: |
| 170 |
some_int: Any |
| 171 |
some_str: str = 'foo' |
| 172 |
|
| 173 |
|
| 174 |
@dataclass |
| 175 |
class C(Base): |
| 176 |
some_int: int |
| 177 |
|
| 178 |
reveal_type(C) # N: Revealed type is "def (some_int: builtins.int, some_str: builtins.str =) -> __main__.C" |
| 179 |
|
| 180 |
[builtins fixtures/dataclasses.pyi] |
| 181 |
|
| 182 |
[case testDataclassIncompatibleOverrides] |
| 183 |
from dataclasses import dataclass |
| 184 |
|
| 185 |
@dataclass |
| 186 |
class Base: |
| 187 |
foo: int |
| 188 |
|
| 189 |
@dataclass |
| 190 |
class BadDerived1(Base): |
| 191 |
def foo(self) -> int: # E: Dataclass attribute may only be overridden by another attribute \ |
| 192 |
# E: Signature of "foo" incompatible with supertype "Base" \ |
| 193 |
# N: Superclass: \ |
| 194 |
# N: int \ |
| 195 |
# N: Subclass: \ |
| 196 |
# N: def foo(self) -> int |
| 197 |
return 1 |
| 198 |
|
| 199 |
@dataclass |
| 200 |
class BadDerived2(Base): |
| 201 |
@property # E: Dataclass attribute may only be overridden by another attribute |
| 202 |
def foo(self) -> int: # E: Cannot override writeable attribute with read-only property |
| 203 |
return 2 |
| 204 |
|
| 205 |
@dataclass |
| 206 |
class BadDerived3(Base): |
| 207 |
class foo: pass # E: Dataclass attribute may only be overridden by another attribute |
| 208 |
[builtins fixtures/dataclasses.pyi] |
| 209 |
|
| 210 |
[case testDataclassMultipleInheritance] |
| 211 |
from dataclasses import dataclass |
| 212 |
|
| 213 |
class Unrelated: |
| 214 |
foo: str |
| 215 |
|
| 216 |
@dataclass |
| 217 |
class Base: |
| 218 |
bar: int |
| 219 |
|
| 220 |
@dataclass |
| 221 |
class Derived(Base, Unrelated): |
| 222 |
pass |
| 223 |
|
| 224 |
d = Derived(3) |
| 225 |
reveal_type(d.foo) # N: Revealed type is "builtins.str" |
| 226 |
reveal_type(d.bar) # N: Revealed type is "builtins.int" |
| 227 |
[builtins fixtures/dataclasses.pyi] |
| 228 |
|
| 229 |
[case testDataclassIncompatibleFrozenOverride] |
| 230 |
from dataclasses import dataclass |
| 231 |
|
| 232 |
@dataclass(frozen=True) |
| 233 |
class Base: |
| 234 |
foo: int |
| 235 |
|
| 236 |
@dataclass(frozen=True) |
| 237 |
class BadDerived(Base): |
| 238 |
@property # E: Dataclass attribute may only be overridden by another attribute |
| 239 |
def foo(self) -> int: |
| 240 |
return 3 |
| 241 |
[builtins fixtures/dataclasses.pyi] |
| 242 |
|
| 243 |
[case testDataclassesFreezing] |
| 244 |
from dataclasses import dataclass |
| 245 |
|
| 246 |
@dataclass(frozen=True) |
| 247 |
class Person: |
| 248 |
name: str |
| 249 |
|
| 250 |
john = Person('John') |
| 251 |
john.name = 'Ben' # E: Property "name" defined in "Person" is read-only |
| 252 |
|
| 253 |
[builtins fixtures/dataclasses.pyi] |
| 254 |
|
| 255 |
[case testDataclassesInconsistentFreezing] |
| 256 |
from dataclasses import dataclass |
| 257 |
|
| 258 |
@dataclass(frozen=True) |
| 259 |
class FrozenBase: |
| 260 |
pass |
| 261 |
|
| 262 |
@dataclass |
| 263 |
class BadNormalDerived(FrozenBase): # E: Non-frozen dataclass cannot inherit from a frozen dataclass |
| 264 |
pass |
| 265 |
|
| 266 |
@dataclass |
| 267 |
class NormalBase: |
| 268 |
pass |
| 269 |
|
| 270 |
@dataclass(frozen=True) |
| 271 |
class BadFrozenDerived(NormalBase): # E: Frozen dataclass cannot inherit from a non-frozen dataclass |
| 272 |
pass |
| 273 |
|
| 274 |
[builtins fixtures/dataclasses.pyi] |
| 275 |
|
| 276 |
[case testDataclassesFields] |
| 277 |
from dataclasses import dataclass, field |
| 278 |
|
| 279 |
@dataclass |
| 280 |
class Person: |
| 281 |
name: str |
| 282 |
age: int = field(default=0, init=False) |
| 283 |
|
| 284 |
reveal_type(Person) # N: Revealed type is "def (name: builtins.str) -> __main__.Person" |
| 285 |
john = Person('John') |
| 286 |
john.age = 'invalid' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 287 |
john.age = 24 |
| 288 |
|
| 289 |
[builtins fixtures/dataclasses.pyi] |
| 290 |
|
| 291 |
[case testDataclassesBadInit] |
| 292 |
from dataclasses import dataclass, field |
| 293 |
|
| 294 |
@dataclass |
| 295 |
class Person: |
| 296 |
name: str |
| 297 |
age: int = field(init=None) # E: No overload variant of "field" matches argument type "None" \ |
| 298 |
# N: Possible overload variants: \ |
| 299 |
# N: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T \ |
| 300 |
# N: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T \ |
| 301 |
# N: def field(*, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> Any |
| 302 |
|
| 303 |
[builtins fixtures/dataclasses.pyi] |
| 304 |
|
| 305 |
[case testDataclassesMultiInit] |
| 306 |
from dataclasses import dataclass, field |
| 307 |
from typing import List |
| 308 |
|
| 309 |
@dataclass |
| 310 |
class Person: |
| 311 |
name: str |
| 312 |
age: int = field(init=False) |
| 313 |
friend_names: List[str] = field(init=True) |
| 314 |
enemy_names: List[str] |
| 315 |
|
| 316 |
reveal_type(Person) # N: Revealed type is "def (name: builtins.str, friend_names: builtins.list[builtins.str], enemy_names: builtins.list[builtins.str]) -> __main__.Person" |
| 317 |
|
| 318 |
[builtins fixtures/dataclasses.pyi] |
| 319 |
|
| 320 |
[case testDataclassesMultiInitDefaults] |
| 321 |
from dataclasses import dataclass, field |
| 322 |
from typing import List, Optional |
| 323 |
|
| 324 |
@dataclass |
| 325 |
class Person: |
| 326 |
name: str |
| 327 |
age: int = field(init=False) |
| 328 |
friend_names: List[str] = field(init=True) |
| 329 |
enemy_names: List[str] |
| 330 |
nickname: Optional[str] = None |
| 331 |
|
| 332 |
reveal_type(Person) # N: Revealed type is "def (name: builtins.str, friend_names: builtins.list[builtins.str], enemy_names: builtins.list[builtins.str], nickname: builtins.str | None =) -> __main__.Person" |
| 333 |
|
| 334 |
[builtins fixtures/dataclasses.pyi] |
| 335 |
|
| 336 |
[case testDataclassesDefaults] |
| 337 |
from dataclasses import dataclass |
| 338 |
|
| 339 |
@dataclass |
| 340 |
class Application: |
| 341 |
name: str = 'Unnamed' |
| 342 |
rating: int = 0 |
| 343 |
|
| 344 |
reveal_type(Application) # N: Revealed type is "def (name: builtins.str =, rating: builtins.int =) -> __main__.Application" |
| 345 |
app = Application() |
| 346 |
|
| 347 |
[builtins fixtures/dataclasses.pyi] |
| 348 |
|
| 349 |
[case testDataclassesDefaultFactories] |
| 350 |
from dataclasses import dataclass, field |
| 351 |
|
| 352 |
@dataclass |
| 353 |
class Application: |
| 354 |
name: str = 'Unnamed' |
| 355 |
rating: int = field(default_factory=int) |
| 356 |
rating_count: int = field() # E: Attributes without a default cannot follow attributes with one |
| 357 |
|
| 358 |
[builtins fixtures/dataclasses.pyi] |
| 359 |
|
| 360 |
[case testDataclassesDefaultFactoryTypeChecking] |
| 361 |
from dataclasses import dataclass, field |
| 362 |
|
| 363 |
@dataclass |
| 364 |
class Application: |
| 365 |
name: str = 'Unnamed' |
| 366 |
rating: int = field(default_factory=str) # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 367 |
|
| 368 |
[builtins fixtures/dataclasses.pyi] |
| 369 |
|
| 370 |
[case testDataclassesDefaultOrdering] |
| 371 |
from dataclasses import dataclass |
| 372 |
|
| 373 |
@dataclass |
| 374 |
class Application: |
| 375 |
name: str = 'Unnamed' |
| 376 |
rating: int # E: Attributes without a default cannot follow attributes with one |
| 377 |
|
| 378 |
[builtins fixtures/dataclasses.pyi] |
| 379 |
|
| 380 |
[case testDataclassesOrderingKwOnly] |
| 381 |
from dataclasses import dataclass |
| 382 |
|
| 383 |
@dataclass(kw_only=True) |
| 384 |
class Application: |
| 385 |
name: str = 'Unnamed' |
| 386 |
rating: int |
| 387 |
|
| 388 |
Application(rating=5) |
| 389 |
Application(name='name', rating=5) |
| 390 |
Application() # E: Missing named argument "rating" for "Application" |
| 391 |
Application('name') # E: Too many positional arguments for "Application" # E: Missing named argument "rating" for "Application" |
| 392 |
Application('name', 123) # E: Too many positional arguments for "Application" |
| 393 |
Application('name', rating=123) # E: Too many positional arguments for "Application" |
| 394 |
Application(name=123, rating='name') # E: Argument "name" to "Application" has incompatible type "int"; expected "str" # E: Argument "rating" to "Application" has incompatible type "str"; expected "int" |
| 395 |
Application(rating='name', name=123) # E: Argument "rating" to "Application" has incompatible type "str"; expected "int" # E: Argument "name" to "Application" has incompatible type "int"; expected "str" |
| 396 |
|
| 397 |
[builtins fixtures/dataclasses.pyi] |
| 398 |
|
| 399 |
[case testDataclassesOrderingKwOnlyOnField] |
| 400 |
from dataclasses import dataclass, field |
| 401 |
|
| 402 |
@dataclass |
| 403 |
class Application: |
| 404 |
name: str = 'Unnamed' |
| 405 |
rating: int = field(kw_only=True) |
| 406 |
|
| 407 |
Application(rating=5) |
| 408 |
Application('name', rating=123) |
| 409 |
Application(name='name', rating=5) |
| 410 |
Application() # E: Missing named argument "rating" for "Application" |
| 411 |
Application('name') # E: Missing named argument "rating" for "Application" |
| 412 |
Application('name', 123) # E: Too many positional arguments for "Application" |
| 413 |
Application(123, rating='name') # E: Argument 1 to "Application" has incompatible type "int"; expected "str" # E: Argument "rating" to "Application" has incompatible type "str"; expected "int" |
| 414 |
|
| 415 |
[builtins fixtures/dataclasses.pyi] |
| 416 |
|
| 417 |
[case testDataclassesOrderingKwOnlyOnFieldFalse] |
| 418 |
from dataclasses import dataclass, field |
| 419 |
|
| 420 |
@dataclass |
| 421 |
class Application: |
| 422 |
name: str = 'Unnamed' |
| 423 |
rating: int = field(kw_only=False) # E: Attributes without a default cannot follow attributes with one |
| 424 |
|
| 425 |
Application(name='name', rating=5) |
| 426 |
Application('name', 123) |
| 427 |
Application('name', rating=123) |
| 428 |
Application() # E: Missing positional argument "name" in call to "Application" |
| 429 |
Application('name') # E: Too few arguments for "Application" |
| 430 |
|
| 431 |
[builtins fixtures/dataclasses.pyi] |
| 432 |
|
| 433 |
[case testDataclassesOrderingKwOnlyWithSentinel] |
| 434 |
from dataclasses import dataclass, KW_ONLY |
| 435 |
|
| 436 |
@dataclass |
| 437 |
class Application: |
| 438 |
_: KW_ONLY |
| 439 |
name: str = 'Unnamed' |
| 440 |
rating: int |
| 441 |
|
| 442 |
Application(rating=5) |
| 443 |
Application(name='name', rating=5) |
| 444 |
Application() # E: Missing named argument "rating" for "Application" |
| 445 |
Application('name') # E: Too many positional arguments for "Application" # E: Missing named argument "rating" for "Application" |
| 446 |
Application('name', 123) # E: Too many positional arguments for "Application" |
| 447 |
Application('name', rating=123) # E: Too many positional arguments for "Application" |
| 448 |
|
| 449 |
[builtins fixtures/dataclasses.pyi] |
| 450 |
|
| 451 |
[case testDataclassesOrderingKwOnlyWithSentinelAndFieldOverride] |
| 452 |
from dataclasses import dataclass, field, KW_ONLY |
| 453 |
|
| 454 |
@dataclass |
| 455 |
class Application: |
| 456 |
_: KW_ONLY |
| 457 |
name: str = 'Unnamed' |
| 458 |
rating: int = field(kw_only=False) |
| 459 |
|
| 460 |
Application(name='name', rating=5) |
| 461 |
Application() # E: Missing positional argument "rating" in call to "Application" |
| 462 |
Application(123) |
| 463 |
Application('name') # E: Argument 1 to "Application" has incompatible type "str"; expected "int" |
| 464 |
Application('name', 123) # E: Too many positional arguments for "Application" \ |
| 465 |
# E: Argument 1 to "Application" has incompatible type "str"; expected "int" \ |
| 466 |
# E: Argument 2 to "Application" has incompatible type "int"; expected "str" |
| 467 |
Application(123, rating=123) # E: "Application" gets multiple values for keyword argument "rating" |
| 468 |
[builtins fixtures/dataclasses.pyi] |
| 469 |
|
| 470 |
[case testDataclassesOrderingKwOnlyWithSentinelAndSubclass] |
| 471 |
from dataclasses import dataclass, field, KW_ONLY |
| 472 |
|
| 473 |
@dataclass |
| 474 |
class Base: |
| 475 |
x: str |
| 476 |
_: KW_ONLY |
| 477 |
y: int = 0 |
| 478 |
w: int = 1 |
| 479 |
|
| 480 |
@dataclass |
| 481 |
class D(Base): |
| 482 |
z: str |
| 483 |
a: str = "a" |
| 484 |
|
| 485 |
D("Hello", "World") |
| 486 |
D(x="Hello", z="World") |
| 487 |
D("Hello", "World", y=1, w=2, a="b") |
| 488 |
D("Hello") # E: Missing positional argument "z" in call to "D" |
| 489 |
D() # E: Missing positional arguments "x", "z" in call to "D" |
| 490 |
D(123, "World") # E: Argument 1 to "D" has incompatible type "int"; expected "str" |
| 491 |
D("Hello", False) # E: Argument 2 to "D" has incompatible type "bool"; expected "str" |
| 492 |
D(123, False) # E: Argument 1 to "D" has incompatible type "int"; expected "str" # E: Argument 2 to "D" has incompatible type "bool"; expected "str" |
| 493 |
|
| 494 |
[builtins fixtures/dataclasses.pyi] |
| 495 |
|
| 496 |
[case testDataclassesOrderingKwOnlyWithMultipleSentinel] |
| 497 |
from dataclasses import dataclass, field, KW_ONLY |
| 498 |
|
| 499 |
@dataclass |
| 500 |
class Base: |
| 501 |
x: str |
| 502 |
_: KW_ONLY |
| 503 |
y: int = 0 |
| 504 |
__: KW_ONLY # E: There may not be more than one field with the KW_ONLY type |
| 505 |
w: int = 1 |
| 506 |
|
| 507 |
[builtins fixtures/dataclasses.pyi] |
| 508 |
|
| 509 |
[case testDataclassesClassmethods] |
| 510 |
from dataclasses import dataclass |
| 511 |
|
| 512 |
@dataclass |
| 513 |
class Application: |
| 514 |
name: str |
| 515 |
|
| 516 |
@classmethod |
| 517 |
def parse(cls, request: str) -> "Application": |
| 518 |
return cls(name='...') |
| 519 |
|
| 520 |
app = Application.parse('') |
| 521 |
|
| 522 |
[builtins fixtures/dataclasses.pyi] |
| 523 |
|
| 524 |
[case testDataclassesOverloadsAndClassmethods] |
| 525 |
from dataclasses import dataclass |
| 526 |
from typing import overload, Union |
| 527 |
|
| 528 |
@dataclass |
| 529 |
class A: |
| 530 |
a: int |
| 531 |
b: str |
| 532 |
|
| 533 |
@classmethod |
| 534 |
def other(cls) -> str: |
| 535 |
return "..." |
| 536 |
|
| 537 |
@overload |
| 538 |
@classmethod |
| 539 |
def foo(cls, x: int) -> int: ... |
| 540 |
|
| 541 |
@overload |
| 542 |
@classmethod |
| 543 |
def foo(cls, x: str) -> str: ... |
| 544 |
|
| 545 |
@classmethod |
| 546 |
def foo(cls, x: Union[int, str]) -> Union[int, str]: |
| 547 |
reveal_type(cls) # N: Revealed type is "type[__main__.A]" |
| 548 |
reveal_type(cls.other()) # N: Revealed type is "builtins.str" |
| 549 |
return x |
| 550 |
|
| 551 |
reveal_type(A.foo(3)) # N: Revealed type is "builtins.int" |
| 552 |
reveal_type(A.foo("foo")) # N: Revealed type is "builtins.str" |
| 553 |
|
| 554 |
[builtins fixtures/dataclasses.pyi] |
| 555 |
|
| 556 |
[case testClassmethodShadowingFieldDoesNotCrash] |
| 557 |
from dataclasses import dataclass |
| 558 |
|
| 559 |
# This used to crash -- see #6217 |
| 560 |
@dataclass |
| 561 |
class Foo: |
| 562 |
bar: str |
| 563 |
@classmethod # E: Name "bar" already defined on line 6 |
| 564 |
def bar(cls) -> "Foo": |
| 565 |
return cls('asdf') |
| 566 |
[builtins fixtures/dataclasses.pyi] |
| 567 |
|
| 568 |
[case testDataclassesClassVars] |
| 569 |
from dataclasses import dataclass |
| 570 |
from typing import ClassVar |
| 571 |
|
| 572 |
@dataclass |
| 573 |
class Application: |
| 574 |
name: str |
| 575 |
|
| 576 |
COUNTER: ClassVar[int] = 0 |
| 577 |
|
| 578 |
reveal_type(Application) # N: Revealed type is "def (name: builtins.str) -> __main__.Application" |
| 579 |
application = Application("example") |
| 580 |
application.COUNTER = 1 # E: Cannot assign to class variable "COUNTER" via instance |
| 581 |
Application.COUNTER = 1 |
| 582 |
|
| 583 |
[builtins fixtures/dataclasses.pyi] |
| 584 |
|
| 585 |
[case testTypeAliasInDataclassDoesNotCrash] |
| 586 |
from dataclasses import dataclass |
| 587 |
from typing import Callable |
| 588 |
from typing_extensions import TypeAlias |
| 589 |
|
| 590 |
@dataclass |
| 591 |
class Foo: |
| 592 |
x: int |
| 593 |
|
| 594 |
@dataclass |
| 595 |
class One: |
| 596 |
S: TypeAlias = Foo # E: Type aliases inside dataclass definitions are not supported at runtime |
| 597 |
|
| 598 |
a = One() |
| 599 |
reveal_type(a.S) # N: Revealed type is "def (x: builtins.int) -> __main__.Foo" |
| 600 |
a.S() # E: Missing positional argument "x" in call to "Foo" |
| 601 |
reveal_type(a.S(5)) # N: Revealed type is "__main__.Foo" |
| 602 |
|
| 603 |
@dataclass |
| 604 |
class Two: |
| 605 |
S: TypeAlias = Callable[[int], str] # E: Type aliases inside dataclass definitions are not supported at runtime |
| 606 |
|
| 607 |
c = Two() |
| 608 |
x = c.S |
| 609 |
reveal_type(x) # N: Revealed type is "typing._SpecialForm" |
| 610 |
[builtins fixtures/dataclasses.pyi] |
| 611 |
[typing fixtures/typing-medium.pyi] |
| 612 |
|
| 613 |
[case testDataclassOrdering] |
| 614 |
from dataclasses import dataclass |
| 615 |
|
| 616 |
@dataclass(order=True) |
| 617 |
class Application: |
| 618 |
name: str |
| 619 |
rating: int |
| 620 |
|
| 621 |
app1 = Application('example-1', 5) |
| 622 |
app2 = Application('example-2', 5) |
| 623 |
app1 < app2 |
| 624 |
app1 > app2 |
| 625 |
app1 <= app2 |
| 626 |
app1 >= app2 |
| 627 |
app1 < 5 # E: Unsupported operand types for < ("Application" and "int") |
| 628 |
app1 > 5 # E: Unsupported operand types for > ("Application" and "int") |
| 629 |
app1 <= 5 # E: Unsupported operand types for <= ("Application" and "int") |
| 630 |
app1 >= 5 # E: Unsupported operand types for >= ("Application" and "int") |
| 631 |
|
| 632 |
class SpecializedApplication(Application): |
| 633 |
... |
| 634 |
|
| 635 |
app3 = SpecializedApplication('example-3', 5) |
| 636 |
app1 < app3 |
| 637 |
app1 > app3 |
| 638 |
app1 <= app3 |
| 639 |
app1 >= app3 |
| 640 |
|
| 641 |
[builtins fixtures/dataclasses.pyi] |
| 642 |
|
| 643 |
[case testDataclassOrderingWithoutEquality] |
| 644 |
from dataclasses import dataclass |
| 645 |
|
| 646 |
@dataclass(eq=False, order=True) # E: "eq" must be True if "order" is True |
| 647 |
class Application: |
| 648 |
... |
| 649 |
|
| 650 |
[builtins fixtures/dataclasses.pyi] |
| 651 |
|
| 652 |
[case testDataclassOrderingWithCustomMethods] |
| 653 |
from dataclasses import dataclass |
| 654 |
|
| 655 |
@dataclass(order=True) |
| 656 |
class Application: |
| 657 |
def __lt__(self, other: 'Application') -> bool: # E: You may not have a custom "__lt__" method when "order" is True |
| 658 |
... |
| 659 |
|
| 660 |
[builtins fixtures/dataclasses.pyi] |
| 661 |
|
| 662 |
[case testDataclassDefaultsInheritance] |
| 663 |
from dataclasses import dataclass |
| 664 |
from typing import Optional |
| 665 |
|
| 666 |
@dataclass(order=True) |
| 667 |
class Application: |
| 668 |
id: Optional[int] |
| 669 |
name: str |
| 670 |
|
| 671 |
@dataclass |
| 672 |
class SpecializedApplication(Application): |
| 673 |
rating: int = 0 |
| 674 |
|
| 675 |
reveal_type(SpecializedApplication) # N: Revealed type is "def (id: builtins.int | None, name: builtins.str, rating: builtins.int =) -> __main__.SpecializedApplication" |
| 676 |
|
| 677 |
[builtins fixtures/dataclasses.pyi] |
| 678 |
|
| 679 |
[case testDataclassGenerics] |
| 680 |
from dataclasses import dataclass |
| 681 |
from typing import Generic, List, Optional, TypeVar |
| 682 |
|
| 683 |
T = TypeVar('T') |
| 684 |
|
| 685 |
@dataclass |
| 686 |
class A(Generic[T]): |
| 687 |
x: T |
| 688 |
y: T |
| 689 |
z: List[T] |
| 690 |
|
| 691 |
def foo(self) -> List[T]: |
| 692 |
return [self.x, self.y] |
| 693 |
|
| 694 |
def bar(self) -> T: |
| 695 |
return self.z[0] |
| 696 |
|
| 697 |
def problem(self) -> T: |
| 698 |
return self.z # E: Incompatible return value type (got "list[T]", expected "T") |
| 699 |
|
| 700 |
reveal_type(A) # N: Revealed type is "def [T] (x: T`1, y: T`1, z: builtins.list[T`1]) -> __main__.A[T`1]" |
| 701 |
A(1, 2, ["a", "b"]) # E: Cannot infer value of type parameter "T" of "A" |
| 702 |
a = A(1, 2, [1, 2]) |
| 703 |
reveal_type(a) # N: Revealed type is "__main__.A[builtins.int]" |
| 704 |
reveal_type(a.x) # N: Revealed type is "builtins.int" |
| 705 |
reveal_type(a.y) # N: Revealed type is "builtins.int" |
| 706 |
reveal_type(a.z) # N: Revealed type is "builtins.list[builtins.int]" |
| 707 |
s: str = a.bar() # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 708 |
|
| 709 |
[builtins fixtures/dataclasses.pyi] |
| 710 |
|
| 711 |
[case testDataclassGenericCovariant] |
| 712 |
from dataclasses import dataclass |
| 713 |
from typing import Generic, TypeVar |
| 714 |
|
| 715 |
T_co = TypeVar("T_co", covariant=True) |
| 716 |
|
| 717 |
@dataclass |
| 718 |
class MyDataclass(Generic[T_co]): |
| 719 |
a: T_co |
| 720 |
|
| 721 |
[builtins fixtures/dataclasses.pyi] |
| 722 |
|
| 723 |
[case testDataclassUntypedGenericInheritance] |
| 724 |
from dataclasses import dataclass |
| 725 |
from typing import Generic, TypeVar |
| 726 |
|
| 727 |
T = TypeVar("T") |
| 728 |
|
| 729 |
@dataclass |
| 730 |
class Base(Generic[T]): |
| 731 |
attr: T |
| 732 |
|
| 733 |
@dataclass |
| 734 |
class Sub(Base): |
| 735 |
pass |
| 736 |
|
| 737 |
sub = Sub(attr=1) |
| 738 |
reveal_type(sub) # N: Revealed type is "__main__.Sub" |
| 739 |
reveal_type(sub.attr) # N: Revealed type is "Any" |
| 740 |
|
| 741 |
[builtins fixtures/dataclasses.pyi] |
| 742 |
|
| 743 |
[case testDataclassGenericSubtype] |
| 744 |
from dataclasses import dataclass |
| 745 |
from typing import Generic, TypeVar |
| 746 |
|
| 747 |
T = TypeVar("T") |
| 748 |
|
| 749 |
@dataclass |
| 750 |
class Base(Generic[T]): |
| 751 |
attr: T |
| 752 |
|
| 753 |
S = TypeVar("S") |
| 754 |
|
| 755 |
@dataclass |
| 756 |
class Sub(Base[S]): |
| 757 |
pass |
| 758 |
|
| 759 |
sub_int = Sub[int](attr=1) |
| 760 |
reveal_type(sub_int) # N: Revealed type is "__main__.Sub[builtins.int]" |
| 761 |
reveal_type(sub_int.attr) # N: Revealed type is "builtins.int" |
| 762 |
|
| 763 |
sub_str = Sub[str](attr='ok') |
| 764 |
reveal_type(sub_str) # N: Revealed type is "__main__.Sub[builtins.str]" |
| 765 |
reveal_type(sub_str.attr) # N: Revealed type is "builtins.str" |
| 766 |
|
| 767 |
[builtins fixtures/dataclasses.pyi] |
| 768 |
|
| 769 |
[case testDataclassGenericInheritance] |
| 770 |
from dataclasses import dataclass |
| 771 |
from typing import Generic, TypeVar |
| 772 |
|
| 773 |
T1 = TypeVar("T1") |
| 774 |
T2 = TypeVar("T2") |
| 775 |
T3 = TypeVar("T3") |
| 776 |
|
| 777 |
@dataclass |
| 778 |
class Base(Generic[T1, T2, T3]): |
| 779 |
one: T1 |
| 780 |
two: T2 |
| 781 |
three: T3 |
| 782 |
|
| 783 |
@dataclass |
| 784 |
class Sub(Base[int, str, float]): |
| 785 |
pass |
| 786 |
|
| 787 |
sub = Sub(one=1, two='ok', three=3.14) |
| 788 |
reveal_type(sub) # N: Revealed type is "__main__.Sub" |
| 789 |
reveal_type(sub.one) # N: Revealed type is "builtins.int" |
| 790 |
reveal_type(sub.two) # N: Revealed type is "builtins.str" |
| 791 |
reveal_type(sub.three) # N: Revealed type is "builtins.float" |
| 792 |
|
| 793 |
[builtins fixtures/dataclasses.pyi] |
| 794 |
|
| 795 |
[case testDataclassMultiGenericInheritance] |
| 796 |
from dataclasses import dataclass |
| 797 |
from typing import Generic, TypeVar |
| 798 |
|
| 799 |
T = TypeVar("T") |
| 800 |
|
| 801 |
@dataclass |
| 802 |
class Base(Generic[T]): |
| 803 |
base_attr: T |
| 804 |
|
| 805 |
S = TypeVar("S") |
| 806 |
|
| 807 |
@dataclass |
| 808 |
class Middle(Base[int], Generic[S]): |
| 809 |
middle_attr: S |
| 810 |
|
| 811 |
@dataclass |
| 812 |
class Sub(Middle[str]): |
| 813 |
pass |
| 814 |
|
| 815 |
sub = Sub(base_attr=1, middle_attr='ok') |
| 816 |
reveal_type(sub) # N: Revealed type is "__main__.Sub" |
| 817 |
reveal_type(sub.base_attr) # N: Revealed type is "builtins.int" |
| 818 |
reveal_type(sub.middle_attr) # N: Revealed type is "builtins.str" |
| 819 |
|
| 820 |
[builtins fixtures/dataclasses.pyi] |
| 821 |
|
| 822 |
[case testDataclassGenericsClassmethod] |
| 823 |
from dataclasses import dataclass |
| 824 |
from typing import Generic, TypeVar |
| 825 |
|
| 826 |
T = TypeVar('T') |
| 827 |
|
| 828 |
@dataclass |
| 829 |
class A(Generic[T]): |
| 830 |
x: T |
| 831 |
|
| 832 |
@classmethod |
| 833 |
def foo(cls) -> None: |
| 834 |
reveal_type(cls) # N: Revealed type is "type[__main__.A[T`1]]" |
| 835 |
cls.x # E: Cannot access instance-only attribute "x" on class object \ |
| 836 |
# E: Access to generic instance variables via class is ambiguous |
| 837 |
|
| 838 |
@classmethod |
| 839 |
def other(cls, x: T) -> A[T]: ... |
| 840 |
|
| 841 |
reveal_type(A(0).other) # N: Revealed type is "def (x: builtins.int) -> __main__.A[builtins.int]" |
| 842 |
[builtins fixtures/dataclasses.pyi] |
| 843 |
|
| 844 |
[case testDataclassesForwardRefs] |
| 845 |
from dataclasses import dataclass |
| 846 |
|
| 847 |
@dataclass |
| 848 |
class A: |
| 849 |
b: 'B' |
| 850 |
|
| 851 |
@dataclass |
| 852 |
class B: |
| 853 |
x: int |
| 854 |
|
| 855 |
reveal_type(A) # N: Revealed type is "def (b: __main__.B) -> __main__.A" |
| 856 |
A(b=B(42)) |
| 857 |
A(b=42) # E: Argument "b" to "A" has incompatible type "int"; expected "B" |
| 858 |
|
| 859 |
[builtins fixtures/dataclasses.pyi] |
| 860 |
|
| 861 |
|
| 862 |
[case testDataclassesInitVars] |
| 863 |
from dataclasses import InitVar, dataclass |
| 864 |
|
| 865 |
@dataclass |
| 866 |
class Application: |
| 867 |
name: str |
| 868 |
database_name: InitVar[str] |
| 869 |
|
| 870 |
reveal_type(Application) # N: Revealed type is "def (name: builtins.str, database_name: builtins.str) -> __main__.Application" |
| 871 |
app = Application("example", 42) # E: Argument 2 to "Application" has incompatible type "int"; expected "str" |
| 872 |
app = Application("example", "apps") |
| 873 |
app.name |
| 874 |
app.database_name # E: "Application" has no attribute "database_name" |
| 875 |
|
| 876 |
|
| 877 |
@dataclass |
| 878 |
class SpecializedApplication(Application): |
| 879 |
rating: int |
| 880 |
|
| 881 |
reveal_type(SpecializedApplication) # N: Revealed type is "def (name: builtins.str, database_name: builtins.str, rating: builtins.int) -> __main__.SpecializedApplication" |
| 882 |
app = SpecializedApplication("example", "apps", "five") # E: Argument 3 to "SpecializedApplication" has incompatible type "str"; expected "int" |
| 883 |
app = SpecializedApplication("example", "apps", 5) |
| 884 |
app.name |
| 885 |
app.rating |
| 886 |
app.database_name # E: "SpecializedApplication" has no attribute "database_name" |
| 887 |
|
| 888 |
[builtins fixtures/dataclasses.pyi] |
| 889 |
|
| 890 |
[case testDataclassesInitVarsAndDefer] |
| 891 |
from dataclasses import InitVar, dataclass |
| 892 |
|
| 893 |
defer: Yes |
| 894 |
|
| 895 |
@dataclass |
| 896 |
class Application: |
| 897 |
name: str |
| 898 |
database_name: InitVar[str] |
| 899 |
|
| 900 |
reveal_type(Application) # N: Revealed type is "def (name: builtins.str, database_name: builtins.str) -> __main__.Application" |
| 901 |
app = Application("example", 42) # E: Argument 2 to "Application" has incompatible type "int"; expected "str" |
| 902 |
app = Application("example", "apps") |
| 903 |
app.name |
| 904 |
app.database_name # E: "Application" has no attribute "database_name" |
| 905 |
|
| 906 |
class Yes: ... |
| 907 |
[builtins fixtures/dataclasses.pyi] |
| 908 |
|
| 909 |
[case testDataclassesNoInitInitVarInheritance] |
| 910 |
from dataclasses import dataclass, field, InitVar |
| 911 |
|
| 912 |
@dataclass |
| 913 |
class Super: |
| 914 |
foo: InitVar = field(init=False) |
| 915 |
|
| 916 |
@dataclass |
| 917 |
class Sub(Super): |
| 918 |
bar: int |
| 919 |
|
| 920 |
sub = Sub(5) |
| 921 |
sub.foo # E: "Sub" has no attribute "foo" |
| 922 |
sub.bar |
| 923 |
[builtins fixtures/dataclasses.pyi] |
| 924 |
|
| 925 |
[case testDataclassFactory] |
| 926 |
from typing import Type, TypeVar |
| 927 |
from dataclasses import dataclass |
| 928 |
|
| 929 |
T = TypeVar('T', bound='A') |
| 930 |
|
| 931 |
@dataclass |
| 932 |
class A: |
| 933 |
@classmethod |
| 934 |
def make(cls: Type[T]) -> T: |
| 935 |
reveal_type(cls) # N: Revealed type is "type[T`-1]" |
| 936 |
reveal_type(cls()) # N: Revealed type is "T`-1" |
| 937 |
return cls() |
| 938 |
[builtins fixtures/dataclasses.pyi] |
| 939 |
|
| 940 |
[case testDataclassesInitVarOverride] |
| 941 |
import dataclasses |
| 942 |
|
| 943 |
@dataclasses.dataclass |
| 944 |
class A: |
| 945 |
a: dataclasses.InitVar[int] |
| 946 |
_a: int = dataclasses.field(init=False) |
| 947 |
|
| 948 |
def __post_init__(self, a): |
| 949 |
self._a = a |
| 950 |
|
| 951 |
@dataclasses.dataclass(init=False) |
| 952 |
class B(A): |
| 953 |
b: dataclasses.InitVar[int] |
| 954 |
_b: int = dataclasses.field(init=False) |
| 955 |
|
| 956 |
def __init__(self, b): |
| 957 |
super().__init__(b+1) |
| 958 |
self._b = b |
| 959 |
|
| 960 |
[builtins fixtures/dataclasses.pyi] |
| 961 |
|
| 962 |
[case testDataclassesInitVarNoOverride] |
| 963 |
import dataclasses |
| 964 |
|
| 965 |
@dataclasses.dataclass |
| 966 |
class A: |
| 967 |
a: dataclasses.InitVar[int] |
| 968 |
_a: int = dataclasses.field(init=False) |
| 969 |
|
| 970 |
def __post_init__(self, a): |
| 971 |
self._a = a |
| 972 |
|
| 973 |
@dataclasses.dataclass(init=True) |
| 974 |
class B(A): |
| 975 |
b: dataclasses.InitVar[int] |
| 976 |
_b: int = dataclasses.field(init=False) |
| 977 |
|
| 978 |
def __post_init__(self, a, b): |
| 979 |
self._a = a |
| 980 |
self._b = b |
| 981 |
|
| 982 |
B(1, 2) |
| 983 |
B(1, 'a') # E: Argument 2 to "B" has incompatible type "str"; expected "int" |
| 984 |
|
| 985 |
[builtins fixtures/dataclasses.pyi] |
| 986 |
|
| 987 |
[case testDataclassesInitVarPostInitOverride] |
| 988 |
import dataclasses |
| 989 |
|
| 990 |
@dataclasses.dataclass |
| 991 |
class A: |
| 992 |
a: dataclasses.InitVar[int] |
| 993 |
_a: int = dataclasses.field(init=False) |
| 994 |
|
| 995 |
def __post_init__(self, a: int) -> None: |
| 996 |
self._a = a |
| 997 |
|
| 998 |
@dataclasses.dataclass |
| 999 |
class B(A): |
| 1000 |
b: int = dataclasses.field(init=False) |
| 1001 |
|
| 1002 |
def __post_init__(self, a: int) -> None: |
| 1003 |
super().__post_init__(a) |
| 1004 |
self.b = a + 1 |
| 1005 |
|
| 1006 |
@dataclasses.dataclass(init=False) |
| 1007 |
class C(B): |
| 1008 |
c: int |
| 1009 |
|
| 1010 |
def __init__(self, a: int, c: int) -> None: |
| 1011 |
super().__init__(a) |
| 1012 |
self.c = c + self.b |
| 1013 |
|
| 1014 |
A(1) |
| 1015 |
B(1) |
| 1016 |
B(1, 2) # E: Too many arguments for "B" |
| 1017 |
C(1, 2) |
| 1018 |
C(1, 'a') # E: Argument 2 to "C" has incompatible type "str"; expected "int" |
| 1019 |
|
| 1020 |
[builtins fixtures/primitives.pyi] |
| 1021 |
|
| 1022 |
[case testDataclassesInitVarIncremental] |
| 1023 |
import a |
| 1024 |
|
| 1025 |
[file a.py] |
| 1026 |
import dataclasses |
| 1027 |
from b import A |
| 1028 |
|
| 1029 |
@dataclasses.dataclass |
| 1030 |
class B(A): |
| 1031 |
b: int = dataclasses.field(init=False) |
| 1032 |
|
| 1033 |
def __post_init__(self, a: int) -> None: |
| 1034 |
super().__post_init__(a) |
| 1035 |
self.b = a + 1 |
| 1036 |
|
| 1037 |
[file a.py.2] |
| 1038 |
import dataclasses |
| 1039 |
from b import A |
| 1040 |
|
| 1041 |
@dataclasses.dataclass |
| 1042 |
class B(A): |
| 1043 |
b: int = dataclasses.field(init=False) |
| 1044 |
|
| 1045 |
def __post_init__(self, a: int) -> None: |
| 1046 |
super().__post_init__(a) |
| 1047 |
self.b = a + 2 |
| 1048 |
|
| 1049 |
reveal_type(B) |
| 1050 |
|
| 1051 |
[file b.py] |
| 1052 |
import dataclasses |
| 1053 |
|
| 1054 |
@dataclasses.dataclass |
| 1055 |
class A: |
| 1056 |
a: dataclasses.InitVar[int] |
| 1057 |
_a: int = dataclasses.field(init=False) |
| 1058 |
|
| 1059 |
def __post_init__(self, a: int) -> None: |
| 1060 |
self._a = a |
| 1061 |
[out2] |
| 1062 |
tmp/a.py:12: note: Revealed type is "def (a: builtins.int) -> a.B" |
| 1063 |
|
| 1064 |
[builtins fixtures/primitives.pyi] |
| 1065 |
|
| 1066 |
|
| 1067 |
[case testNoComplainFieldNone] |
| 1068 |
# flags: --no-strict-optional |
| 1069 |
from dataclasses import dataclass, field |
| 1070 |
from typing import Optional |
| 1071 |
|
| 1072 |
@dataclass |
| 1073 |
class Foo: |
| 1074 |
bar: Optional[int] = field(default=None) |
| 1075 |
[builtins fixtures/dataclasses.pyi] |
| 1076 |
[out] |
| 1077 |
|
| 1078 |
[case testNoComplainFieldNoneStrict] |
| 1079 |
from dataclasses import dataclass, field |
| 1080 |
from typing import Optional |
| 1081 |
|
| 1082 |
@dataclass |
| 1083 |
class Foo: |
| 1084 |
bar: Optional[int] = field(default=None) |
| 1085 |
[builtins fixtures/dataclasses.pyi] |
| 1086 |
[out] |
| 1087 |
|
| 1088 |
[case testDisallowUntypedWorksForward] |
| 1089 |
# flags: --disallow-untyped-defs |
| 1090 |
from dataclasses import dataclass |
| 1091 |
from typing import List |
| 1092 |
|
| 1093 |
@dataclass |
| 1094 |
class B: |
| 1095 |
x: C |
| 1096 |
|
| 1097 |
class C(List[C]): |
| 1098 |
pass |
| 1099 |
|
| 1100 |
reveal_type(B) # N: Revealed type is "def (x: __main__.C) -> __main__.B" |
| 1101 |
[builtins fixtures/dataclasses.pyi] |
| 1102 |
|
| 1103 |
[case testDisallowUntypedWorksForwardBad] |
| 1104 |
# flags: --disallow-untyped-defs |
| 1105 |
from dataclasses import dataclass |
| 1106 |
|
| 1107 |
@dataclass |
| 1108 |
class B: |
| 1109 |
x: Undefined # E: Name "Undefined" is not defined |
| 1110 |
y = undefined() # E: Name "undefined" is not defined |
| 1111 |
|
| 1112 |
reveal_type(B) # N: Revealed type is "def (x: Any) -> __main__.B" |
| 1113 |
[builtins fixtures/dataclasses.pyi] |
| 1114 |
|
| 1115 |
[case testMemberExprWorksAsField] |
| 1116 |
import dataclasses |
| 1117 |
|
| 1118 |
@dataclasses.dataclass |
| 1119 |
class A: |
| 1120 |
x: int = dataclasses.field(metadata={"doc": "foo"}) |
| 1121 |
y: str |
| 1122 |
|
| 1123 |
@dataclasses.dataclass |
| 1124 |
class B: |
| 1125 |
x: int = dataclasses.field(init=False, default=1) |
| 1126 |
y: str |
| 1127 |
|
| 1128 |
@dataclasses.dataclass |
| 1129 |
class C: |
| 1130 |
x: int = dataclasses.field(default=1) |
| 1131 |
y: str = dataclasses.field(metadata={"doc": "foo"}) # E: Attributes without a default cannot follow attributes with one |
| 1132 |
[builtins fixtures/dict.pyi] |
| 1133 |
|
| 1134 |
[case testDataclassOrderingDeferred] |
| 1135 |
from dataclasses import dataclass |
| 1136 |
|
| 1137 |
defer: Yes |
| 1138 |
|
| 1139 |
@dataclass(order=True) |
| 1140 |
class Application: |
| 1141 |
name: str |
| 1142 |
rating: int |
| 1143 |
|
| 1144 |
a = Application('', 0) |
| 1145 |
b = Application('', 0) |
| 1146 |
a < b |
| 1147 |
|
| 1148 |
class Yes: ... |
| 1149 |
[builtins fixtures/dataclasses.pyi] |
| 1150 |
|
| 1151 |
[case testDataclassFieldDeferred] |
| 1152 |
from dataclasses import field, dataclass |
| 1153 |
|
| 1154 |
@dataclass |
| 1155 |
class C: |
| 1156 |
x: int = field(default=func()) |
| 1157 |
|
| 1158 |
def func() -> int: ... |
| 1159 |
C('no') # E: Argument 1 to "C" has incompatible type "str"; expected "int" |
| 1160 |
[builtins fixtures/dataclasses.pyi] |
| 1161 |
|
| 1162 |
[case testDataclassFieldDeferredFrozen] |
| 1163 |
from dataclasses import field, dataclass |
| 1164 |
|
| 1165 |
@dataclass(frozen=True) |
| 1166 |
class C: |
| 1167 |
x: int = field(default=func()) |
| 1168 |
|
| 1169 |
def func() -> int: ... |
| 1170 |
c: C |
| 1171 |
c.x = 1 # E: Property "x" defined in "C" is read-only |
| 1172 |
[builtins fixtures/dataclasses.pyi] |
| 1173 |
|
| 1174 |
[case testTypeInDataclassDeferredStar] |
| 1175 |
import lib |
| 1176 |
[file lib.py] |
| 1177 |
from dataclasses import dataclass |
| 1178 |
MYPY = False |
| 1179 |
if MYPY: # Force deferral |
| 1180 |
from other import * |
| 1181 |
|
| 1182 |
@dataclass |
| 1183 |
class C: |
| 1184 |
total: int |
| 1185 |
|
| 1186 |
C() # E: Missing positional argument "total" in call to "C" |
| 1187 |
C('no') # E: Argument 1 to "C" has incompatible type "str"; expected "int" |
| 1188 |
[file other.py] |
| 1189 |
import lib |
| 1190 |
[builtins fixtures/dataclasses.pyi] |
| 1191 |
|
| 1192 |
[case testDeferredDataclassInitSignature] |
| 1193 |
# flags: --no-strict-optional |
| 1194 |
from dataclasses import dataclass |
| 1195 |
from typing import Optional, Type |
| 1196 |
|
| 1197 |
@dataclass |
| 1198 |
class C: |
| 1199 |
x: Optional[int] = None |
| 1200 |
y: Type[Deferred] = Deferred |
| 1201 |
|
| 1202 |
@classmethod |
| 1203 |
def default(cls) -> C: |
| 1204 |
return cls(x=None, y=None) |
| 1205 |
|
| 1206 |
class Deferred: pass |
| 1207 |
[builtins fixtures/dataclasses.pyi] |
| 1208 |
|
| 1209 |
[case testDeferredDataclassInitSignatureSubclass] |
| 1210 |
from dataclasses import dataclass |
| 1211 |
from typing import Optional |
| 1212 |
|
| 1213 |
@dataclass |
| 1214 |
class B: |
| 1215 |
x: Optional[C] |
| 1216 |
|
| 1217 |
@dataclass |
| 1218 |
class C(B): |
| 1219 |
y: str |
| 1220 |
|
| 1221 |
a = C(None, 'abc') |
| 1222 |
[builtins fixtures/dataclasses.pyi] |
| 1223 |
|
| 1224 |
[case testDataclassesDefaultsIncremental] |
| 1225 |
import a |
| 1226 |
|
| 1227 |
[file a.py] |
| 1228 |
from dataclasses import dataclass |
| 1229 |
from b import Person |
| 1230 |
|
| 1231 |
@dataclass |
| 1232 |
class Asdf(Person): |
| 1233 |
c: str = 'test' |
| 1234 |
|
| 1235 |
[file a.py.2] |
| 1236 |
from dataclasses import dataclass |
| 1237 |
from b import Person |
| 1238 |
|
| 1239 |
@dataclass |
| 1240 |
class Asdf(Person): |
| 1241 |
c: str = 'test' |
| 1242 |
|
| 1243 |
# asdf |
| 1244 |
|
| 1245 |
[file b.py] |
| 1246 |
from dataclasses import dataclass |
| 1247 |
|
| 1248 |
@dataclass |
| 1249 |
class Person: |
| 1250 |
b: int |
| 1251 |
a: str = 'test' |
| 1252 |
|
| 1253 |
[builtins fixtures/dataclasses.pyi] |
| 1254 |
|
| 1255 |
[case testDataclassesFloatSerializationIncremental] |
| 1256 |
import m |
| 1257 |
[file m.py] |
| 1258 |
from lib import MyDataClass |
| 1259 |
[file m.py.2] |
| 1260 |
from lib import MyDataClass |
| 1261 |
|
| 1262 |
reveal_type(MyDataClass.MY_CONSTANT) |
| 1263 |
[file lib.py] |
| 1264 |
from dataclasses import dataclass |
| 1265 |
from typing import Final |
| 1266 |
|
| 1267 |
@dataclass(kw_only=True, repr=False) |
| 1268 |
class MyDataClass: |
| 1269 |
MY_CONSTANT: Final = 1.234 |
| 1270 |
[builtins fixtures/dataclasses.pyi] |
| 1271 |
[out] |
| 1272 |
[out2] |
| 1273 |
tmp/m.py:3: note: Revealed type is "Literal[1.234]?" |
| 1274 |
|
| 1275 |
[case testDataclassesDefaultsMroOtherFile] |
| 1276 |
import a |
| 1277 |
|
| 1278 |
[file a.py] |
| 1279 |
from dataclasses import dataclass |
| 1280 |
from b import A1, A2 |
| 1281 |
|
| 1282 |
@dataclass |
| 1283 |
class Asdf(A1, A2): # E: Attributes without a default cannot follow attributes with one |
| 1284 |
pass |
| 1285 |
|
| 1286 |
[file b.py] |
| 1287 |
from dataclasses import dataclass |
| 1288 |
|
| 1289 |
# a bunch of blank lines to make sure the error doesn't accidentally line up... |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
@dataclass |
| 1294 |
class A1: |
| 1295 |
a: int |
| 1296 |
|
| 1297 |
@dataclass |
| 1298 |
class A2: |
| 1299 |
b: str = 'test' |
| 1300 |
|
| 1301 |
[builtins fixtures/dataclasses.pyi] |
| 1302 |
|
| 1303 |
[case testDataclassesInheritingDuplicateField] |
| 1304 |
# see mypy issue #7792 |
| 1305 |
from dataclasses import dataclass |
| 1306 |
|
| 1307 |
@dataclass |
| 1308 |
class A: |
| 1309 |
x: int = 0 |
| 1310 |
x: int = 0 # E: Name "x" already defined on line 6 |
| 1311 |
|
| 1312 |
@dataclass |
| 1313 |
class B(A): |
| 1314 |
pass |
| 1315 |
|
| 1316 |
[builtins fixtures/dataclasses.pyi] |
| 1317 |
|
| 1318 |
[case testDataclassInheritanceNoAnnotation] |
| 1319 |
from dataclasses import dataclass |
| 1320 |
|
| 1321 |
@dataclass |
| 1322 |
class A: |
| 1323 |
foo: int |
| 1324 |
|
| 1325 |
x = 0 |
| 1326 |
@dataclass |
| 1327 |
class B(A): |
| 1328 |
foo = x |
| 1329 |
|
| 1330 |
reveal_type(B) # N: Revealed type is "def (foo: builtins.int) -> __main__.B" |
| 1331 |
|
| 1332 |
[builtins fixtures/dataclasses.pyi] |
| 1333 |
|
| 1334 |
[case testDataclassInheritanceNoAnnotation2] |
| 1335 |
from dataclasses import dataclass |
| 1336 |
|
| 1337 |
@dataclass(frozen=True) |
| 1338 |
class A: |
| 1339 |
foo: int |
| 1340 |
|
| 1341 |
@dataclass(frozen=True) |
| 1342 |
class B(A): |
| 1343 |
@property # E: Dataclass attribute may only be overridden by another attribute |
| 1344 |
def foo(self) -> int: pass |
| 1345 |
|
| 1346 |
reveal_type(B) # N: Revealed type is "def (foo: builtins.int) -> __main__.B" |
| 1347 |
|
| 1348 |
[builtins fixtures/dataclasses.pyi] |
| 1349 |
|
| 1350 |
[case testDataclassHasAttributeWithFields] |
| 1351 |
from dataclasses import dataclass |
| 1352 |
|
| 1353 |
@dataclass |
| 1354 |
class A: |
| 1355 |
pass |
| 1356 |
|
| 1357 |
reveal_type(A.__dataclass_fields__) # N: Revealed type is "builtins.dict[builtins.str, dataclasses.Field[Any]]" |
| 1358 |
|
| 1359 |
[builtins fixtures/dict.pyi] |
| 1360 |
|
| 1361 |
[case testDataclassCallableFieldAccess] |
| 1362 |
from dataclasses import dataclass |
| 1363 |
from typing import Callable |
| 1364 |
|
| 1365 |
@dataclass |
| 1366 |
class A: |
| 1367 |
x: Callable[[int], int] |
| 1368 |
y: Callable[[int], int] = lambda i: i |
| 1369 |
|
| 1370 |
a = A(lambda i:i) |
| 1371 |
x: int = a.x(0) |
| 1372 |
y: str = a.y(0) # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 1373 |
reveal_type(a.x) # N: Revealed type is "def (builtins.int) -> builtins.int" |
| 1374 |
reveal_type(a.y) # N: Revealed type is "def (builtins.int) -> builtins.int" |
| 1375 |
reveal_type(A.y) # N: Revealed type is "def (builtins.int) -> builtins.int" |
| 1376 |
[builtins fixtures/dataclasses.pyi] |
| 1377 |
|
| 1378 |
[case testDataclassCallableFieldAssignment] |
| 1379 |
from dataclasses import dataclass |
| 1380 |
from typing import Callable |
| 1381 |
|
| 1382 |
@dataclass |
| 1383 |
class A: |
| 1384 |
x: Callable[[int], int] |
| 1385 |
|
| 1386 |
def x(i: int) -> int: |
| 1387 |
return i |
| 1388 |
def x2(s: str) -> str: |
| 1389 |
return s |
| 1390 |
|
| 1391 |
a = A(lambda i:i) |
| 1392 |
a.x = x |
| 1393 |
a.x = x2 # E: Incompatible types in assignment (expression has type "Callable[[str], str]", variable has type "Callable[[int], int]") |
| 1394 |
[builtins fixtures/dataclasses.pyi] |
| 1395 |
|
| 1396 |
[case testDataclassFieldDoesNotFailOnKwargsUnpacking] |
| 1397 |
# https://github.com/python/mypy/issues/10879 |
| 1398 |
from dataclasses import dataclass, field |
| 1399 |
|
| 1400 |
@dataclass |
| 1401 |
class Foo: |
| 1402 |
bar: float = field(**{"repr": False}) |
| 1403 |
[out] |
| 1404 |
main:6: error: Unpacking **kwargs in "field()" is not supported |
| 1405 |
main:6: error: No overload variant of "field" matches argument type "dict[str, bool]" |
| 1406 |
main:6: note: Possible overload variants: |
| 1407 |
main:6: note: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T |
| 1408 |
main:6: note: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T |
| 1409 |
main:6: note: def field(*, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> Any |
| 1410 |
[builtins fixtures/dataclasses.pyi] |
| 1411 |
|
| 1412 |
[case testDataclassFieldWithPositionalArguments] |
| 1413 |
from dataclasses import dataclass, field |
| 1414 |
|
| 1415 |
@dataclass |
| 1416 |
class C: |
| 1417 |
x: int = field(0) # E: "field()" does not accept positional arguments \ |
| 1418 |
# E: No overload variant of "field" matches argument type "int" \ |
| 1419 |
# N: Possible overload variants: \ |
| 1420 |
# N: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T \ |
| 1421 |
# N: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T \ |
| 1422 |
# N: def field(*, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> Any |
| 1423 |
[builtins fixtures/dataclasses.pyi] |
| 1424 |
|
| 1425 |
[case testDataclassFieldWithTypedDictUnpacking] |
| 1426 |
from dataclasses import dataclass, field |
| 1427 |
from typing import TypedDict |
| 1428 |
|
| 1429 |
class FieldKwargs(TypedDict): |
| 1430 |
repr: bool |
| 1431 |
|
| 1432 |
field_kwargs: FieldKwargs = {"repr": False} |
| 1433 |
|
| 1434 |
@dataclass |
| 1435 |
class Foo: |
| 1436 |
bar: float = field(**field_kwargs) # E: Unpacking **kwargs in "field()" is not supported |
| 1437 |
|
| 1438 |
reveal_type(Foo(bar=1.5)) # N: Revealed type is "__main__.Foo" |
| 1439 |
[builtins fixtures/dataclasses.pyi] |
| 1440 |
[typing fixtures/typing-typeddict.pyi] |
| 1441 |
|
| 1442 |
[case testDataclassWithSlotsArg] |
| 1443 |
from dataclasses import dataclass |
| 1444 |
|
| 1445 |
@dataclass(slots=True) |
| 1446 |
class Some: |
| 1447 |
x: int |
| 1448 |
|
| 1449 |
def __init__(self, x: int) -> None: |
| 1450 |
self.x = x |
| 1451 |
self.y = 0 # E: Trying to assign name "y" that is not in "__slots__" of type "__main__.Some" |
| 1452 |
|
| 1453 |
def __post_init__(self) -> None: |
| 1454 |
self.y = 1 # E: Trying to assign name "y" that is not in "__slots__" of type "__main__.Some" |
| 1455 |
[builtins fixtures/dataclasses.pyi] |
| 1456 |
|
| 1457 |
[case testDataclassWithSlotsDef] |
| 1458 |
from dataclasses import dataclass |
| 1459 |
|
| 1460 |
@dataclass(slots=False) |
| 1461 |
class Some: |
| 1462 |
__slots__ = ('x',) |
| 1463 |
x: int |
| 1464 |
|
| 1465 |
def __init__(self, x: int) -> None: |
| 1466 |
self.x = x |
| 1467 |
self.y = 0 # E: Trying to assign name "y" that is not in "__slots__" of type "__main__.Some" |
| 1468 |
|
| 1469 |
def __post_init__(self) -> None: |
| 1470 |
self.y = 1 # E: Trying to assign name "y" that is not in "__slots__" of type "__main__.Some" |
| 1471 |
[builtins fixtures/dataclasses.pyi] |
| 1472 |
|
| 1473 |
[case testDataclassWithSlotsDerivedFromNonSlot] |
| 1474 |
from dataclasses import dataclass |
| 1475 |
|
| 1476 |
class A: |
| 1477 |
pass |
| 1478 |
|
| 1479 |
@dataclass(slots=True) |
| 1480 |
class B(A): |
| 1481 |
x: int |
| 1482 |
|
| 1483 |
def __post_init__(self) -> None: |
| 1484 |
self.y = 42 |
| 1485 |
|
| 1486 |
[builtins fixtures/dataclasses.pyi] |
| 1487 |
|
| 1488 |
[case testDataclassWithSlotsConflict] |
| 1489 |
from dataclasses import dataclass |
| 1490 |
|
| 1491 |
@dataclass(slots=True) |
| 1492 |
class Some: # E: "Some" both defines "__slots__" and is used with "slots=True" |
| 1493 |
__slots__ = ('x',) |
| 1494 |
x: int |
| 1495 |
|
| 1496 |
@dataclass(slots=True) |
| 1497 |
class EmptyDef: # E: "EmptyDef" both defines "__slots__" and is used with "slots=True" |
| 1498 |
__slots__ = () |
| 1499 |
x: int |
| 1500 |
|
| 1501 |
slots = ('x',) |
| 1502 |
|
| 1503 |
@dataclass(slots=True) |
| 1504 |
class DynamicDef: # E: "DynamicDef" both defines "__slots__" and is used with "slots=True" |
| 1505 |
__slots__ = slots |
| 1506 |
x: int |
| 1507 |
[builtins fixtures/dataclasses.pyi] |
| 1508 |
|
| 1509 |
[case testDataclassWithSlotsRuntimeAttr] |
| 1510 |
from dataclasses import dataclass |
| 1511 |
|
| 1512 |
@dataclass(slots=True) |
| 1513 |
class Some: |
| 1514 |
x: int |
| 1515 |
y: str |
| 1516 |
z: bool |
| 1517 |
|
| 1518 |
reveal_type(Some.__slots__) # N: Revealed type is "tuple[builtins.str, builtins.str, builtins.str]" |
| 1519 |
|
| 1520 |
@dataclass(slots=True) |
| 1521 |
class Other: |
| 1522 |
x: int |
| 1523 |
y: str |
| 1524 |
|
| 1525 |
reveal_type(Other.__slots__) # N: Revealed type is "tuple[builtins.str, builtins.str]" |
| 1526 |
|
| 1527 |
|
| 1528 |
@dataclass |
| 1529 |
class NoSlots: |
| 1530 |
x: int |
| 1531 |
y: str |
| 1532 |
|
| 1533 |
NoSlots.__slots__ # E: "type[NoSlots]" has no attribute "__slots__" |
| 1534 |
[builtins fixtures/dataclasses.pyi] |
| 1535 |
|
| 1536 |
|
| 1537 |
[case testSlotsDefinitionWithTwoPasses1] |
| 1538 |
# https://github.com/python/mypy/issues/11821 |
| 1539 |
from typing import TypeVar, Protocol, Generic |
| 1540 |
from dataclasses import dataclass |
| 1541 |
|
| 1542 |
C = TypeVar("C", bound="Comparable") |
| 1543 |
|
| 1544 |
class Comparable(Protocol): |
| 1545 |
pass |
| 1546 |
|
| 1547 |
V = TypeVar("V", bound=Comparable) |
| 1548 |
|
| 1549 |
@dataclass(slots=True) |
| 1550 |
class Node(Generic[V]): # Error was here |
| 1551 |
data: V |
| 1552 |
[builtins fixtures/dataclasses.pyi] |
| 1553 |
|
| 1554 |
[case testSlotsDefinitionWithTwoPasses2] |
| 1555 |
from typing import TypeVar, Protocol, Generic |
| 1556 |
from dataclasses import dataclass |
| 1557 |
|
| 1558 |
C = TypeVar("C", bound="Comparable") |
| 1559 |
|
| 1560 |
class Comparable(Protocol): |
| 1561 |
pass |
| 1562 |
|
| 1563 |
V = TypeVar("V", bound=Comparable) |
| 1564 |
|
| 1565 |
@dataclass(slots=True) # Explicit slots are still not ok: |
| 1566 |
class Node(Generic[V]): # E: "Node" both defines "__slots__" and is used with "slots=True" |
| 1567 |
__slots__ = ('data',) |
| 1568 |
data: V |
| 1569 |
[builtins fixtures/dataclasses.pyi] |
| 1570 |
|
| 1571 |
[case testSlotsDefinitionWithTwoPasses3] |
| 1572 |
from typing import TypeVar, Protocol, Generic |
| 1573 |
from dataclasses import dataclass |
| 1574 |
|
| 1575 |
C = TypeVar("C", bound="Comparable") |
| 1576 |
|
| 1577 |
class Comparable(Protocol): |
| 1578 |
pass |
| 1579 |
|
| 1580 |
V = TypeVar("V", bound=Comparable) |
| 1581 |
|
| 1582 |
@dataclass(slots=True) # Explicit slots are still not ok, even empty ones: |
| 1583 |
class Node(Generic[V]): # E: "Node" both defines "__slots__" and is used with "slots=True" |
| 1584 |
__slots__ = () |
| 1585 |
data: V |
| 1586 |
[builtins fixtures/dataclasses.pyi] |
| 1587 |
|
| 1588 |
[case testSlotsDefinitionWithTwoPasses4] |
| 1589 |
import dataclasses as dtc |
| 1590 |
|
| 1591 |
PublishedMessagesVar = dict[int, 'PublishedMessages'] |
| 1592 |
|
| 1593 |
@dtc.dataclass(frozen=True, slots=True) |
| 1594 |
class PublishedMessages: |
| 1595 |
left: int |
| 1596 |
[builtins fixtures/dataclasses.pyi] |
| 1597 |
|
| 1598 |
[case testDataclassSlotsWithDeferredBase] |
| 1599 |
from dataclasses import dataclass |
| 1600 |
|
| 1601 |
@dataclass |
| 1602 |
class A(B): ... |
| 1603 |
|
| 1604 |
@dataclass |
| 1605 |
class B: ... |
| 1606 |
|
| 1607 |
@dataclass(slots=True) |
| 1608 |
class C: ... |
| 1609 |
[builtins fixtures/dataclasses.pyi] |
| 1610 |
|
| 1611 |
[case testDataclassesAnyInherit] |
| 1612 |
from dataclasses import dataclass |
| 1613 |
from typing import Any |
| 1614 |
B: Any |
| 1615 |
@dataclass |
| 1616 |
class A(B): |
| 1617 |
a: int |
| 1618 |
@dataclass |
| 1619 |
class C(B): |
| 1620 |
generated_args: int |
| 1621 |
generated_kwargs: int |
| 1622 |
|
| 1623 |
A(a=1, b=2) |
| 1624 |
A(1) |
| 1625 |
A(a="foo") # E: Argument "a" to "A" has incompatible type "str"; expected "int" |
| 1626 |
C(generated_args="foo", generated_kwargs="bar") # E: Argument "generated_args" to "C" has incompatible type "str"; expected "int" \ |
| 1627 |
# E: Argument "generated_kwargs" to "C" has incompatible type "str"; expected "int" |
| 1628 |
[builtins fixtures/dataclasses.pyi] |
| 1629 |
|
| 1630 |
[case testDataclassesCallableFrozen] |
| 1631 |
from dataclasses import dataclass |
| 1632 |
from typing import Any, Callable |
| 1633 |
@dataclass(frozen=True) |
| 1634 |
class A: |
| 1635 |
a: Callable[..., None] |
| 1636 |
|
| 1637 |
def func() -> None: |
| 1638 |
pass |
| 1639 |
|
| 1640 |
reveal_type(A.a) # N: Revealed type is "def (*Any, **Any)" |
| 1641 |
A(a=func).a() |
| 1642 |
A(a=func).a = func # E: Property "a" defined in "A" is read-only |
| 1643 |
[builtins fixtures/dataclasses.pyi] |
| 1644 |
|
| 1645 |
[case testDataclassInFunctionDoesNotCrash] |
| 1646 |
from dataclasses import dataclass |
| 1647 |
|
| 1648 |
def foo() -> None: |
| 1649 |
@dataclass |
| 1650 |
class Foo: |
| 1651 |
foo: int |
| 1652 |
# This used to crash (see #8703) |
| 1653 |
# The return type of __call__ here needs to be something undefined |
| 1654 |
# In order to trigger the crash that existed prior to #12762 |
| 1655 |
def __call__(self) -> asdf: ... # E: Name "asdf" is not defined |
| 1656 |
[builtins fixtures/dataclasses.pyi] |
| 1657 |
|
| 1658 |
[case testDataclassesMultipleInheritanceWithNonDataclass] |
| 1659 |
from dataclasses import dataclass |
| 1660 |
|
| 1661 |
@dataclass |
| 1662 |
class A: |
| 1663 |
prop_a: str |
| 1664 |
|
| 1665 |
@dataclass |
| 1666 |
class B: |
| 1667 |
prop_b: bool |
| 1668 |
|
| 1669 |
class Derived(A, B): |
| 1670 |
pass |
| 1671 |
[builtins fixtures/dataclasses.pyi] |
| 1672 |
|
| 1673 |
[case testDataclassGenericInheritance2] |
| 1674 |
from dataclasses import dataclass |
| 1675 |
from typing import Any, Callable, Generic, TypeVar, List |
| 1676 |
|
| 1677 |
T = TypeVar("T") |
| 1678 |
S = TypeVar("S") |
| 1679 |
|
| 1680 |
@dataclass |
| 1681 |
class Parent(Generic[T]): |
| 1682 |
f: Callable[[T], Any] |
| 1683 |
|
| 1684 |
@dataclass |
| 1685 |
class Child(Parent[T]): ... |
| 1686 |
|
| 1687 |
class A: ... |
| 1688 |
def func(obj: A) -> bool: ... |
| 1689 |
|
| 1690 |
reveal_type(Child[A](func).f) # N: Revealed type is "def (__main__.A) -> Any" |
| 1691 |
|
| 1692 |
@dataclass |
| 1693 |
class Parent2(Generic[T]): |
| 1694 |
a: List[T] |
| 1695 |
|
| 1696 |
@dataclass |
| 1697 |
class Child2(Generic[T, S], Parent2[S]): |
| 1698 |
b: List[T] |
| 1699 |
|
| 1700 |
reveal_type(Child2([A()], [1]).a) # N: Revealed type is "builtins.list[__main__.A]" |
| 1701 |
reveal_type(Child2[int, A]([A()], [1]).b) # N: Revealed type is "builtins.list[builtins.int]" |
| 1702 |
[builtins fixtures/dataclasses.pyi] |
| 1703 |
|
| 1704 |
[case testDataclassInheritOptionalType] |
| 1705 |
from dataclasses import dataclass |
| 1706 |
from typing import Any, Callable, Generic, TypeVar, List, Optional |
| 1707 |
|
| 1708 |
T = TypeVar("T") |
| 1709 |
|
| 1710 |
@dataclass |
| 1711 |
class Parent(Generic[T]): |
| 1712 |
x: Optional[str] |
| 1713 |
@dataclass |
| 1714 |
class Child(Parent): |
| 1715 |
y: Optional[int] |
| 1716 |
Child(x=1, y=1) # E: Argument "x" to "Child" has incompatible type "int"; expected "str | None" |
| 1717 |
Child(x='', y='') # E: Argument "y" to "Child" has incompatible type "str"; expected "int | None" |
| 1718 |
Child(x='', y=1) |
| 1719 |
Child(x=None, y=None) |
| 1720 |
[builtins fixtures/dataclasses.pyi] |
| 1721 |
|
| 1722 |
[case testDataclassGenericInheritanceSpecialCase1] |
| 1723 |
from dataclasses import dataclass |
| 1724 |
from typing import Generic, TypeVar, List |
| 1725 |
|
| 1726 |
T = TypeVar("T") |
| 1727 |
|
| 1728 |
@dataclass |
| 1729 |
class Parent(Generic[T]): |
| 1730 |
x: List[T] |
| 1731 |
|
| 1732 |
@dataclass |
| 1733 |
class Child1(Parent["Child2"]): ... |
| 1734 |
|
| 1735 |
@dataclass |
| 1736 |
class Child2(Parent["Child1"]): ... |
| 1737 |
|
| 1738 |
def f(c: Child2) -> None: |
| 1739 |
reveal_type(Child1([c]).x) # N: Revealed type is "builtins.list[__main__.Child2]" |
| 1740 |
|
| 1741 |
def g(c: Child1) -> None: |
| 1742 |
reveal_type(Child2([c]).x) # N: Revealed type is "builtins.list[__main__.Child1]" |
| 1743 |
[builtins fixtures/dataclasses.pyi] |
| 1744 |
|
| 1745 |
[case testDataclassGenericInheritanceSpecialCase2] |
| 1746 |
from dataclasses import dataclass |
| 1747 |
from typing import Generic, TypeVar |
| 1748 |
|
| 1749 |
T = TypeVar("T") |
| 1750 |
|
| 1751 |
# A subclass might be analyzed before base in import cycles. They are |
| 1752 |
# defined here in reversed order to simulate this. |
| 1753 |
|
| 1754 |
@dataclass |
| 1755 |
class Child1(Parent["Child2"]): |
| 1756 |
x: int |
| 1757 |
|
| 1758 |
@dataclass |
| 1759 |
class Child2(Parent["Child1"]): |
| 1760 |
y: int |
| 1761 |
|
| 1762 |
@dataclass |
| 1763 |
class Parent(Generic[T]): |
| 1764 |
key: str |
| 1765 |
|
| 1766 |
Child1(x=1, key='') |
| 1767 |
Child2(y=1, key='') |
| 1768 |
[builtins fixtures/dataclasses.pyi] |
| 1769 |
|
| 1770 |
[case testDataclassGenericWithBound] |
| 1771 |
from dataclasses import dataclass |
| 1772 |
from typing import Generic, TypeVar |
| 1773 |
|
| 1774 |
T = TypeVar("T", bound="C") |
| 1775 |
|
| 1776 |
@dataclass |
| 1777 |
class C(Generic[T]): |
| 1778 |
x: int |
| 1779 |
|
| 1780 |
c: C[C] |
| 1781 |
d: C[str] # E: Type argument "str" of "C" must be a subtype of "C[Any]" |
| 1782 |
C(x=2) |
| 1783 |
[builtins fixtures/dataclasses.pyi] |
| 1784 |
|
| 1785 |
[case testDataclassGenericBoundToInvalidTypeVarDoesNotCrash] |
| 1786 |
import dataclasses |
| 1787 |
from typing import Generic, TypeVar |
| 1788 |
|
| 1789 |
T = TypeVar("T", bound="NotDefined") # E: Name "NotDefined" is not defined |
| 1790 |
|
| 1791 |
@dataclasses.dataclass |
| 1792 |
class C(Generic[T]): |
| 1793 |
x: float |
| 1794 |
[builtins fixtures/dataclasses.pyi] |
| 1795 |
|
| 1796 |
[case testDataclassInitVarCannotBeSet] |
| 1797 |
from dataclasses import dataclass, InitVar |
| 1798 |
|
| 1799 |
@dataclass |
| 1800 |
class C: |
| 1801 |
x: InitVar[int] = 0 |
| 1802 |
y: InitVar[str] = '' |
| 1803 |
|
| 1804 |
def f(self) -> None: |
| 1805 |
# This works at runtime, but it seems like an abuse of the InitVar |
| 1806 |
# feature and thus we don't support it |
| 1807 |
self.x = 1 # E: "C" has no attribute "x" |
| 1808 |
self.y: str = 'x' # E: "C" has no attribute "y" |
| 1809 |
|
| 1810 |
c = C() |
| 1811 |
c2 = C(x=1) |
| 1812 |
c.x # E: "C" has no attribute "x" |
| 1813 |
[builtins fixtures/dataclasses.pyi] |
| 1814 |
|
| 1815 |
[case testDataclassCheckTypeVarBounds] |
| 1816 |
from dataclasses import dataclass |
| 1817 |
from typing import ClassVar, Protocol, Dict, TypeVar, Generic |
| 1818 |
|
| 1819 |
class DataclassProtocol(Protocol): |
| 1820 |
__dataclass_fields__: ClassVar[Dict] |
| 1821 |
|
| 1822 |
T = TypeVar("T", bound=DataclassProtocol) |
| 1823 |
|
| 1824 |
@dataclass |
| 1825 |
class MyDataclass: |
| 1826 |
x: int = 1 |
| 1827 |
|
| 1828 |
class MyGeneric(Generic[T]): ... |
| 1829 |
class MyClass(MyGeneric[MyDataclass]): ... |
| 1830 |
[builtins fixtures/dataclasses.pyi] |
| 1831 |
|
| 1832 |
[case testDataclassWithMatchArgs] |
| 1833 |
from dataclasses import dataclass |
| 1834 |
@dataclass |
| 1835 |
class One: |
| 1836 |
bar: int |
| 1837 |
baz: str |
| 1838 |
o: One |
| 1839 |
reveal_type(o.__match_args__) # N: Revealed type is "tuple[Literal['bar'], Literal['baz']]" |
| 1840 |
@dataclass(match_args=True) |
| 1841 |
class Two: |
| 1842 |
bar: int |
| 1843 |
t: Two |
| 1844 |
reveal_type(t.__match_args__) # N: Revealed type is "tuple[Literal['bar']]" |
| 1845 |
@dataclass |
| 1846 |
class Empty: |
| 1847 |
... |
| 1848 |
e: Empty |
| 1849 |
reveal_type(e.__match_args__) # N: Revealed type is "tuple[()]" |
| 1850 |
[builtins fixtures/dataclasses.pyi] |
| 1851 |
|
| 1852 |
[case testDataclassWithMatchArgsAndKwOnly] |
| 1853 |
from dataclasses import dataclass, field |
| 1854 |
@dataclass(kw_only=True) |
| 1855 |
class One: |
| 1856 |
a: int |
| 1857 |
b: str |
| 1858 |
reveal_type(One.__match_args__) # N: Revealed type is "tuple[()]" |
| 1859 |
|
| 1860 |
@dataclass(kw_only=True) |
| 1861 |
class Two: |
| 1862 |
a: int = field(kw_only=False) |
| 1863 |
b: str |
| 1864 |
reveal_type(Two.__match_args__) # N: Revealed type is "tuple[Literal['a']]" |
| 1865 |
[builtins fixtures/dataclasses.pyi] |
| 1866 |
|
| 1867 |
[case testDataclassWithoutMatchArgs] |
| 1868 |
from dataclasses import dataclass |
| 1869 |
@dataclass(match_args=False) |
| 1870 |
class One: |
| 1871 |
bar: int |
| 1872 |
baz: str |
| 1873 |
o: One |
| 1874 |
reveal_type(o.__match_args__) # E: "One" has no attribute "__match_args__" \ |
| 1875 |
# N: Revealed type is "Any" |
| 1876 |
[builtins fixtures/dataclasses.pyi] |
| 1877 |
|
| 1878 |
[case testFinalInDataclass] |
| 1879 |
from dataclasses import dataclass |
| 1880 |
from typing import Final |
| 1881 |
|
| 1882 |
@dataclass |
| 1883 |
class FirstClass: |
| 1884 |
FIRST_CONST: Final = 3 # OK |
| 1885 |
|
| 1886 |
@dataclass |
| 1887 |
class SecondClass: |
| 1888 |
SECOND_CONST: Final = FirstClass.FIRST_CONST # E: Need type argument for Final[...] with non-literal default in dataclass |
| 1889 |
|
| 1890 |
reveal_type(FirstClass().FIRST_CONST) # N: Revealed type is "Literal[3]?" |
| 1891 |
FirstClass().FIRST_CONST = 42 # E: Cannot assign to final attribute "FIRST_CONST" |
| 1892 |
reveal_type(SecondClass().SECOND_CONST) # N: Revealed type is "Literal[3]?" |
| 1893 |
SecondClass().SECOND_CONST = 42 # E: Cannot assign to final attribute "SECOND_CONST" |
| 1894 |
[builtins fixtures/dataclasses.pyi] |
| 1895 |
|
| 1896 |
[case testDataclassFieldsProtocol] |
| 1897 |
from dataclasses import dataclass |
| 1898 |
from typing import Any, Protocol |
| 1899 |
|
| 1900 |
class ConfigProtocol(Protocol): |
| 1901 |
__dataclass_fields__: dict[str, Any] |
| 1902 |
|
| 1903 |
def takes_cp(cp: ConfigProtocol): ... |
| 1904 |
|
| 1905 |
@dataclass |
| 1906 |
class MyDataclass: |
| 1907 |
x: int = 3 |
| 1908 |
|
| 1909 |
takes_cp(MyDataclass) |
| 1910 |
[builtins fixtures/dataclasses.pyi] |
| 1911 |
|
| 1912 |
[case testDataclassTypeAnnotationAliasUpdated] |
| 1913 |
import a |
| 1914 |
[file a.py] |
| 1915 |
from dataclasses import dataclass |
| 1916 |
from b import B |
| 1917 |
|
| 1918 |
@dataclass |
| 1919 |
class D: |
| 1920 |
x: B |
| 1921 |
|
| 1922 |
reveal_type(D) # N: Revealed type is "def (x: builtins.list[b.C]) -> a.D" |
| 1923 |
[file b.py] |
| 1924 |
from typing import List |
| 1925 |
import a |
| 1926 |
class CC: ... |
| 1927 |
class C(CC): ... |
| 1928 |
B = List[C] |
| 1929 |
[builtins fixtures/dataclasses.pyi] |
| 1930 |
|
| 1931 |
[case testDataclassSelfType] |
| 1932 |
from dataclasses import dataclass |
| 1933 |
from typing import Self, TypeVar, Generic, Optional |
| 1934 |
|
| 1935 |
T = TypeVar("T") |
| 1936 |
|
| 1937 |
@dataclass |
| 1938 |
class LinkedList(Generic[T]): |
| 1939 |
value: T |
| 1940 |
next: Optional[Self] = None |
| 1941 |
|
| 1942 |
def meth(self) -> None: |
| 1943 |
reveal_type(self.next) # N: Revealed type is "Self`0 | None" |
| 1944 |
|
| 1945 |
l_int: LinkedList[int] = LinkedList(1, LinkedList("no", None)) # E: Argument 1 to "LinkedList" has incompatible type "str"; expected "int" |
| 1946 |
|
| 1947 |
@dataclass |
| 1948 |
class SubLinkedList(LinkedList[int]): ... |
| 1949 |
|
| 1950 |
lst = SubLinkedList(1, LinkedList(2)) # E: Argument 2 to "SubLinkedList" has incompatible type "LinkedList[int]"; expected "SubLinkedList | None" |
| 1951 |
reveal_type(lst.next) # N: Revealed type is "__main__.SubLinkedList | None" |
| 1952 |
reveal_type(SubLinkedList) # N: Revealed type is "def (value: builtins.int, next: __main__.SubLinkedList | None =) -> __main__.SubLinkedList" |
| 1953 |
[builtins fixtures/dataclasses.pyi] |
| 1954 |
|
| 1955 |
[case testNoCrashOnNestedGenericCallable] |
| 1956 |
from dataclasses import dataclass |
| 1957 |
from typing import Generic, TypeVar, Callable |
| 1958 |
|
| 1959 |
T = TypeVar('T') |
| 1960 |
R = TypeVar('R') |
| 1961 |
X = TypeVar('X') |
| 1962 |
|
| 1963 |
@dataclass |
| 1964 |
class Box(Generic[T]): |
| 1965 |
inner: T |
| 1966 |
|
| 1967 |
@dataclass |
| 1968 |
class Cont(Generic[R]): |
| 1969 |
run: Box[Callable[[X], R]] |
| 1970 |
|
| 1971 |
def const_two(x: T) -> str: |
| 1972 |
return "two" |
| 1973 |
|
| 1974 |
c = Cont(Box(const_two)) |
| 1975 |
reveal_type(c) # N: Revealed type is "__main__.Cont[builtins.str]" |
| 1976 |
[builtins fixtures/dataclasses.pyi] |
| 1977 |
|
| 1978 |
[case testNoCrashOnSelfWithForwardRefGenericDataclass] |
| 1979 |
from typing import Generic, Sequence, TypeVar, Self |
| 1980 |
from dataclasses import dataclass |
| 1981 |
|
| 1982 |
_T = TypeVar('_T', bound="Foo") |
| 1983 |
|
| 1984 |
@dataclass |
| 1985 |
class Foo: |
| 1986 |
foo: int |
| 1987 |
|
| 1988 |
@dataclass |
| 1989 |
class Element(Generic[_T]): |
| 1990 |
elements: Sequence[Self] |
| 1991 |
|
| 1992 |
@dataclass |
| 1993 |
class Bar(Foo): ... |
| 1994 |
e: Element[Bar] |
| 1995 |
reveal_type(e.elements) # N: Revealed type is "typing.Sequence[__main__.Element[__main__.Bar]]" |
| 1996 |
[builtins fixtures/dataclasses.pyi] |
| 1997 |
|
| 1998 |
[case testIfConditionsInDefinition] |
| 1999 |
# flags: --python-version 3.11 --always-true TRUTH |
| 2000 |
from dataclasses import dataclass |
| 2001 |
from typing import TYPE_CHECKING |
| 2002 |
|
| 2003 |
TRUTH = False # Is set to --always-true |
| 2004 |
|
| 2005 |
@dataclass |
| 2006 |
class Foo: |
| 2007 |
if TYPE_CHECKING: |
| 2008 |
present_1: int |
| 2009 |
else: |
| 2010 |
skipped_1: int |
| 2011 |
if True: # Mypy does not know if it is True or False, so the block is used |
| 2012 |
present_2: int |
| 2013 |
if False: # Mypy does not know if it is True or False, so the block is used |
| 2014 |
present_3: int |
| 2015 |
if not TRUTH: |
| 2016 |
skipped_2: int |
| 2017 |
elif 123: |
| 2018 |
present_4: int |
| 2019 |
elif TRUTH: |
| 2020 |
present_5: int |
| 2021 |
else: |
| 2022 |
skipped_3: int |
| 2023 |
|
| 2024 |
Foo( |
| 2025 |
present_1=1, |
| 2026 |
present_2=2, |
| 2027 |
present_3=3, |
| 2028 |
present_4=4, |
| 2029 |
present_5=5, |
| 2030 |
) |
| 2031 |
|
| 2032 |
[builtins fixtures/dataclasses.pyi] |
| 2033 |
|
| 2034 |
[case testReplace] |
| 2035 |
from dataclasses import dataclass, replace, InitVar |
| 2036 |
from typing import ClassVar |
| 2037 |
|
| 2038 |
@dataclass |
| 2039 |
class A: |
| 2040 |
x: int |
| 2041 |
q: InitVar[int] |
| 2042 |
q2: InitVar[int] = 0 |
| 2043 |
c: ClassVar[int] |
| 2044 |
|
| 2045 |
|
| 2046 |
a = A(x=42, q=7) |
| 2047 |
a2 = replace(a) # E: Missing named argument "q" for "replace" of "A" |
| 2048 |
a2 = replace(a, q=42) |
| 2049 |
a2 = replace(a, x=42, q=42) |
| 2050 |
a2 = replace(a, x=42, q=42, c=7) # E: Unexpected keyword argument "c" for "replace" of "A" |
| 2051 |
a2 = replace(a, x='42', q=42) # E: Argument "x" to "replace" of "A" has incompatible type "str"; expected "int" |
| 2052 |
a2 = replace(a, q='42') # E: Argument "q" to "replace" of "A" has incompatible type "str"; expected "int" |
| 2053 |
reveal_type(a2) # N: Revealed type is "__main__.A" |
| 2054 |
|
| 2055 |
[builtins fixtures/tuple.pyi] |
| 2056 |
|
| 2057 |
[case testReplaceUnion] |
| 2058 |
from typing import Generic, Union, TypeVar |
| 2059 |
from dataclasses import dataclass, replace, InitVar |
| 2060 |
|
| 2061 |
T = TypeVar('T') |
| 2062 |
|
| 2063 |
@dataclass |
| 2064 |
class A(Generic[T]): |
| 2065 |
x: T # exercises meet(T=int, int) = int |
| 2066 |
y: bool # exercises meet(bool, int) = bool |
| 2067 |
z: str # exercises meet(str, bytes) = Never |
| 2068 |
w: dict # exercises meet(dict, Never) = Never |
| 2069 |
init_var: InitVar[int] # exercises (non-optional, optional) = non-optional |
| 2070 |
|
| 2071 |
@dataclass |
| 2072 |
class B: |
| 2073 |
x: int |
| 2074 |
y: int |
| 2075 |
z: bytes |
| 2076 |
init_var: int |
| 2077 |
|
| 2078 |
|
| 2079 |
a_or_b: Union[A[int], B] |
| 2080 |
_ = replace(a_or_b, x=42, y=True, init_var=42) |
| 2081 |
_ = replace(a_or_b, x=42, y=True) # E: Missing named argument "init_var" for "replace" of "A[int] | B" |
| 2082 |
_ = replace(a_or_b, x=42, y=True, z='42', init_var=42) # E: Argument "z" to "replace" of "A[int] | B" has incompatible type "str"; expected "Never" |
| 2083 |
_ = replace(a_or_b, x=42, y=True, w={}, init_var=42) # E: Argument "w" to "replace" of "A[int] | B" has incompatible type "dict[Never, Never]"; expected "Never" |
| 2084 |
_ = replace(a_or_b, y=42, init_var=42) # E: Argument "y" to "replace" of "A[int] | B" has incompatible type "int"; expected "bool" |
| 2085 |
|
| 2086 |
[builtins fixtures/tuple.pyi] |
| 2087 |
|
| 2088 |
[case testReplaceUnionOfTypeVar] |
| 2089 |
from typing import Generic, Union, TypeVar |
| 2090 |
from dataclasses import dataclass, replace |
| 2091 |
|
| 2092 |
@dataclass |
| 2093 |
class A: |
| 2094 |
x: int |
| 2095 |
y: int |
| 2096 |
z: str |
| 2097 |
w: dict |
| 2098 |
|
| 2099 |
class B: |
| 2100 |
pass |
| 2101 |
|
| 2102 |
TA = TypeVar('TA', bound=A) |
| 2103 |
TB = TypeVar('TB', bound=B) |
| 2104 |
|
| 2105 |
def f(b_or_t: Union[TA, TB, int]) -> None: |
| 2106 |
a2 = replace(b_or_t) # E: Value of type variable "_DataclassT" of "replace" cannot be "TA | TB | int" |
| 2107 |
|
| 2108 |
[builtins fixtures/tuple.pyi] |
| 2109 |
|
| 2110 |
[case testReplaceTypeVarBoundNotDataclass] |
| 2111 |
from dataclasses import dataclass, replace |
| 2112 |
from typing import Union, TypeVar |
| 2113 |
|
| 2114 |
TInt = TypeVar('TInt', bound=int) |
| 2115 |
TAny = TypeVar('TAny') |
| 2116 |
TNone = TypeVar('TNone', bound=None) |
| 2117 |
TUnion = TypeVar('TUnion', bound=Union[str, int]) |
| 2118 |
|
| 2119 |
def f1(t: TInt) -> None: |
| 2120 |
_ = replace(t, x=42) # E: Value of type variable "_DataclassT" of "replace" cannot be "TInt" |
| 2121 |
|
| 2122 |
def f2(t: TAny) -> TAny: |
| 2123 |
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TAny" |
| 2124 |
|
| 2125 |
def f3(t: TNone) -> TNone: |
| 2126 |
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TNone" |
| 2127 |
|
| 2128 |
def f4(t: TUnion) -> TUnion: |
| 2129 |
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TUnion" |
| 2130 |
|
| 2131 |
[builtins fixtures/tuple.pyi] |
| 2132 |
|
| 2133 |
[case testReplaceTypeVarBound] |
| 2134 |
from dataclasses import dataclass, replace |
| 2135 |
from typing import TypeVar |
| 2136 |
|
| 2137 |
@dataclass |
| 2138 |
class A: |
| 2139 |
x: int |
| 2140 |
|
| 2141 |
@dataclass |
| 2142 |
class B(A): |
| 2143 |
pass |
| 2144 |
|
| 2145 |
TA = TypeVar('TA', bound=A) |
| 2146 |
|
| 2147 |
def f(t: TA) -> TA: |
| 2148 |
t2 = replace(t, x=42) |
| 2149 |
reveal_type(t2) # N: Revealed type is "TA`-1" |
| 2150 |
_ = replace(t, x='42') # E: Argument "x" to "replace" of "TA" has incompatible type "str"; expected "int" |
| 2151 |
return t2 |
| 2152 |
|
| 2153 |
f(A(x=42)) |
| 2154 |
f(B(x=42)) |
| 2155 |
|
| 2156 |
[builtins fixtures/tuple.pyi] |
| 2157 |
|
| 2158 |
[case testReplaceAny] |
| 2159 |
from dataclasses import replace |
| 2160 |
from typing import Any |
| 2161 |
|
| 2162 |
a: Any |
| 2163 |
a2 = replace(a) |
| 2164 |
reveal_type(a2) # N: Revealed type is "Any" |
| 2165 |
|
| 2166 |
[builtins fixtures/tuple.pyi] |
| 2167 |
|
| 2168 |
[case testReplaceNotDataclass] |
| 2169 |
from dataclasses import replace |
| 2170 |
|
| 2171 |
replace(5) # E: Value of type variable "_DataclassT" of "replace" cannot be "int" |
| 2172 |
|
| 2173 |
class C: |
| 2174 |
pass |
| 2175 |
|
| 2176 |
replace(C()) # E: Value of type variable "_DataclassT" of "replace" cannot be "C" |
| 2177 |
|
| 2178 |
replace(None) # E: Value of type variable "_DataclassT" of "replace" cannot be "None" |
| 2179 |
|
| 2180 |
[builtins fixtures/tuple.pyi] |
| 2181 |
|
| 2182 |
[case testReplaceIsDataclass] |
| 2183 |
from dataclasses import is_dataclass, replace |
| 2184 |
|
| 2185 |
def f(x: object) -> None: |
| 2186 |
_ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object" |
| 2187 |
if is_dataclass(x): |
| 2188 |
_ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "DataclassInstance | type[DataclassInstance]" |
| 2189 |
if not isinstance(x, type): |
| 2190 |
_ = replace(x) |
| 2191 |
|
| 2192 |
[builtins fixtures/tuple.pyi] |
| 2193 |
|
| 2194 |
[case testReplaceGeneric] |
| 2195 |
from dataclasses import dataclass, replace, InitVar |
| 2196 |
from typing import ClassVar, Generic, TypeVar |
| 2197 |
|
| 2198 |
T = TypeVar('T') |
| 2199 |
|
| 2200 |
@dataclass |
| 2201 |
class A(Generic[T]): |
| 2202 |
x: T |
| 2203 |
|
| 2204 |
a = A(x=42) |
| 2205 |
reveal_type(a) # N: Revealed type is "__main__.A[builtins.int]" |
| 2206 |
a2 = replace(a, x=42) |
| 2207 |
reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]" |
| 2208 |
a2 = replace(a, x='42') # E: Argument "x" to "replace" of "A[int]" has incompatible type "str"; expected "int" |
| 2209 |
reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]" |
| 2210 |
|
| 2211 |
[builtins fixtures/tuple.pyi] |
| 2212 |
|
| 2213 |
[case testPostInitNotMethod] |
| 2214 |
def __post_init__() -> None: |
| 2215 |
pass |
| 2216 |
|
| 2217 |
[case testPostInitCorrectSignature] |
| 2218 |
from typing import Any, Generic, TypeVar, Callable, Self |
| 2219 |
from dataclasses import dataclass, InitVar |
| 2220 |
|
| 2221 |
@dataclass |
| 2222 |
class Test1: |
| 2223 |
x: int |
| 2224 |
def __post_init__(self) -> None: ... |
| 2225 |
|
| 2226 |
@dataclass |
| 2227 |
class Test2: |
| 2228 |
x: int |
| 2229 |
y: InitVar[int] |
| 2230 |
z: str |
| 2231 |
def __post_init__(self, y: int) -> None: ... |
| 2232 |
|
| 2233 |
@dataclass |
| 2234 |
class Test3: |
| 2235 |
x: InitVar[int] |
| 2236 |
y: InitVar[str] |
| 2237 |
def __post_init__(self, x: int, y: str) -> None: ... |
| 2238 |
|
| 2239 |
@dataclass |
| 2240 |
class Test4: |
| 2241 |
x: int |
| 2242 |
y: InitVar[str] |
| 2243 |
z: InitVar[bool] = True |
| 2244 |
def __post_init__(self, y: str, z: bool) -> None: ... |
| 2245 |
|
| 2246 |
@dataclass |
| 2247 |
class Test5: |
| 2248 |
y: InitVar[str] = 'a' |
| 2249 |
z: InitVar[bool] = True |
| 2250 |
def __post_init__(self, y: str = 'a', z: bool = True) -> None: ... |
| 2251 |
|
| 2252 |
F = TypeVar('F', bound=Callable[..., Any]) |
| 2253 |
def identity(f: F) -> F: return f |
| 2254 |
|
| 2255 |
@dataclass |
| 2256 |
class Test6: |
| 2257 |
y: InitVar[str] |
| 2258 |
@identity # decorated method works |
| 2259 |
def __post_init__(self, y: str) -> None: ... |
| 2260 |
|
| 2261 |
T = TypeVar('T') |
| 2262 |
|
| 2263 |
@dataclass |
| 2264 |
class Test7(Generic[T]): |
| 2265 |
t: InitVar[T] |
| 2266 |
def __post_init__(self, t: T) -> None: ... |
| 2267 |
|
| 2268 |
@dataclass |
| 2269 |
class Test8: |
| 2270 |
s: InitVar[Self] |
| 2271 |
def __post_init__(self, s: Self) -> None: ... |
| 2272 |
[builtins fixtures/dataclasses.pyi] |
| 2273 |
|
| 2274 |
[case testPostInitSubclassing] |
| 2275 |
from dataclasses import dataclass, InitVar |
| 2276 |
|
| 2277 |
@dataclass |
| 2278 |
class Base: |
| 2279 |
a: str |
| 2280 |
x: InitVar[int] |
| 2281 |
def __post_init__(self, x: int) -> None: ... |
| 2282 |
|
| 2283 |
@dataclass |
| 2284 |
class Child(Base): |
| 2285 |
b: str |
| 2286 |
y: InitVar[str] |
| 2287 |
def __post_init__(self, x: int, y: str) -> None: ... |
| 2288 |
|
| 2289 |
@dataclass |
| 2290 |
class GrandChild(Child): |
| 2291 |
c: int |
| 2292 |
z: InitVar[str] = "a" |
| 2293 |
def __post_init__(self, x: int, y: str, z: str) -> None: ... |
| 2294 |
[builtins fixtures/dataclasses.pyi] |
| 2295 |
|
| 2296 |
[case testPostInitNotADataclassCheck] |
| 2297 |
from dataclasses import dataclass, InitVar |
| 2298 |
|
| 2299 |
class Regular: |
| 2300 |
__post_init__ = 1 # can be whatever |
| 2301 |
|
| 2302 |
class Base: |
| 2303 |
x: InitVar[int] |
| 2304 |
def __post_init__(self) -> None: ... # can be whatever |
| 2305 |
|
| 2306 |
@dataclass |
| 2307 |
class Child(Base): |
| 2308 |
y: InitVar[str] |
| 2309 |
def __post_init__(self, y: str) -> None: ... |
| 2310 |
[builtins fixtures/dataclasses.pyi] |
| 2311 |
|
| 2312 |
[case testPostInitMissingParam] |
| 2313 |
from dataclasses import dataclass, InitVar |
| 2314 |
|
| 2315 |
@dataclass |
| 2316 |
class Child: |
| 2317 |
y: InitVar[str] |
| 2318 |
def __post_init__(self) -> None: ... |
| 2319 |
[builtins fixtures/dataclasses.pyi] |
| 2320 |
[out] |
| 2321 |
main:6: error: Signature of "__post_init__" incompatible with supertype "dataclass" |
| 2322 |
main:6: note: Superclass: |
| 2323 |
main:6: note: def __post_init__(self: Child, y: str) -> None |
| 2324 |
main:6: note: Subclass: |
| 2325 |
main:6: note: def __post_init__(self: Child) -> None |
| 2326 |
|
| 2327 |
[case testPostInitWrongTypeAndName] |
| 2328 |
from dataclasses import dataclass, InitVar |
| 2329 |
|
| 2330 |
@dataclass |
| 2331 |
class Test1: |
| 2332 |
y: InitVar[str] |
| 2333 |
def __post_init__(self, x: int) -> None: ... # E: Argument 2 of "__post_init__" is incompatible with supertype "dataclass"; supertype defines the argument type as "str" |
| 2334 |
|
| 2335 |
@dataclass |
| 2336 |
class Test2: |
| 2337 |
y: InitVar[str] = 'a' |
| 2338 |
def __post_init__(self, x: int) -> None: ... # E: Argument 2 of "__post_init__" is incompatible with supertype "dataclass"; supertype defines the argument type as "str" |
| 2339 |
[builtins fixtures/dataclasses.pyi] |
| 2340 |
|
| 2341 |
[case testPostInitExtraParam] |
| 2342 |
from dataclasses import dataclass, InitVar |
| 2343 |
|
| 2344 |
@dataclass |
| 2345 |
class Child: |
| 2346 |
y: InitVar[str] |
| 2347 |
def __post_init__(self, y: str, z: int) -> None: ... |
| 2348 |
[builtins fixtures/dataclasses.pyi] |
| 2349 |
[out] |
| 2350 |
main:6: error: Signature of "__post_init__" incompatible with supertype "dataclass" |
| 2351 |
main:6: note: Superclass: |
| 2352 |
main:6: note: def __post_init__(self: Child, y: str) -> None |
| 2353 |
main:6: note: Subclass: |
| 2354 |
main:6: note: def __post_init__(self: Child, y: str, z: int) -> None |
| 2355 |
|
| 2356 |
[case testPostInitReturnType] |
| 2357 |
from dataclasses import dataclass, InitVar |
| 2358 |
|
| 2359 |
@dataclass |
| 2360 |
class Child: |
| 2361 |
y: InitVar[str] |
| 2362 |
def __post_init__(self, y: str) -> int: ... # E: Return type "int" of "__post_init__" incompatible with return type "None" in supertype "dataclass" |
| 2363 |
[builtins fixtures/dataclasses.pyi] |
| 2364 |
|
| 2365 |
[case testPostInitDecoratedMethodError] |
| 2366 |
from dataclasses import dataclass, InitVar |
| 2367 |
from typing import Any, Callable, TypeVar |
| 2368 |
|
| 2369 |
F = TypeVar('F', bound=Callable[..., Any]) |
| 2370 |
def identity(f: F) -> F: return f |
| 2371 |
|
| 2372 |
@dataclass |
| 2373 |
class Klass: |
| 2374 |
y: InitVar[str] |
| 2375 |
@identity |
| 2376 |
def __post_init__(self) -> None: ... |
| 2377 |
[builtins fixtures/dataclasses.pyi] |
| 2378 |
[out] |
| 2379 |
main:11: error: Signature of "__post_init__" incompatible with supertype "dataclass" |
| 2380 |
main:11: note: Superclass: |
| 2381 |
main:11: note: def __post_init__(self: Klass, y: str) -> None |
| 2382 |
main:11: note: Subclass: |
| 2383 |
main:11: note: def __post_init__(self: Klass) -> None |
| 2384 |
|
| 2385 |
[case testPostInitIsNotAFunction] |
| 2386 |
from dataclasses import dataclass, InitVar |
| 2387 |
|
| 2388 |
@dataclass |
| 2389 |
class Test: |
| 2390 |
y: InitVar[str] |
| 2391 |
__post_init__ = 1 # E: "__post_init__" method must be an instance method |
| 2392 |
[builtins fixtures/dataclasses.pyi] |
| 2393 |
|
| 2394 |
[case testPostInitClassMethod] |
| 2395 |
from dataclasses import dataclass, InitVar |
| 2396 |
|
| 2397 |
@dataclass |
| 2398 |
class Test: |
| 2399 |
y: InitVar[str] |
| 2400 |
@classmethod |
| 2401 |
def __post_init__(cls) -> None: ... |
| 2402 |
[builtins fixtures/dataclasses.pyi] |
| 2403 |
[out] |
| 2404 |
main:7: error: Signature of "__post_init__" incompatible with supertype "dataclass" |
| 2405 |
main:7: note: Superclass: |
| 2406 |
main:7: note: def __post_init__(self: Test, y: str) -> None |
| 2407 |
main:7: note: Subclass: |
| 2408 |
main:7: note: @classmethod |
| 2409 |
main:7: note: def __post_init__(cls: type[Test]) -> None |
| 2410 |
|
| 2411 |
[case testPostInitStaticMethod] |
| 2412 |
from dataclasses import dataclass, InitVar |
| 2413 |
|
| 2414 |
@dataclass |
| 2415 |
class Test: |
| 2416 |
y: InitVar[str] |
| 2417 |
@staticmethod |
| 2418 |
def __post_init__() -> None: ... |
| 2419 |
[builtins fixtures/dataclasses.pyi] |
| 2420 |
[out] |
| 2421 |
main:7: error: Signature of "__post_init__" incompatible with supertype "dataclass" |
| 2422 |
main:7: note: Superclass: |
| 2423 |
main:7: note: def __post_init__(self: Test, y: str) -> None |
| 2424 |
main:7: note: Subclass: |
| 2425 |
main:7: note: @staticmethod |
| 2426 |
main:7: note: def __post_init__() -> None |
| 2427 |
|
| 2428 |
[case testProtocolNoCrash] |
| 2429 |
from typing import Protocol, Union, ClassVar |
| 2430 |
from dataclasses import dataclass, field |
| 2431 |
|
| 2432 |
DEFAULT = 0 |
| 2433 |
|
| 2434 |
@dataclass |
| 2435 |
class Test(Protocol): |
| 2436 |
x: int |
| 2437 |
def reset(self) -> None: |
| 2438 |
self.x = DEFAULT |
| 2439 |
[builtins fixtures/dataclasses.pyi] |
| 2440 |
|
| 2441 |
[case testProtocolNoCrashOnJoining] |
| 2442 |
from dataclasses import dataclass |
| 2443 |
from typing import Protocol |
| 2444 |
|
| 2445 |
@dataclass |
| 2446 |
class MyDataclass(Protocol): ... |
| 2447 |
|
| 2448 |
a: MyDataclass |
| 2449 |
b = [a, a] # trigger joining the types |
| 2450 |
|
| 2451 |
[builtins fixtures/dataclasses.pyi] |
| 2452 |
|
| 2453 |
[case testPropertyAndFieldRedefinitionNoCrash] |
| 2454 |
from dataclasses import dataclass |
| 2455 |
|
| 2456 |
@dataclass |
| 2457 |
class Foo: |
| 2458 |
@property |
| 2459 |
def c(self) -> int: |
| 2460 |
return 0 |
| 2461 |
|
| 2462 |
c: int # E: Name "c" already defined on line 5 |
| 2463 |
[builtins fixtures/dataclasses.pyi] |
| 2464 |
|
| 2465 |
[case testDataclassInheritanceWorksWithExplicitOverrides] |
| 2466 |
# flags: --enable-error-code explicit-override |
| 2467 |
from dataclasses import dataclass |
| 2468 |
|
| 2469 |
@dataclass |
| 2470 |
class Base: |
| 2471 |
x: int |
| 2472 |
|
| 2473 |
@dataclass |
| 2474 |
class Child(Base): |
| 2475 |
y: int |
| 2476 |
[builtins fixtures/dataclasses.pyi] |
| 2477 |
|
| 2478 |
[case testDataclassesInitVarsWithProperty] |
| 2479 |
from dataclasses import InitVar, dataclass, field |
| 2480 |
|
| 2481 |
@dataclass |
| 2482 |
class Test: |
| 2483 |
foo: InitVar[str] |
| 2484 |
_foo: str = field(init=False) |
| 2485 |
|
| 2486 |
def __post_init__(self, foo: str) -> None: |
| 2487 |
self._foo = foo |
| 2488 |
|
| 2489 |
@property |
| 2490 |
def foo(self) -> str: |
| 2491 |
return self._foo |
| 2492 |
|
| 2493 |
@foo.setter |
| 2494 |
def foo(self, value: str) -> None: |
| 2495 |
self._foo = value |
| 2496 |
|
| 2497 |
reveal_type(Test) # N: Revealed type is "def (foo: builtins.str) -> __main__.Test" |
| 2498 |
test = Test(42) # E: Argument 1 to "Test" has incompatible type "int"; expected "str" |
| 2499 |
test = Test("foo") |
| 2500 |
test.foo |
| 2501 |
[builtins fixtures/dataclasses.pyi] |
| 2502 |
|
| 2503 |
[case testDataclassesDefaultValueInitVarWithProperty] |
| 2504 |
from dataclasses import InitVar, dataclass, field |
| 2505 |
|
| 2506 |
@dataclass |
| 2507 |
class Test: |
| 2508 |
foo: InitVar[str] = "foo" |
| 2509 |
_foo: str = field(init=False) |
| 2510 |
|
| 2511 |
def __post_init__(self, foo: str) -> None: |
| 2512 |
self._foo = foo |
| 2513 |
|
| 2514 |
@property # E: InitVar with default value cannot be redefined |
| 2515 |
def foo(self) -> str: |
| 2516 |
return self._foo |
| 2517 |
|
| 2518 |
[builtins fixtures/dataclasses.pyi] |
| 2519 |
|
| 2520 |
[case testDataclassesWithProperty] |
| 2521 |
from dataclasses import dataclass |
| 2522 |
|
| 2523 |
@dataclass |
| 2524 |
class Test: |
| 2525 |
@property |
| 2526 |
def foo(self) -> str: |
| 2527 |
return "a" |
| 2528 |
|
| 2529 |
@foo.setter |
| 2530 |
def foo(self, value: str) -> None: |
| 2531 |
pass |
| 2532 |
[builtins fixtures/dataclasses.pyi] |
| 2533 |
|
| 2534 |
[case testDataclassInheritanceWorksWithExplicitOverridesAndOrdering] |
| 2535 |
# flags: --enable-error-code explicit-override |
| 2536 |
from dataclasses import dataclass |
| 2537 |
|
| 2538 |
@dataclass(order=True) |
| 2539 |
class Base: |
| 2540 |
x: int |
| 2541 |
|
| 2542 |
@dataclass(order=True) |
| 2543 |
class Child(Base): |
| 2544 |
y: int |
| 2545 |
[builtins fixtures/dataclasses.pyi] |
| 2546 |
|
| 2547 |
[case testDunderReplacePresent] |
| 2548 |
# flags: --python-version 3.13 |
| 2549 |
from dataclasses import dataclass, field |
| 2550 |
|
| 2551 |
@dataclass |
| 2552 |
class Coords: |
| 2553 |
x: int |
| 2554 |
y: int |
| 2555 |
# non-init fields are not allowed with replace: |
| 2556 |
z: int = field(init=False) |
| 2557 |
|
| 2558 |
|
| 2559 |
replaced = Coords(2, 4).__replace__(x=2, y=5) |
| 2560 |
reveal_type(replaced) # N: Revealed type is "__main__.Coords" |
| 2561 |
|
| 2562 |
replaced = Coords(2, 4).__replace__(x=2) |
| 2563 |
reveal_type(replaced) # N: Revealed type is "__main__.Coords" |
| 2564 |
|
| 2565 |
Coords(2, 4).__replace__(x="asdf") # E: Argument "x" to "__replace__" of "Coords" has incompatible type "str"; expected "int" |
| 2566 |
Coords(2, 4).__replace__(23) # E: Too many positional arguments for "__replace__" of "Coords" |
| 2567 |
Coords(2, 4).__replace__(23, 25) # E: Too many positional arguments for "__replace__" of "Coords" |
| 2568 |
Coords(2, 4).__replace__(x=23, y=25, z=42) # E: Unexpected keyword argument "z" for "__replace__" of "Coords" |
| 2569 |
|
| 2570 |
from typing import Generic, TypeVar |
| 2571 |
T = TypeVar('T') |
| 2572 |
|
| 2573 |
@dataclass |
| 2574 |
class Gen(Generic[T]): |
| 2575 |
x: T |
| 2576 |
|
| 2577 |
replaced_2 = Gen(2).__replace__(x=2) |
| 2578 |
reveal_type(replaced_2) # N: Revealed type is "__main__.Gen[builtins.int]" |
| 2579 |
Gen(2).__replace__(x="not an int") # E: Argument "x" to "__replace__" of "Gen" has incompatible type "str"; expected "int" |
| 2580 |
|
| 2581 |
[builtins fixtures/tuple.pyi] |
| 2582 |
|
| 2583 |
[case testDunderReplaceCovariantOverride] |
| 2584 |
# flags: --python-version 3.13 --enable-error-code mutable-override |
| 2585 |
from dataclasses import dataclass |
| 2586 |
from typing import Optional |
| 2587 |
from typing_extensions import dataclass_transform |
| 2588 |
|
| 2589 |
@dataclass |
| 2590 |
class Base: |
| 2591 |
a: Optional[int] |
| 2592 |
|
| 2593 |
@dataclass |
| 2594 |
class Child(Base): |
| 2595 |
a: int # E: Covariant override of a mutable attribute (base class "Base" defined the type as "int | None", expression has type "int") |
| 2596 |
|
| 2597 |
@dataclass |
| 2598 |
class Other(Base): |
| 2599 |
a: str # E: Incompatible types in assignment (expression has type "str", base class "Base" defined the type as "int | None") |
| 2600 |
|
| 2601 |
@dataclass_transform(kw_only_default=True) |
| 2602 |
class DCMeta(type): ... |
| 2603 |
|
| 2604 |
class X(metaclass=DCMeta): |
| 2605 |
a: Optional[int] |
| 2606 |
|
| 2607 |
class Y(X): |
| 2608 |
a: int # E: Covariant override of a mutable attribute (base class "X" defined the type as "int | None", expression has type "int") |
| 2609 |
[builtins fixtures/tuple.pyi] |
| 2610 |
|
| 2611 |
|
| 2612 |
[case testFrozenWithFinal] |
| 2613 |
from dataclasses import dataclass |
| 2614 |
from typing import Final |
| 2615 |
|
| 2616 |
@dataclass(frozen=True) |
| 2617 |
class My: |
| 2618 |
a: Final = 1 |
| 2619 |
b: Final[int] = 2 |
| 2620 |
|
| 2621 |
reveal_type(My.a) # N: Revealed type is "Literal[1]?" |
| 2622 |
reveal_type(My.b) # N: Revealed type is "builtins.int" |
| 2623 |
My.a = 1 # E: Cannot assign to final attribute "a" |
| 2624 |
My.b = 2 # E: Cannot assign to final attribute "b" |
| 2625 |
|
| 2626 |
m = My() |
| 2627 |
reveal_type(m.a) # N: Revealed type is "Literal[1]?" |
| 2628 |
reveal_type(m.b) # N: Revealed type is "builtins.int" |
| 2629 |
|
| 2630 |
m.a = 1 # E: Cannot assign to final attribute "a" |
| 2631 |
m.b = 2 # E: Cannot assign to final attribute "b" |
| 2632 |
[builtins fixtures/tuple.pyi] |
| 2633 |
|
| 2634 |
[case testNoCrashForDataclassNamedTupleCombination] |
| 2635 |
# flags: --python-version 3.13 |
| 2636 |
from dataclasses import dataclass |
| 2637 |
from typing import NamedTuple |
| 2638 |
|
| 2639 |
@dataclass |
| 2640 |
class A(NamedTuple): # E: A NamedTuple cannot be a dataclass |
| 2641 |
i: int |
| 2642 |
|
| 2643 |
class B1(NamedTuple): |
| 2644 |
i: int |
| 2645 |
@dataclass |
| 2646 |
class B2(B1): # E: A NamedTuple cannot be a dataclass |
| 2647 |
pass |
| 2648 |
|
| 2649 |
[builtins fixtures/tuple.pyi] |
| 2650 |
|
| 2651 |
[case testDataclassesTypeGuard] |
| 2652 |
import dataclasses |
| 2653 |
|
| 2654 |
raw_target: object |
| 2655 |
|
| 2656 |
if isinstance(raw_target, type) and dataclasses.is_dataclass(raw_target): |
| 2657 |
reveal_type(raw_target) # N: Revealed type is "type[dataclasses.DataclassInstance]" |
| 2658 |
[builtins fixtures/tuple.pyi] |
| 2659 |
|
| 2660 |
[case testDataclassKwOnlyArgsLast] |
| 2661 |
from dataclasses import dataclass, field |
| 2662 |
|
| 2663 |
@dataclass |
| 2664 |
class User: |
| 2665 |
id: int = field(kw_only=True) |
| 2666 |
name: str |
| 2667 |
|
| 2668 |
User("Foo", id=0) |
| 2669 |
[builtins fixtures/tuple.pyi] |
| 2670 |
|
| 2671 |
[case testDataclassKwOnlyArgsDefaultAllowedNonLast] |
| 2672 |
from dataclasses import dataclass, field |
| 2673 |
|
| 2674 |
@dataclass |
| 2675 |
class User: |
| 2676 |
id: int = field(kw_only=True, default=0) |
| 2677 |
name: str |
| 2678 |
|
| 2679 |
User() # E: Missing positional argument "name" in call to "User" |
| 2680 |
User("") |
| 2681 |
User(0) # E: Argument 1 to "User" has incompatible type "int"; expected "str" |
| 2682 |
User("", 0) # E: Too many positional arguments for "User" |
| 2683 |
User("", id=0) |
| 2684 |
User("", name="") # E: "User" gets multiple values for keyword argument "name" |
| 2685 |
[builtins fixtures/tuple.pyi] |
| 2686 |
|
| 2687 |
[case testDataclassDefaultFactoryTypedDict] |
| 2688 |
from dataclasses import dataclass, field |
| 2689 |
from mypy_extensions import TypedDict |
| 2690 |
|
| 2691 |
class Person(TypedDict, total=False): |
| 2692 |
name: str |
| 2693 |
|
| 2694 |
@dataclass |
| 2695 |
class Job: |
| 2696 |
person: Person = field(default_factory=Person) |
| 2697 |
|
| 2698 |
class PersonBad(TypedDict): |
| 2699 |
name: str |
| 2700 |
|
| 2701 |
@dataclass |
| 2702 |
class JobBad: |
| 2703 |
person: PersonBad = field(default_factory=PersonBad) # E: Argument "default_factory" to "field" has incompatible type "type[PersonBad]"; expected "Callable[[], PersonBad]" |
| 2704 |
[builtins fixtures/dict.pyi] |
| 2705 |
|
| 2706 |
[case testDataclassInitVarRedefinitionNoCrash] |
| 2707 |
# https://github.com/python/mypy/issues/19443 |
| 2708 |
from dataclasses import InitVar, dataclass |
| 2709 |
|
| 2710 |
class ClassA: |
| 2711 |
def value(self) -> int: |
| 2712 |
return 0 |
| 2713 |
|
| 2714 |
@dataclass |
| 2715 |
class ClassB(ClassA): |
| 2716 |
value: InitVar[int] |
| 2717 |
|
| 2718 |
def value(self) -> int: |
| 2719 |
return 0 |
| 2720 |
[builtins fixtures/dict.pyi] |
| 2721 |
|
| 2722 |
[case testDataclassAllowsFinalField] |
| 2723 |
# flags: --python-version=3.13 |
| 2724 |
from dataclasses import InitVar, dataclass |
| 2725 |
from typing import Final, ClassVar |
| 2726 |
|
| 2727 |
@dataclass |
| 2728 |
class C: |
| 2729 |
x: int |
| 2730 |
y: Final[int] |
| 2731 |
z: ClassVar[Final[int]] = 4 |
| 2732 |
|
| 2733 |
c = C(1, 2) |
| 2734 |
c.y = 3 # E: Cannot assign to final attribute "y" |
| 2735 |
|
| 2736 |
@dataclass |
| 2737 |
class D(C): |
| 2738 |
x: Final[int] # E: Cannot override writable attribute "x" with a final one |
| 2739 |
|
| 2740 |
@dataclass |
| 2741 |
class Bad1: |
| 2742 |
x: Final[InitVar[int]] # E: InitVar cannot be final |
| 2743 |
|
| 2744 |
@dataclass |
| 2745 |
class Bad2: |
| 2746 |
x: InitVar[Final[int]] # E: Final can be only used as an outermost qualifier in a variable annotation |
| 2747 |
[builtins fixtures/dict.pyi] |