[ty] Add more tests for NamedTuples (#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:
Abhijeet Prasad Bodas 2025-05-10 14:16:08 +05:30 committed by GitHub
parent 40fd52dde0
commit 235b74a310
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,6 +37,10 @@ Person(3, "Eve", 99, "extra")
# error: [invalid-argument-type]
Person(id="3", name="Eve")
# TODO: over-writing NamedTuple fields should be an error
alice.id = 42
bob.age = None
```
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)
```
### 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 is not supported for `NamedTuple` classes:
@ -89,6 +106,20 @@ reveal_type(alice.level) # revealed: int
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
```toml