mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 07:04:37 +00:00
[ty] Make dataclass instances adhere to DataclassInstance (#18115)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
## Summary Make dataclass instances adhere to the `DataclassInstance` protocol. fixes astral-sh/ty#400 ## Test Plan New Markdown tests
This commit is contained in:
parent
57617031de
commit
279dac1c0e
3 changed files with 41 additions and 20 deletions
|
@ -618,21 +618,43 @@ To do
|
|||
|
||||
## `dataclass.fields`
|
||||
|
||||
Dataclasses have `__dataclass_fields__` in them, which makes them a subtype of the
|
||||
`DataclassInstance` protocol.
|
||||
|
||||
Here, we verify that dataclasses can be passed to `dataclasses.fields` without any errors, and that
|
||||
the return type of `dataclasses.fields` is correct.
|
||||
Dataclasses have a special `__dataclass_fields__` class variable member. The `DataclassInstance`
|
||||
protocol checks for the presence of this attribute. It is used in the `dataclasses.fields` and
|
||||
`dataclasses.asdict` functions, for example:
|
||||
|
||||
```py
|
||||
from dataclasses import dataclass, fields
|
||||
from dataclasses import dataclass, fields, asdict
|
||||
|
||||
@dataclass
|
||||
class Foo:
|
||||
x: int
|
||||
|
||||
reveal_type(Foo.__dataclass_fields__) # revealed: dict[str, Field[Any]]
|
||||
foo = Foo(1)
|
||||
|
||||
reveal_type(foo.__dataclass_fields__) # revealed: dict[str, Field[Any]]
|
||||
reveal_type(fields(Foo)) # revealed: tuple[Field[Any], ...]
|
||||
reveal_type(asdict(foo)) # revealed: dict[str, Any]
|
||||
```
|
||||
|
||||
The class objects themselves also have a `__dataclass_fields__` attribute:
|
||||
|
||||
```py
|
||||
reveal_type(Foo.__dataclass_fields__) # revealed: dict[str, Field[Any]]
|
||||
```
|
||||
|
||||
They can be passed into `fields` as well, because it also accepts `type[DataclassInstance]`
|
||||
arguments:
|
||||
|
||||
```py
|
||||
reveal_type(fields(Foo)) # revealed: tuple[Field[Any], ...]
|
||||
```
|
||||
|
||||
But calling `asdict` on the class object is not allowed:
|
||||
|
||||
```py
|
||||
# TODO: this should be a invalid-argument-type error, but we don't properly check the
|
||||
# types (and more importantly, the `ClassVar` type qualifier) of protocol members yet.
|
||||
asdict(Foo)
|
||||
```
|
||||
|
||||
## Other special cases
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue