[ty] Detect illegal multiple inheritance with NamedTuple (#19943)

This commit is contained in:
Alex Waygood
2025-08-18 13:03:01 +01:00
committed by GitHub
parent 5e4fa9e442
commit fbf24be8ae
7 changed files with 250 additions and 76 deletions

View File

@@ -115,15 +115,28 @@ class Location(NamedTuple):
### Multiple Inheritance
Multiple inheritance is not supported for `NamedTuple` classes:
<!-- snapshot-diagnostics -->
Multiple inheritance is not supported for `NamedTuple` classes except with `Generic`:
```py
from typing import NamedTuple
from typing import NamedTuple, Protocol
# This should ideally emit a diagnostic
# error: [invalid-named-tuple] "NamedTuple class `C` cannot use multiple inheritance except with `Generic[]`"
class C(NamedTuple, object):
id: int
name: str
# fmt: off
class D(
int, # error: [invalid-named-tuple]
NamedTuple
): ...
# fmt: on
# error: [invalid-named-tuple]
class E(NamedTuple, Protocol): ...
```
### Inheriting from a `NamedTuple`