[ty] Add tests for dataclass fields annotated with `Final`

This commit is contained in:
David Peter 2025-07-08 13:23:19 +02:00
parent e16473d260
commit b92a283f35
1 changed files with 23 additions and 0 deletions

View File

@ -444,6 +444,29 @@ To do
To do
## `Final` fields
Dataclass fields can be annotated with `Final`, which means that the field cannot be reassigned
after the instance is created. Fields that are additionally annotated with `ClassVar` are not part
of the `__init__` signature.
```py
from dataclasses import dataclass
from typing import Final, ClassVar
@dataclass
class C:
instance_variable: Final[int] = 1
class_variable1: ClassVar[Final[int]] = 1
class_variable2: ClassVar[Final[int]] = 1
reveal_type(C.__init__) # revealed: (self: C, instance_variable: int = Literal[1]) -> None
c = C()
# TODO: this should be an error
c.instance_variable = 2
```
## Inheritance
### Normal class inheriting from a dataclass