[ty] Add tests for dataclass fields annotated with Final (#19202)

## Summary

Adds some tests for dataclass fields that are annotated with `Final`
(see comment
[here](https://github.com/astral-sh/ruff/pull/15768#issuecomment-3044737645)).
Turns out that nothing is needed here, everything already works as
expected (apart from the fact that we can assign to `Final` fields,
which is tracked in https://github.com/astral-sh/ty/issues/158

## Test Plan

New Markdown tests
This commit is contained in:
David Peter 2025-07-08 14:33:46 +02:00 committed by GitHub
parent 6d8c84bde9
commit 9a4b85d845
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 0 deletions

View file

@ -444,6 +444,33 @@ 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:
# a `Final` annotation without a right-hand side is not allowed in normal classes,
# but valid for dataclasses. The field will be initialized in the synthesized
# `__init__` method
instance_variable_no_default: Final[int]
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_no_default: int, instance_variable: int = Literal[1]) -> None
c = C(1)
# TODO: this should be an error
c.instance_variable = 2
```
## Inheritance
### Normal class inheriting from a dataclass

View file

@ -162,6 +162,10 @@ from typing import Final
# TODO: This should be an error
NO_RHS: Final
class C:
# TODO: This should be an error
NO_RHS: Final
```
[`typing.final`]: https://docs.python.org/3/library/typing.html#typing.Final