mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +00:00
[ty] Add more tests for NamedTuple
s (#17975)
## Summary Add more tests and TODOs for `NamedTuple` support, based on the typing spec: https://typing.python.org/en/latest/spec/namedtuples.html ## Test Plan This PR adds new tests.
This commit is contained in:
parent
40fd52dde0
commit
235b74a310
1 changed files with 31 additions and 0 deletions
|
@ -37,6 +37,10 @@ Person(3, "Eve", 99, "extra")
|
||||||
|
|
||||||
# error: [invalid-argument-type]
|
# error: [invalid-argument-type]
|
||||||
Person(id="3", name="Eve")
|
Person(id="3", name="Eve")
|
||||||
|
|
||||||
|
# TODO: over-writing NamedTuple fields should be an error
|
||||||
|
alice.id = 42
|
||||||
|
bob.age = None
|
||||||
```
|
```
|
||||||
|
|
||||||
Alternative functional syntax:
|
Alternative functional syntax:
|
||||||
|
@ -52,6 +56,19 @@ reveal_type(alice2.id) # revealed: @Todo(functional `NamedTuple` syntax)
|
||||||
reveal_type(alice2.name) # revealed: @Todo(functional `NamedTuple` syntax)
|
reveal_type(alice2.name) # revealed: @Todo(functional `NamedTuple` syntax)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Definition
|
||||||
|
|
||||||
|
TODO: Fields without default values should come before fields with.
|
||||||
|
|
||||||
|
```py
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
|
class Location(NamedTuple):
|
||||||
|
altitude: float = 0.0
|
||||||
|
latitude: float # this should be an error
|
||||||
|
longitude: float
|
||||||
|
```
|
||||||
|
|
||||||
### Multiple Inheritance
|
### Multiple Inheritance
|
||||||
|
|
||||||
Multiple inheritance is not supported for `NamedTuple` classes:
|
Multiple inheritance is not supported for `NamedTuple` classes:
|
||||||
|
@ -89,6 +106,20 @@ reveal_type(alice.level) # revealed: int
|
||||||
alice = SuperUser(1, "Alice", 3)
|
alice = SuperUser(1, "Alice", 3)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
TODO: If any fields added by the subclass conflict with those in the base class, that should be
|
||||||
|
flagged.
|
||||||
|
|
||||||
|
```py
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
|
class User(NamedTuple):
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
|
||||||
|
class SuperUser(User):
|
||||||
|
id: int # this should be an error
|
||||||
|
```
|
||||||
|
|
||||||
### Generic named tuples
|
### Generic named tuples
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue