mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-21 07:41:53 +00:00
[ty] apply KW_ONLY
sentinel only to local fields (#19986)
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-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (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-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
fix https://github.com/astral-sh/ty/issues/1047 ## Summary This PR fixes how `KW_ONLY` is applied in dataclasses. Previously, the sentinel leaked into subclasses and incorrectly marked their fields as keyword-only; now it only affects fields declared in the same class. ```py from dataclasses import dataclass, KW_ONLY @dataclass class D: x: int _: KW_ONLY y: str @dataclass class E(D): z: bytes # This should work: x=1 (positional), z=b"foo" (positional), y="foo" (keyword-only) E(1, b"foo", y="foo") reveal_type(E.__init__) # revealed: (self: E, x: int, z: bytes, *, y: str) -> None ``` <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan <!-- How was it tested? --> mdtests
This commit is contained in:
parent
c6dcfe36d0
commit
58efd19f11
4 changed files with 93 additions and 45 deletions
|
@ -988,6 +988,28 @@ class D: # error: [duplicate-kw-only]
|
|||
z: float
|
||||
```
|
||||
|
||||
`KW_ONLY` should only affect fields declared after it within the same class, not fields in
|
||||
subclasses:
|
||||
|
||||
```py
|
||||
from dataclasses import dataclass, KW_ONLY
|
||||
|
||||
@dataclass
|
||||
class D:
|
||||
x: int
|
||||
_: KW_ONLY
|
||||
y: str
|
||||
|
||||
@dataclass
|
||||
class E(D):
|
||||
z: bytes
|
||||
|
||||
# This should work: x=1 (positional), z=b"foo" (positional), y="foo" (keyword-only)
|
||||
E(1, b"foo", y="foo")
|
||||
|
||||
reveal_type(E.__init__) # revealed: (self: E, x: int, z: bytes, *, y: str) -> None
|
||||
```
|
||||
|
||||
## Other special cases
|
||||
|
||||
### `dataclasses.dataclass`
|
||||
|
|
|
@ -49,6 +49,22 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/dataclasses/dataclasses.
|
|||
35 | y: str
|
||||
36 | _2: KW_ONLY
|
||||
37 | z: float
|
||||
38 | from dataclasses import dataclass, KW_ONLY
|
||||
39 |
|
||||
40 | @dataclass
|
||||
41 | class D:
|
||||
42 | x: int
|
||||
43 | _: KW_ONLY
|
||||
44 | y: str
|
||||
45 |
|
||||
46 | @dataclass
|
||||
47 | class E(D):
|
||||
48 | z: bytes
|
||||
49 |
|
||||
50 | # This should work: x=1 (positional), z=b"foo" (positional), y="foo" (keyword-only)
|
||||
51 | E(1, b"foo", y="foo")
|
||||
52 |
|
||||
53 | reveal_type(E.__init__) # revealed: (self: E, x: int, z: bytes, *, y: str) -> None
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
@ -141,3 +157,15 @@ info: `KW_ONLY` fields: `_1`, `_2`
|
|||
info: rule `duplicate-kw-only` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
info[revealed-type]: Revealed type
|
||||
--> src/mdtest_snippet.py:53:13
|
||||
|
|
||||
51 | E(1, b"foo", y="foo")
|
||||
52 |
|
||||
53 | reveal_type(E.__init__) # revealed: (self: E, x: int, z: bytes, *, y: str) -> None
|
||||
| ^^^^^^^^^^ `(self: E, x: int, z: bytes, *, y: str) -> None`
|
||||
|
|
||||
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue