Compare commits

..

2 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
40d7fc05d1 Initial plan 2026-01-14 18:23:40 +00:00
Micha Reiser
4d24fa8169 [ty] Fix flaky completions 2026-01-14 18:54:15 +01:00
16 changed files with 546 additions and 2184 deletions

View File

@@ -4025,7 +4025,7 @@ quux.<CURSOR>
__module__ :: str
__mul__ :: bound method Quux.__mul__(value: SupportsIndex, /) -> tuple[int | str, ...]
__ne__ :: bound method Quux.__ne__(value: object, /) -> bool
__new__ :: (x: int, y: str) -> Quux
__new__ :: (x: int, y: str) -> None
__orig_bases__ :: tuple[Any, ...]
__reduce__ :: bound method Quux.__reduce__() -> str | tuple[Any, ...]
__reduce_ex__ :: bound method Quux.__reduce_ex__(protocol: SupportsIndex, /) -> str | tuple[Any, ...]

View File

@@ -1708,44 +1708,6 @@ class Foo(type("Ba<CURSOR>r", (), {})):
assert_snapshot!(test.goto_definition(), @"No goto target found");
}
/// goto-definition on a dynamic namedtuple class literal (created via `collections.namedtuple()`)
#[test]
fn goto_definition_dynamic_namedtuple_literal() {
let test = CursorTest::builder()
.source(
"main.py",
r#"
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Poi<CURSOR>nt(1, 2)
"#,
)
.build();
assert_snapshot!(test.goto_definition(), @r#"
info[goto-definition]: Go to definition
--> main.py:6:5
|
4 | Point = namedtuple("Point", ["x", "y"])
5 |
6 | p = Point(1, 2)
| ^^^^^ Clicking here
|
info: Found 1 definition
--> main.py:4:1
|
2 | from collections import namedtuple
3 |
4 | Point = namedtuple("Point", ["x", "y"])
| -----
5 |
6 | p = Point(1, 2)
|
"#);
}
// TODO: Should only list `a: int`
#[test]
fn redeclarations() {

View File

@@ -367,7 +367,7 @@ def f_wrong(c: Callable[[], None]):
# error: [unresolved-attribute] "Object of type `() -> None` has no attribute `__qualname__`"
c.__qualname__
# error: [unresolved-attribute] "Unresolved attribute `__qualname__` on type `() -> None`"
# error: [unresolved-attribute] "Unresolved attribute `__qualname__` on type `() -> None`."
c.__qualname__ = "my_callable"
```

View File

@@ -1,7 +1,7 @@
# Unsupported special types
We do not understand the functional syntax for creating `TypedDict`s or `Enum`s yet. But we also do
not emit false positives when these are used in type expressions.
We do not understand the functional syntax for creating `NamedTuple`s, `TypedDict`s or `Enum`s yet.
But we also do not emit false positives when these are used in type expressions.
```py
import collections
@@ -11,6 +11,8 @@ import typing
MyEnum = enum.Enum("MyEnum", ["foo", "bar", "baz"])
MyIntEnum = enum.IntEnum("MyIntEnum", ["foo", "bar", "baz"])
MyTypedDict = typing.TypedDict("MyTypedDict", {"foo": int})
MyNamedTuple1 = typing.NamedTuple("MyNamedTuple1", [("foo", int)])
MyNamedTuple2 = collections.namedtuple("MyNamedTuple2", ["foo"])
def f(a: MyEnum, b: MyTypedDict): ...
def f(a: MyEnum, b: MyTypedDict, c: MyNamedTuple1, d: MyNamedTuple2): ...
```

View File

@@ -84,489 +84,17 @@ alice.id = 42
bob.age = None
```
Alternative functional syntax with a list of tuples:
Alternative functional syntax:
```py
Person2 = NamedTuple("Person", [("id", int), ("name", str)])
alice2 = Person2(1, "Alice")
# error: [missing-argument]
# TODO: should be an error
Person2(1)
reveal_type(alice2.id) # revealed: int
reveal_type(alice2.name) # revealed: str
```
Functional syntax with a tuple of tuples:
```py
Person3 = NamedTuple("Person", (("id", int), ("name", str)))
alice3 = Person3(1, "Alice")
reveal_type(alice3.id) # revealed: int
reveal_type(alice3.name) # revealed: str
```
Functional syntax with a tuple of lists:
```py
Person4 = NamedTuple("Person", (["id", int], ["name", str]))
alice4 = Person4(1, "Alice")
reveal_type(alice4.id) # revealed: int
reveal_type(alice4.name) # revealed: str
```
Functional syntax with a list of lists:
```py
Person5 = NamedTuple("Person", [["id", int], ["name", str]])
alice5 = Person5(1, "Alice")
reveal_type(alice5.id) # revealed: int
reveal_type(alice5.name) # revealed: str
```
### Functional syntax with variable name
When the typename is passed via a variable, we can extract it from the inferred literal string type:
```py
from typing import NamedTuple
name = "Person"
Person = NamedTuple(name, [("id", int), ("name", str)])
p = Person(1, "Alice")
reveal_type(p.id) # revealed: int
reveal_type(p.name) # revealed: str
```
### Functional syntax with tuple variable fields
When fields are passed via a tuple variable, we can extract the literal field names and types from
the inferred tuple type:
```py
from typing import NamedTuple
from ty_extensions import static_assert, is_subtype_of, reveal_mro
fields = (("host", str), ("port", int))
Url = NamedTuple("Url", fields)
url = Url("localhost", 8080)
reveal_type(url.host) # revealed: str
reveal_type(url.port) # revealed: int
# Generic types are also correctly converted to instance types.
generic_fields = (("items", list[int]), ("mapping", dict[str, bool]))
Container = NamedTuple("Container", generic_fields)
container = Container([1, 2, 3], {"a": True})
reveal_type(container.items) # revealed: list[int]
reveal_type(container.mapping) # revealed: dict[str, bool]
# MRO includes the properly specialized tuple type.
# revealed: (<class 'Url'>, <class 'tuple[str, int]'>, <class 'object'>)
reveal_mro(Url)
static_assert(is_subtype_of(Url, tuple[str, int]))
# Invalid type expressions in fields produce a diagnostic.
invalid_fields = (("x", 42),) # 42 is not a valid type
# error: [invalid-type-form] "Invalid type `Literal[42]` in `NamedTuple` field type"
InvalidNT = NamedTuple("InvalidNT", invalid_fields)
reveal_type(InvalidNT) # revealed: <class 'InvalidNT'>
# Unpacking works correctly with the field types.
host, port = url
reveal_type(host) # revealed: str
reveal_type(port) # revealed: int
# error: [invalid-assignment] "Too many values to unpack: Expected 1"
(only_one,) = url
# error: [invalid-assignment] "Not enough values to unpack: Expected 3"
a, b, c = url
# Indexing works correctly.
reveal_type(url[0]) # revealed: str
reveal_type(url[1]) # revealed: int
# error: [index-out-of-bounds]
url[2]
```
### Functional syntax with variadic tuple fields
When fields are passed as a variadic tuple (e.g., `tuple[..., *tuple[T, ...]]`), we cannot determine
the exact field count statically. In this case, we fall back to unknown fields:
```toml
[environment]
python-version = "3.11"
```
```py
from typing import NamedTuple
from ty_extensions import reveal_mro
# Variadic tuple - we can't determine the exact fields statically.
def get_fields() -> tuple[tuple[str, type[int]], *tuple[tuple[str, type[str]], ...]]:
return (("x", int), ("y", str))
fields = get_fields()
NT = NamedTuple("NT", fields)
# Fields are unknown, so attribute access returns Any and MRO has Unknown tuple.
reveal_type(NT) # revealed: <class 'NT'>
reveal_mro(NT) # revealed: (<class 'NT'>, <class 'tuple[Unknown, ...]'>, <class 'object'>)
reveal_type(NT(1, "a").x) # revealed: Any
```
Similarly for `collections.namedtuple`:
```py
import collections
from ty_extensions import reveal_mro
def get_field_names() -> tuple[str, *tuple[str, ...]]:
return ("x", "y")
field_names = get_field_names()
NT = collections.namedtuple("NT", field_names)
# Fields are unknown, so attribute access returns Any and MRO has Unknown tuple.
reveal_type(NT) # revealed: <class 'NT'>
reveal_mro(NT) # revealed: (<class 'NT'>, <class 'tuple[Unknown, ...]'>, <class 'object'>)
reveal_type(NT(1, 2).x) # revealed: Any
```
### Class inheriting from functional NamedTuple
Classes can inherit from functional namedtuples. The constructor parameters and field types are
properly inherited:
```py
from typing import NamedTuple
from ty_extensions import reveal_mro
class Url(NamedTuple("Url", [("host", str), ("path", str)])):
pass
reveal_type(Url) # revealed: <class 'Url'>
# revealed: (<class 'mdtest_snippet.Url @ src/mdtest_snippet.py:4:7'>, <class 'mdtest_snippet.Url @ src/mdtest_snippet.py:4:11'>, <class 'tuple[str, str]'>, <class 'object'>)
reveal_mro(Url)
reveal_type(Url.__new__) # revealed: (cls: type, host: str, path: str) -> Url
# Constructor works with the inherited fields.
url = Url("example.com", "/path")
reveal_type(url) # revealed: Url
reveal_type(url.host) # revealed: str
reveal_type(url.path) # revealed: str
# Error handling works correctly.
# error: [missing-argument]
Url("example.com")
# error: [too-many-positional-arguments]
Url("example.com", "/path", "extra")
```
Subclasses can add methods that use inherited fields:
```py
from typing import NamedTuple
from typing_extensions import Self
class Url(NamedTuple("Url", [("host", str), ("port", int)])):
def with_port(self, port: int) -> Self:
reveal_type(self.host) # revealed: str
reveal_type(self.port) # revealed: int
return self._replace(port=port)
url = Url("localhost", 8080)
reveal_type(url.with_port(9000)) # revealed: Url
```
For `class Foo(namedtuple("Foo", ...)): ...`, the inner call creates a namedtuple class, but the
outer class is just a regular class inheriting from it. This is equivalent to:
```py
class _Foo(NamedTuple): ...
class Foo(_Foo): # Regular class, not a namedtuple
...
```
Because the outer class is not itself a namedtuple, it can use `super()` and override `__new__`:
```py
from collections import namedtuple
from typing import NamedTuple
class ExtType(namedtuple("ExtType", "code data")):
"""Override __new__ to add validation."""
def __new__(cls, code, data):
if not isinstance(code, int):
raise TypeError("code must be int")
return super().__new__(cls, code, data)
class Url(NamedTuple("Url", [("host", str), ("path", str)])):
"""Override __new__ to normalize the path."""
def __new__(cls, host, path):
if path and not path.startswith("/"):
path = "/" + path
return super().__new__(cls, host, path)
# Both work correctly.
ext = ExtType(42, b"hello")
reveal_type(ext) # revealed: ExtType
url = Url("example.com", "path")
reveal_type(url) # revealed: Url
```
### Functional syntax with list variable fields
When fields are passed via a list variable (not a literal), the field names cannot be determined
statically. Attribute access returns `Any` and the constructor accepts any arguments:
```py
from typing import NamedTuple
from typing_extensions import Self
fields = [("host", str), ("port", int)]
class Url(NamedTuple("Url", fields)):
def with_port(self, port: int) -> Self:
# Fields are unknown, so attribute access returns Any.
reveal_type(self.host) # revealed: Any
reveal_type(self.port) # revealed: Any
reveal_type(self.unknown) # revealed: Any
return self._replace(port=port)
```
When constructing a namedtuple directly with dynamically-defined fields, keyword arguments are
accepted because the constructor uses a gradual signature:
```py
import collections
from ty_extensions import reveal_mro
CheckerConfig = ["duration", "video_fps", "audio_sample_rate"]
GroundTruth = collections.namedtuple("GroundTruth", " ".join(CheckerConfig))
# No error - fields are unknown, so any keyword arguments are accepted
config = GroundTruth(duration=0, video_fps=30, audio_sample_rate=44100)
reveal_type(config) # revealed: GroundTruth
reveal_type(config.duration) # revealed: Any
# Namedtuples with unknown fields inherit from tuple[Unknown, ...] to avoid false positives.
# revealed: (<class 'GroundTruth'>, <class 'tuple[Unknown, ...]'>, <class 'object'>)
reveal_mro(GroundTruth)
# No index-out-of-bounds error since the tuple length is unknown.
reveal_type(config[0]) # revealed: Unknown
reveal_type(config[100]) # revealed: Unknown
```
### Functional syntax signature validation
The `collections.namedtuple` function accepts `str | Iterable[str]` for `field_names`:
```py
import collections
from ty_extensions import reveal_mro
# String field names (space-separated)
Point1 = collections.namedtuple("Point", "x y")
reveal_type(Point1) # revealed: <class 'Point'>
reveal_mro(Point1) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
# String field names with multiple spaces
Point1a = collections.namedtuple("Point", "x y")
reveal_type(Point1a) # revealed: <class 'Point'>
reveal_mro(Point1a) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
# String field names (comma-separated also works at runtime)
Point2 = collections.namedtuple("Point", "x, y")
reveal_type(Point2) # revealed: <class 'Point'>
reveal_mro(Point2) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
# List of strings
Point3 = collections.namedtuple("Point", ["x", "y"])
reveal_type(Point3) # revealed: <class 'Point'>
reveal_mro(Point3) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
# Tuple of strings
Point4 = collections.namedtuple("Point", ("x", "y"))
reveal_type(Point4) # revealed: <class 'Point'>
reveal_mro(Point4) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
# Invalid: integer is not a valid typename
# error: [invalid-argument-type]
Invalid = collections.namedtuple(123, ["x", "y"])
reveal_type(Invalid) # revealed: <class '<unknown>'>
reveal_mro(Invalid) # revealed: (<class '<unknown>'>, <class 'tuple[Any, Any]'>, <class 'object'>)
# Invalid: too many positional arguments
# error: [too-many-positional-arguments] "Too many positional arguments to function `namedtuple`: expected 2, got 4"
TooMany = collections.namedtuple("TooMany", "x", "y", "z")
reveal_type(TooMany) # revealed: <class 'TooMany'>
```
The `typing.NamedTuple` function accepts `Iterable[tuple[str, Any]]` for `fields`:
```py
from typing import NamedTuple
# List of tuples
Person1 = NamedTuple("Person", [("name", str), ("age", int)])
reveal_type(Person1) # revealed: <class 'Person'>
# Tuple of tuples
Person2 = NamedTuple("Person", (("name", str), ("age", int)))
reveal_type(Person2) # revealed: <class 'Person'>
# Invalid: integer is not a valid typename
# error: [invalid-argument-type]
NamedTuple(123, [("name", str)])
# Invalid: too many positional arguments
# error: [too-many-positional-arguments] "Too many positional arguments to function `NamedTuple`: expected 2, got 4"
TooMany = NamedTuple("TooMany", [("x", int)], "extra", "args")
reveal_type(TooMany) # revealed: <class 'TooMany'>
```
### Keyword arguments for `collections.namedtuple`
The `collections.namedtuple` function accepts `rename`, `defaults`, and `module` keyword arguments:
```py
import collections
from ty_extensions import reveal_mro
# `rename=True` replaces invalid identifiers with positional names
Point = collections.namedtuple("Point", ["x", "class", "_y", "z", "z"], rename=True)
reveal_type(Point) # revealed: <class 'Point'>
reveal_type(Point.__new__) # revealed: (cls: type, x: Any, _1: Any, _2: Any, z: Any, _4: Any) -> Point
reveal_mro(Point) # revealed: (<class 'Point'>, <class 'tuple[Any, Any, Any, Any, Any]'>, <class 'object'>)
p = Point(1, 2, 3, 4, 5)
reveal_type(p.x) # revealed: Any
reveal_type(p._1) # revealed: Any
reveal_type(p._2) # revealed: Any
reveal_type(p.z) # revealed: Any
reveal_type(p._4) # revealed: Any
# Truthy non-bool values for `rename` are also handled, but emit a diagnostic
# error: [invalid-argument-type] "Invalid argument to parameter `rename` of `namedtuple()`"
Point2 = collections.namedtuple("Point2", ["_x", "class"], rename=1)
reveal_type(Point2) # revealed: <class 'Point2'>
reveal_type(Point2.__new__) # revealed: (cls: type, _0: Any, _1: Any) -> Point2
# `defaults` provides default values for the rightmost fields
Person = collections.namedtuple("Person", ["name", "age", "city"], defaults=["Unknown"])
reveal_type(Person) # revealed: <class 'Person'>
reveal_type(Person.__new__) # revealed: (cls: type, name: Any, age: Any, city: Any = ...) -> Person
reveal_mro(Person) # revealed: (<class 'Person'>, <class 'tuple[Any, Any, Any]'>, <class 'object'>)
# Can create with all fields
person1 = Person("Alice", 30, "NYC")
# Can omit the field with default
person2 = Person("Bob", 25)
reveal_type(person1.city) # revealed: Any
reveal_type(person2.city) # revealed: Any
# `module` is valid but doesn't affect type checking
Config = collections.namedtuple("Config", ["host", "port"], module="myapp")
reveal_type(Config) # revealed: <class 'Config'>
# When more defaults are provided than fields, we treat all fields as having defaults.
# TODO: This should emit a diagnostic since it would fail at runtime.
TooManyDefaults = collections.namedtuple("TooManyDefaults", ["x", "y"], defaults=("a", "b", "c"))
reveal_type(TooManyDefaults) # revealed: <class 'TooManyDefaults'>
reveal_type(TooManyDefaults.__new__) # revealed: (cls: type, x: Any = ..., y: Any = ...) -> TooManyDefaults
# Unknown keyword arguments produce an error
# error: [unknown-argument]
Bad1 = collections.namedtuple("Bad1", ["x", "y"], foobarbaz=42)
reveal_type(Bad1) # revealed: <class 'Bad1'>
reveal_mro(Bad1) # revealed: (<class 'Bad1'>, <class 'tuple[Any, Any]'>, <class 'object'>)
# Multiple unknown keyword arguments
# error: [unknown-argument]
# error: [unknown-argument]
Bad2 = collections.namedtuple("Bad2", ["x"], invalid1=True, invalid2=False)
reveal_type(Bad2) # revealed: <class 'Bad2'>
reveal_mro(Bad2) # revealed: (<class 'Bad2'>, <class 'tuple[Any]'>, <class 'object'>)
# Invalid type for `defaults` (not Iterable[Any] | None)
# error: [invalid-argument-type] "Invalid argument to parameter `defaults` of `namedtuple()`"
Bad3 = collections.namedtuple("Bad3", ["x"], defaults=123)
reveal_type(Bad3) # revealed: <class 'Bad3'>
# Invalid type for `module` (not str | None)
# error: [invalid-argument-type] "Invalid argument to parameter `module` of `namedtuple()`"
Bad4 = collections.namedtuple("Bad4", ["x"], module=456)
reveal_type(Bad4) # revealed: <class 'Bad4'>
# Invalid type for `field_names` (not str | Iterable[str])
# error: [invalid-argument-type] "Invalid argument to parameter `field_names` of `namedtuple()`"
Bad5 = collections.namedtuple("Bad5", 12345)
reveal_type(Bad5) # revealed: <class 'Bad5'>
```
### Keyword arguments for `typing.NamedTuple`
The `typing.NamedTuple` function does not accept any keyword arguments:
```py
from typing import NamedTuple
# error: [unknown-argument]
Bad3 = NamedTuple("Bad3", [("x", int)], rename=True)
# error: [unknown-argument]
Bad4 = NamedTuple("Bad4", [("x", int)], defaults=[0])
# error: [unknown-argument]
Bad5 = NamedTuple("Bad5", [("x", int)], foobarbaz=42)
# Invalid type for `fields` (not an iterable)
# error: [invalid-argument-type] "Invalid argument to parameter `fields` of `NamedTuple()`"
Bad6 = NamedTuple("Bad6", 12345)
reveal_type(Bad6) # revealed: <class 'Bad6'>
```
### Starred and double-starred arguments
When starred (`*args`) or double-starred (`**kwargs`) arguments are used, we fall back to normal
call binding since we can't statically determine the arguments. This results in `NamedTupleFallback`
being returned:
```py
import collections
from typing import NamedTuple
args = ("Point", ["x", "y"])
kwargs = {"rename": True}
# Starred positional arguments - falls back to NamedTupleFallback
Point1 = collections.namedtuple(*args)
reveal_type(Point1) # revealed: type[NamedTupleFallback]
# Ideally we'd catch this false negative,
# but it's unclear if it's worth the added complexity
Point2 = NamedTuple(*args)
reveal_type(Point2) # revealed: type[NamedTupleFallback]
# Double-starred keyword arguments - falls back to NamedTupleFallback
Point3 = collections.namedtuple("Point", ["x", "y"], **kwargs)
reveal_type(Point3) # revealed: type[NamedTupleFallback]
Point4 = NamedTuple("Point", [("x", int), ("y", int)], **kwargs)
reveal_type(Point4) # revealed: type[NamedTupleFallback]
reveal_type(alice2.id) # revealed: @Todo(functional `NamedTuple` syntax)
reveal_type(alice2.name) # revealed: @Todo(functional `NamedTuple` syntax)
```
### Definition
@@ -626,84 +154,6 @@ class D(
class E(NamedTuple, Protocol): ...
```
However, as explained above, for `class Foo(namedtuple("Foo", ...)): ...` the outer class is not
itself a namedtuple—it just inherits from one. So it can use multiple inheritance freely:
```py
from abc import ABC
from collections import namedtuple
from typing import NamedTuple
class Point(namedtuple("Point", ["x", "y"]), ABC):
"""No error - functional namedtuple inheritance allows multiple inheritance."""
class Url(NamedTuple("Url", [("host", str), ("port", int)]), ABC):
"""No error - typing.NamedTuple functional syntax also allows multiple inheritance."""
p = Point(1, 2)
reveal_type(p.x) # revealed: Any
reveal_type(p.y) # revealed: Any
u = Url("localhost", 8080)
reveal_type(u.host) # revealed: str
reveal_type(u.port) # revealed: int
```
### Inherited tuple methods
Namedtuples inherit methods from their tuple base class, including `count`, `index`, and comparison
methods (`__lt__`, `__le__`, `__gt__`, `__ge__`).
```py
from collections import namedtuple
from typing import NamedTuple
# typing.NamedTuple inherits tuple methods
class Point(NamedTuple):
x: int
y: int
p = Point(1, 2)
reveal_type(p.count(1)) # revealed: int
reveal_type(p.index(2)) # revealed: int
# collections.namedtuple also inherits tuple methods
Person = namedtuple("Person", ["name", "age"])
alice = Person("Alice", 30)
reveal_type(alice.count("Alice")) # revealed: int
reveal_type(alice.index(30)) # revealed: int
```
The `@total_ordering` decorator should not emit a diagnostic, since the required `__lt__` method is
already present:
```py
from collections import namedtuple
from functools import total_ordering
from typing import NamedTuple
# No error - __lt__ is inherited from the tuple base class
@total_ordering
class Point(namedtuple("Point", "x y")): ...
p1 = Point(1, 2)
p2 = Point(3, 4)
# TODO: should be `bool`, not `Any | Literal[False]`
reveal_type(p1 < p2) # revealed: Any | Literal[False]
reveal_type(p1 <= p2) # revealed: Any | Literal[True]
# Same for typing.NamedTuple - no error
@total_ordering
class Person(NamedTuple):
name: str
age: int
alice = Person("Alice", 30)
bob = Person("Bob", 25)
reveal_type(alice < bob) # revealed: bool
reveal_type(alice >= bob) # revealed: bool
```
### Inheriting from a `NamedTuple`
Inheriting from a `NamedTuple` is supported, but new fields on the subclass will not be part of the
@@ -804,34 +254,6 @@ reveal_type(LegacyProperty[str].value.fget) # revealed: (self, /) -> str
reveal_type(LegacyProperty("height", 3.4).value) # revealed: int | float
```
Generic namedtuples can also be defined using the functional syntax with type variables in the field
types. We don't currently support this, but mypy does:
```py
from typing import NamedTuple, TypeVar
T = TypeVar("T")
# TODO: ideally this would create a generic namedtuple class
Pair = NamedTuple("Pair", [("first", T), ("second", T)])
# For now, the TypeVar is not specialized, so the field types remain as `T@Pair` and argument type
# errors are emitted when calling the constructor.
reveal_type(Pair) # revealed: <class 'Pair'>
# error: [invalid-argument-type]
# error: [invalid-argument-type]
reveal_type(Pair(1, 2)) # revealed: Pair
# error: [invalid-argument-type]
# error: [invalid-argument-type]
reveal_type(Pair(1, 2).first) # revealed: T@Pair
# error: [invalid-argument-type]
# error: [invalid-argument-type]
reveal_type(Pair(1, 2).second) # revealed: T@Pair
```
## Attributes on `NamedTuple`
The following attributes are available on `NamedTuple` classes / instances:
@@ -889,73 +311,6 @@ alice = Person(1, "Alice", 42)
bob = Person(2, "Bob")
```
## `collections.namedtuple` with tuple variable field names
When field names are passed via a tuple variable, we can extract the literal field names from the
inferred tuple type. The class is properly synthesized (not a fallback), but field types are `Any`
since `collections.namedtuple` doesn't include type annotations:
```py
from collections import namedtuple
field_names = ("name", "age")
Person = namedtuple("Person", field_names)
reveal_type(Person) # revealed: <class 'Person'>
alice = Person("Alice", 42)
reveal_type(alice) # revealed: Person
reveal_type(alice.name) # revealed: Any
reveal_type(alice.age) # revealed: Any
```
## `collections.namedtuple` with list variable field names
When field names are passed via a list variable (not a literal), we fall back to
`NamedTupleFallback` which allows any attribute access. This is a regression test for accessing
`Self` attributes in methods of classes that inherit from namedtuples with dynamic fields:
```py
from collections import namedtuple
from typing_extensions import Self
field_names = ["host", "port"]
class Url(namedtuple("Url", field_names)):
def with_port(self, port: int) -> Self:
# Fields are unknown, so attribute access returns `Any`.
reveal_type(self.host) # revealed: Any
reveal_type(self.port) # revealed: Any
reveal_type(self.unknown) # revealed: Any
return self._replace(port=port)
```
## `collections.namedtuple` attributes
Functional namedtuples have synthesized attributes similar to class-based namedtuples:
```py
from collections import namedtuple
Person = namedtuple("Person", ["name", "age"])
reveal_type(Person._fields) # revealed: tuple[Literal["name"], Literal["age"]]
reveal_type(Person._field_defaults) # revealed: dict[str, Any]
reveal_type(Person._make) # revealed: bound method <class 'Person'>._make(iterable: Iterable[Any]) -> Person
reveal_type(Person._asdict) # revealed: def _asdict(self) -> dict[str, Any]
reveal_type(Person._replace) # revealed: (self: Self, *, name: Any = ..., age: Any = ...) -> Self
# _make creates instances from an iterable.
reveal_type(Person._make(["Alice", 30])) # revealed: Person
# _asdict converts to a dictionary.
person = Person("Alice", 30)
reveal_type(person._asdict()) # revealed: dict[str, Any]
# _replace creates a copy with replaced fields.
reveal_type(person._replace(name="Bob")) # revealed: Person
```
## The symbol `NamedTuple` itself
At runtime, `NamedTuple` is a function, and we understand this:

View File

@@ -39,7 +39,7 @@ info: rule `unresolved-attribute` is enabled by default
```
```
error[unresolved-attribute]: Unresolved attribute `non_existent` on type `C`
error[unresolved-attribute]: Unresolved attribute `non_existent` on type `C`.
--> src/mdtest_snippet.py:6:1
|
5 | instance = C()

View File

@@ -30,23 +30,6 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/named_tuple.md
15 |
16 | # error: [invalid-named-tuple]
17 | class E(NamedTuple, Protocol): ...
18 | from abc import ABC
19 | from collections import namedtuple
20 | from typing import NamedTuple
21 |
22 | class Point(namedtuple("Point", ["x", "y"]), ABC):
23 | """No error - functional namedtuple inheritance allows multiple inheritance."""
24 |
25 | class Url(NamedTuple("Url", [("host", str), ("port", int)]), ABC):
26 | """No error - typing.NamedTuple functional syntax also allows multiple inheritance."""
27 |
28 | p = Point(1, 2)
29 | reveal_type(p.x) # revealed: Any
30 | reveal_type(p.y) # revealed: Any
31 |
32 | u = Url("localhost", 8080)
33 | reveal_type(u.host) # revealed: str
34 | reveal_type(u.port) # revealed: int
```
# Diagnostics
@@ -85,8 +68,6 @@ error[invalid-named-tuple]: NamedTuple class `E` cannot use multiple inheritance
16 | # error: [invalid-named-tuple]
17 | class E(NamedTuple, Protocol): ...
| ^^^^^^^^
18 | from abc import ABC
19 | from collections import namedtuple
|
info: rule `invalid-named-tuple` is enabled by default

View File

@@ -3384,16 +3384,17 @@ impl<'db> Type<'db> {
.map(|class| class.class_literal(db)),
_ => None,
};
if let Some(enum_class) = enum_class
&& let Some(metadata) = enum_metadata(db, enum_class)
&& let Some(resolved_name) = metadata.resolve_member(&name)
{
return Place::bound(Type::EnumLiteral(EnumLiteralType::new(
db,
enum_class,
resolved_name,
)))
.into();
if let Some(enum_class) = enum_class {
if let Some(metadata) = enum_metadata(db, enum_class) {
if let Some(resolved_name) = metadata.resolve_member(&name) {
return Place::bound(Type::EnumLiteral(EnumLiteralType::new(
db,
enum_class,
resolved_name,
)))
.into();
}
}
}
let class_attr_plain = self.find_name_in_mro_with_policy(db, name_str, policy).expect(
@@ -4401,6 +4402,10 @@ impl<'db> Type<'db> {
.into()
}
Type::SpecialForm(SpecialFormType::NamedTuple) => {
Binding::single(self, Signature::todo("functional `NamedTuple` syntax")).into()
}
Type::GenericAlias(_) => {
// TODO annotated return type on `__new__` or metaclass `__call__`
// TODO check call vs signatures of `__new__` and/or `__init__`
@@ -5062,15 +5067,16 @@ impl<'db> Type<'db> {
let from_class_base = |base: ClassBase<'db>| {
let class = base.into_class()?;
if class.is_known(db, KnownClass::Generator)
&& let Some((_, Some(specialization))) =
if class.is_known(db, KnownClass::Generator) {
if let Some((_, Some(specialization))) =
class.static_class_literal_specialized(db, None)
&& let [_, _, return_ty] = specialization.types(db)
{
Some(*return_ty)
} else {
None
{
if let [_, _, return_ty] = specialization.types(db) {
return Some(*return_ty);
}
}
}
None
};
match self {
@@ -8052,10 +8058,15 @@ impl<'db> TypeVarInstance<'db> {
let typevar_node = typevar.node(&module);
let bound =
definition_expression_type(db, definition, typevar_node.bound.as_ref()?);
let constraints = if let Some(tuple) = bound.tuple_instance_spec(db)
&& let Tuple::Fixed(tuple) = tuple.into_owned()
let constraints = if let Some(tuple) = bound
.as_nominal_instance()
.and_then(|instance| instance.tuple_spec(db))
{
tuple.owned_elements()
if let Tuple::Fixed(tuple) = tuple.into_owned() {
tuple.owned_elements()
} else {
vec![Type::unknown()].into_boxed_slice()
}
} else {
vec![Type::unknown()].into_boxed_slice()
};
@@ -9133,13 +9144,13 @@ impl<'db> AwaitError<'db> {
""
};
diag.info(format_args!("`__await__` is{possibly} not callable"));
if let Some(definition) = bindings.callable_type().definition(db)
&& let Some(definition_range) = definition.focus_range(db)
{
diag.annotate(
Annotation::secondary(definition_range.into())
.message("attribute defined here"),
);
if let Some(definition) = bindings.callable_type().definition(db) {
if let Some(definition_range) = definition.focus_range(db) {
diag.annotate(
Annotation::secondary(definition_range.into())
.message("attribute defined here"),
);
}
}
}
Self::Call(CallDunderError::PossiblyUnbound(bindings)) => {
@@ -9153,12 +9164,13 @@ impl<'db> AwaitError<'db> {
}
Self::Call(CallDunderError::MethodNotAvailable) => {
diag.info("`__await__` is missing");
if let Some(type_definition) = context_expression_type.definition(db)
&& let Some(definition_range) = type_definition.focus_range(db)
{
diag.annotate(
Annotation::secondary(definition_range.into()).message("type defined here"),
);
if let Some(type_definition) = context_expression_type.definition(db) {
if let Some(definition_range) = type_definition.focus_range(db) {
diag.annotate(
Annotation::secondary(definition_range.into())
.message("type defined here"),
);
}
}
}
Self::InvalidReturnType(return_type, bindings) => {
@@ -11344,20 +11356,20 @@ impl<'db> ModuleLiteralType<'db> {
// if it exists. First, we need to look up the `__getattr__` function in the module's scope.
if let Some(file) = self.module(db).file(db) {
let getattr_symbol = imported_symbol(db, file, "__getattr__", None);
// If we found a __getattr__ function, try to call it with the name argument
if let Place::Defined(place) = getattr_symbol.place
&& let Ok(outcome) = place.ty.try_call(
if let Place::Defined(place) = getattr_symbol.place {
// If we found a __getattr__ function, try to call it with the name argument
if let Ok(outcome) = place.ty.try_call(
db,
&CallArguments::positional([Type::string_literal(db, name)]),
)
{
return PlaceAndQualifiers {
place: Place::Defined(DefinedPlace {
ty: outcome.return_type(db),
..place
}),
qualifiers: TypeQualifiers::FROM_MODULE_GETATTR,
};
) {
return PlaceAndQualifiers {
place: Place::Defined(DefinedPlace {
ty: outcome.return_type(db),
..place
}),
qualifiers: TypeQualifiers::FROM_MODULE_GETATTR,
};
}
}
}
@@ -11383,10 +11395,10 @@ impl<'db> ModuleLiteralType<'db> {
// the parent module's `__init__.py` file being evaluated. That said, we have
// chosen to always have the submodule take priority. (This matches pyright's
// current behavior, but is the opposite of mypy's current behavior.)
if self.available_submodule_attributes(db).contains(name)
&& let Some(submodule) = self.resolve_submodule(db, name)
{
return Place::bound(submodule).into();
if self.available_submodule_attributes(db).contains(name) {
if let Some(submodule) = self.resolve_submodule(db, name) {
return Place::bound(submodule).into();
}
}
let place_and_qualifiers = self
@@ -12085,11 +12097,17 @@ impl<'db> UnionType<'db> {
let mut has_float = false;
let mut has_complex = false;
for element in self.elements(db) {
match element.as_nominal_instance()?.known_class(db)? {
KnownClass::Int => has_int = true,
KnownClass::Float => has_float = true,
KnownClass::Complex => has_complex = true,
_ => return None,
if let Type::NominalInstance(nominal) = element
&& let Some(known) = nominal.known_class(db)
{
match known {
KnownClass::Int => has_int = true,
KnownClass::Float => has_float = true,
KnownClass::Complex => has_complex = true,
_ => return None,
}
} else {
return None;
}
}
match (has_int, has_float, has_complex) {

View File

@@ -1187,6 +1187,11 @@ impl<'db> Bindings<'db> {
}
}
Some(KnownFunction::NamedTuple) => {
overload
.set_return_type(todo_type!("Support for functional `namedtuple`"));
}
_ => {
// Ideally, either the implementation, or exactly one of the overloads
// of the function can have the dataclass_transform decorator applied.

View File

@@ -194,7 +194,6 @@ impl<'db> CodeGeneratorKind<'db> {
Self::from_static_class(db, static_class, specialization)
}
ClassLiteral::Dynamic(dynamic_class) => Self::from_dynamic_class(db, dynamic_class),
ClassLiteral::DynamicNamedTuple(_) => Some(Self::NamedTuple),
}
}
@@ -464,8 +463,6 @@ pub enum ClassLiteral<'db> {
Static(StaticClassLiteral<'db>),
/// A class created dynamically via `type(name, bases, dict)`.
Dynamic(DynamicClassLiteral<'db>),
/// A class created via `collections.namedtuple()` or `typing.NamedTuple()`.
DynamicNamedTuple(DynamicNamedTupleLiteral<'db>),
}
impl<'db> ClassLiteral<'db> {
@@ -474,7 +471,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.name(db),
Self::Dynamic(class) => class.name(db),
Self::DynamicNamedTuple(namedtuple) => namedtuple.name(db),
}
}
@@ -499,7 +495,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.metaclass(db),
Self::Dynamic(class) => class.metaclass(db),
Self::DynamicNamedTuple(namedtuple) => namedtuple.metaclass(db),
}
}
@@ -513,7 +508,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.class_member(db, name, policy),
Self::Dynamic(class) => class.class_member(db, name, policy),
Self::DynamicNamedTuple(namedtuple) => namedtuple.class_member(db, name, policy),
}
}
@@ -529,7 +523,7 @@ impl<'db> ClassLiteral<'db> {
) -> PlaceAndQualifiers<'db> {
match self {
Self::Static(class) => class.class_member_from_mro(db, name, policy, mro_iter),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => {
Self::Dynamic(_) => {
// Dynamic classes don't have inherited generic context and are never `object`.
let result = MroLookup::new(db, mro_iter).class_member(name, policy, None, false);
match result {
@@ -555,7 +549,7 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn default_specialization(self, db: &'db dyn Db) -> ClassType<'db> {
match self {
Self::Static(class) => class.default_specialization(db),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => ClassType::NonGeneric(self),
Self::Dynamic(_) => ClassType::NonGeneric(self),
}
}
@@ -563,7 +557,7 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn identity_specialization(self, db: &'db dyn Db) -> ClassType<'db> {
match self {
Self::Static(class) => class.identity_specialization(db),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => ClassType::NonGeneric(self),
Self::Dynamic(_) => ClassType::NonGeneric(self),
}
}
@@ -581,7 +575,7 @@ impl<'db> ClassLiteral<'db> {
pub fn is_typed_dict(self, db: &'db dyn Db) -> bool {
match self {
Self::Static(class) => class.is_typed_dict(db),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => false,
Self::Dynamic(_) => false,
}
}
@@ -589,7 +583,7 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn is_tuple(self, db: &'db dyn Db) -> bool {
match self {
Self::Static(class) => class.is_tuple(db),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => false,
Self::Dynamic(_) => false,
}
}
@@ -611,7 +605,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.file(db),
Self::Dynamic(class) => class.scope(db).file(db),
Self::DynamicNamedTuple(class) => class.scope(db).file(db),
}
}
@@ -623,7 +616,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.header_range(db),
Self::Dynamic(class) => class.header_range(db),
Self::DynamicNamedTuple(class) => class.header_range(db),
}
}
@@ -636,10 +628,8 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn is_final(self, db: &'db dyn Db) -> bool {
match self {
Self::Static(class) => class.is_final(db),
// Dynamic classes created via `type()`, `collections.namedtuple()`, etc. cannot be
// marked as final.
// Dynamic classes created via `type()` cannot be marked as final.
Self::Dynamic(_) => false,
Self::DynamicNamedTuple(_) => false,
}
}
@@ -656,7 +646,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.has_own_ordering_method(db),
Self::Dynamic(class) => class.has_own_ordering_method(db),
Self::DynamicNamedTuple(_) => false,
}
}
@@ -664,7 +653,7 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn as_static(self) -> Option<StaticClassLiteral<'db>> {
match self {
Self::Static(class) => Some(class),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => None,
Self::Dynamic(_) => None,
}
}
@@ -673,7 +662,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => Some(class.definition(db)),
Self::Dynamic(class) => class.definition(db),
Self::DynamicNamedTuple(namedtuple) => namedtuple.definition(db),
}
}
@@ -685,9 +673,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => Some(TypeDefinition::StaticClass(class.definition(db))),
Self::Dynamic(class) => class.definition(db).map(TypeDefinition::DynamicClass),
Self::DynamicNamedTuple(namedtuple) => {
namedtuple.definition(db).map(TypeDefinition::DynamicClass)
}
}
}
@@ -704,7 +689,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.header_span(db),
Self::Dynamic(class) => class.header_span(db),
Self::DynamicNamedTuple(namedtuple) => namedtuple.header_span(db),
}
}
@@ -729,9 +713,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.as_disjoint_base(db),
Self::Dynamic(class) => class.as_disjoint_base(db),
// Dynamic namedtuples define `__slots__ = ()`, but `__slots__` must be
// non-empty for a class to be a disjoint base.
Self::DynamicNamedTuple(_) => None,
}
}
@@ -739,9 +720,7 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn to_non_generic_instance(self, db: &'db dyn Db) -> Type<'db> {
match self {
Self::Static(class) => class.to_non_generic_instance(db),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => {
Type::instance(db, ClassType::NonGeneric(self))
}
Self::Dynamic(_) => Type::instance(db, ClassType::NonGeneric(self)),
}
}
@@ -762,7 +741,7 @@ impl<'db> ClassLiteral<'db> {
) -> ClassType<'db> {
match self {
Self::Static(class) => class.apply_specialization(db, f),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => ClassType::NonGeneric(self),
Self::Dynamic(_) => ClassType::NonGeneric(self),
}
}
@@ -776,7 +755,6 @@ impl<'db> ClassLiteral<'db> {
match self {
Self::Static(class) => class.instance_member(db, specialization, name),
Self::Dynamic(class) => class.instance_member(db, name),
Self::DynamicNamedTuple(namedtuple) => namedtuple.instance_member(db, name),
}
}
@@ -784,7 +762,7 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn top_materialization(self, db: &'db dyn Db) -> ClassType<'db> {
match self {
Self::Static(class) => class.top_materialization(db),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => ClassType::NonGeneric(self),
Self::Dynamic(_) => ClassType::NonGeneric(self),
}
}
@@ -798,16 +776,11 @@ impl<'db> ClassLiteral<'db> {
) -> PlaceAndQualifiers<'db> {
match self {
Self::Static(class) => class.typed_dict_member(db, specialization, name, policy),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => Place::Undefined.into(),
Self::Dynamic(_) => Place::Undefined.into(),
}
}
/// Returns a new `ClassLiteral` with the given dataclass params, preserving all other fields.
///
/// TODO: Applying `@dataclasses.dataclass` to a `NamedTuple` subclass doesn't fail at runtime
/// (e.g., `@dataclasses.dataclass class Foo(NamedTuple): ...`), and neither does
/// `dataclasses.dataclass(collections.namedtuple("A", ()))`. We should either infer these
/// accurately or emit a diagnostic on them.
pub(crate) fn with_dataclass_params(
self,
db: &'db dyn Db,
@@ -818,7 +791,6 @@ impl<'db> ClassLiteral<'db> {
Self::Dynamic(class) => {
Self::Dynamic(class.with_dataclass_params(db, dataclass_params))
}
Self::DynamicNamedTuple(_) => self,
}
}
}
@@ -835,12 +807,6 @@ impl<'db> From<DynamicClassLiteral<'db>> for ClassLiteral<'db> {
}
}
impl<'db> From<DynamicNamedTupleLiteral<'db>> for ClassLiteral<'db> {
fn from(literal: DynamicNamedTupleLiteral<'db>) -> Self {
ClassLiteral::DynamicNamedTuple(literal)
}
}
/// Represents a class type, which might be a non-generic class, or a specialization of a generic
/// class.
#[derive(
@@ -933,7 +899,7 @@ impl<'db> ClassType<'db> {
) -> Option<(StaticClassLiteral<'db>, Option<Specialization<'db>>)> {
match self {
Self::NonGeneric(ClassLiteral::Static(class)) => Some((class, None)),
Self::NonGeneric(ClassLiteral::Dynamic(_) | ClassLiteral::DynamicNamedTuple(_)) => None,
Self::NonGeneric(ClassLiteral::Dynamic(_)) => None,
Self::Generic(generic) => Some((generic.origin(db), Some(generic.specialization(db)))),
}
}
@@ -947,7 +913,7 @@ impl<'db> ClassType<'db> {
) -> Option<(StaticClassLiteral<'db>, Option<Specialization<'db>>)> {
match self {
Self::NonGeneric(ClassLiteral::Static(class)) => Some((class, None)),
Self::NonGeneric(ClassLiteral::Dynamic(_) | ClassLiteral::DynamicNamedTuple(_)) => None,
Self::NonGeneric(ClassLiteral::Dynamic(_)) => None,
Self::Generic(generic) => Some((
generic.origin(db),
Some(
@@ -1357,9 +1323,6 @@ impl<'db> ClassType<'db> {
Self::NonGeneric(ClassLiteral::Dynamic(dynamic)) => {
return dynamic.own_class_member(db, name);
}
Self::NonGeneric(ClassLiteral::DynamicNamedTuple(namedtuple)) => {
return namedtuple.own_class_member(db, name);
}
Self::NonGeneric(ClassLiteral::Static(class)) => (class, None),
Self::Generic(generic) => (generic.origin(db), Some(generic.specialization(db))),
};
@@ -1650,9 +1613,6 @@ impl<'db> ClassType<'db> {
pub(super) fn instance_member(self, db: &'db dyn Db, name: &str) -> PlaceAndQualifiers<'db> {
match self {
Self::NonGeneric(ClassLiteral::Dynamic(class)) => class.instance_member(db, name),
Self::NonGeneric(ClassLiteral::DynamicNamedTuple(namedtuple)) => {
namedtuple.instance_member(db, name)
}
Self::NonGeneric(ClassLiteral::Static(class)) => {
if class.is_typed_dict(db) {
return Place::Undefined.into();
@@ -1681,9 +1641,6 @@ impl<'db> ClassType<'db> {
Self::NonGeneric(ClassLiteral::Dynamic(dynamic)) => {
dynamic.own_instance_member(db, name)
}
Self::NonGeneric(ClassLiteral::DynamicNamedTuple(namedtuple)) => {
namedtuple.own_instance_member(db, name)
}
Self::NonGeneric(ClassLiteral::Static(class_literal)) => {
class_literal.own_instance_member(db, name)
}
@@ -1946,9 +1903,7 @@ impl<'db> VarianceInferable<'db> for ClassType<'db> {
fn variance_of(self, db: &'db dyn Db, typevar: BoundTypeVarInstance<'db>) -> TypeVarVariance {
match self {
Self::NonGeneric(ClassLiteral::Static(class)) => class.variance_of(db, typevar),
Self::NonGeneric(ClassLiteral::Dynamic(_) | ClassLiteral::DynamicNamedTuple(_)) => {
TypeVarVariance::Bivariant
}
Self::NonGeneric(ClassLiteral::Dynamic(_)) => TypeVarVariance::Bivariant,
Self::Generic(generic) => generic.variance_of(db, typevar),
}
}
@@ -2105,20 +2060,6 @@ impl<'db> StaticClassLiteral<'db> {
self.is_known(db, KnownClass::Tuple)
}
/// Returns `true` if this class inherits from a functional namedtuple
/// (`DynamicNamedTupleLiteral`) that has unknown fields.
///
/// When the base namedtuple's fields were determined dynamically (e.g., from a variable),
/// we can't synthesize precise method signatures and should fall back to `NamedTupleFallback`.
pub(crate) fn namedtuple_base_has_unknown_fields(self, db: &'db dyn Db) -> bool {
self.explicit_bases(db).iter().any(|base| match base {
Type::ClassLiteral(ClassLiteral::DynamicNamedTuple(namedtuple)) => {
!namedtuple.has_known_fields(db)
}
_ => false,
})
}
/// Returns a new [`StaticClassLiteral`] with the given dataclass params, preserving all other fields.
pub(crate) fn with_dataclass_params(
self,
@@ -2200,8 +2141,6 @@ impl<'db> StaticClassLiteral<'db> {
return Some(ty);
}
}
// Dynamic namedtuples don't define their own ordering methods.
ClassLiteral::DynamicNamedTuple(_) => {}
}
}
}
@@ -3222,44 +3161,37 @@ impl<'db> StaticClassLiteral<'db> {
.with_annotated_type(instance_ty);
signature_from_fields(vec![self_parameter], Type::none(db))
}
(
CodeGeneratorKind::NamedTuple,
"__new__" | "__init__" | "_replace" | "__replace__" | "_fields",
) if self.namedtuple_base_has_unknown_fields(db) => {
// When the namedtuple base has unknown fields, fall back to NamedTupleFallback
// which has generic signatures that accept any arguments.
KnownClass::NamedTupleFallback
.to_class_literal(db)
.as_class_literal()?
.as_static()?
.own_class_member(db, inherited_generic_context, None, name)
.ignore_possibly_undefined()
.map(|ty| {
ty.apply_type_mapping(
db,
&TypeMapping::ReplaceSelf {
new_upper_bound: instance_ty,
},
TypeContext::default(),
)
})
(CodeGeneratorKind::NamedTuple, "__new__") => {
let cls_parameter = Parameter::positional_or_keyword(Name::new_static("cls"))
.with_annotated_type(KnownClass::Type.to_instance(db));
signature_from_fields(vec![cls_parameter], Type::none(db))
}
(CodeGeneratorKind::NamedTuple, "__new__" | "_replace" | "__replace__" | "_fields") => {
let fields = self.fields(db, specialization, field_policy);
let fields_iter = fields.iter().map(|(name, field)| {
let default_ty = match &field.kind {
FieldKind::NamedTuple { default_ty } => *default_ty,
_ => None,
};
(name.clone(), field.declared_ty, default_ty)
});
synthesize_namedtuple_class_member(
(CodeGeneratorKind::NamedTuple, "_replace" | "__replace__") => {
if name == "__replace__"
&& Program::get(db).python_version(db) < PythonVersion::PY313
{
return None;
}
// Use `Self` type variable as return type so that subclasses get the correct
// return type when calling `_replace`. For example, if `IntBox` inherits from
// `Box[int]` (a NamedTuple), then `IntBox(1)._replace(content=42)` should return
// `IntBox`, not `Box[int]`.
let self_ty = Type::TypeVar(BoundTypeVarInstance::synthetic_self(
db,
name,
instance_ty,
fields_iter,
specialization.map(|s| s.generic_context(db)),
)
BindingContext::Synthetic,
));
let self_parameter = Parameter::positional_or_keyword(Name::new_static("self"))
.with_annotated_type(self_ty);
signature_from_fields(vec![self_parameter], self_ty)
}
(CodeGeneratorKind::NamedTuple, "_fields") => {
// Synthesize a precise tuple type for _fields using literal string types.
// For example, a NamedTuple with `name` and `age` fields gets
// `tuple[Literal["name"], Literal["age"]]`.
let fields = self.fields(db, specialization, field_policy);
let field_types = fields.keys().map(|name| Type::string_literal(db, name));
Some(Type::heterogeneous_tuple(db, field_types))
}
(CodeGeneratorKind::DataclassLike(_), "__lt__" | "__le__" | "__gt__" | "__ge__") => {
if !has_dataclass_param(DataclassFlags::ORDER) {
@@ -4744,7 +4676,7 @@ impl<'db> VarianceInferable<'db> for ClassLiteral<'db> {
fn variance_of(self, db: &'db dyn Db, typevar: BoundTypeVarInstance<'db>) -> TypeVarVariance {
match self {
Self::Static(class) => class.variance_of(db, typevar),
Self::Dynamic(_) | Self::DynamicNamedTuple(_) => TypeVarVariance::Bivariant,
Self::Dynamic(_) => TypeVarVariance::Bivariant,
}
}
}
@@ -5154,404 +5086,6 @@ pub(crate) struct DynamicMetaclassConflict<'db> {
pub(crate) base2: ClassBase<'db>,
}
/// Create a property type for a namedtuple field.
fn create_field_property<'db>(db: &'db dyn Db, field_ty: Type<'db>) -> Type<'db> {
let property_getter_signature = Signature::new(
Parameters::new(
db,
[Parameter::positional_only(Some(Name::new_static("self")))],
),
field_ty,
);
let property_getter = Type::single_callable(db, property_getter_signature);
let property = PropertyInstanceType::new(db, Some(property_getter), None);
Type::PropertyInstance(property)
}
/// Synthesize a namedtuple class member given the field information.
///
/// This is used by both `DynamicNamedTupleLiteral` and `StaticClassLiteral` (for declarative
/// namedtuples) to avoid duplicating the synthesis logic.
///
/// The `inherited_generic_context` parameter is used for declarative namedtuples to preserve
/// generic context in the synthesized `__new__` signature.
fn synthesize_namedtuple_class_member<'db>(
db: &'db dyn Db,
name: &str,
instance_ty: Type<'db>,
fields: impl Iterator<Item = (Name, Type<'db>, Option<Type<'db>>)>,
inherited_generic_context: Option<GenericContext<'db>>,
) -> Option<Type<'db>> {
match name {
"__new__" => {
// __new__(cls, field1, field2, ...) -> Self
let mut parameters = vec![
Parameter::positional_or_keyword(Name::new_static("cls"))
.with_annotated_type(KnownClass::Type.to_instance(db)),
];
for (field_name, field_ty, default_ty) in fields {
let mut param =
Parameter::positional_or_keyword(field_name).with_annotated_type(field_ty);
if let Some(default) = default_ty {
param = param.with_default_type(default);
}
parameters.push(param);
}
let signature = Signature::new_generic(
inherited_generic_context,
Parameters::new(db, parameters),
instance_ty,
);
Some(Type::function_like_callable(db, signature))
}
"_fields" => {
// _fields: tuple[Literal["field1"], Literal["field2"], ...]
let field_types =
fields.map(|(field_name, _, _)| Type::string_literal(db, &field_name));
Some(Type::heterogeneous_tuple(db, field_types))
}
"_replace" | "__replace__" => {
if name == "__replace__" && Program::get(db).python_version(db) < PythonVersion::PY313 {
return None;
}
// _replace(self, *, field1=..., field2=...) -> Self
let self_ty = Type::TypeVar(BoundTypeVarInstance::synthetic_self(
db,
instance_ty,
BindingContext::Synthetic,
));
let mut parameters = vec![
Parameter::positional_or_keyword(Name::new_static("self"))
.with_annotated_type(self_ty),
];
for (field_name, field_ty, _) in fields {
parameters.push(
Parameter::keyword_only(field_name)
.with_annotated_type(field_ty)
.with_default_type(field_ty),
);
}
let signature = Signature::new(Parameters::new(db, parameters), self_ty);
Some(Type::function_like_callable(db, signature))
}
"__init__" => {
// Namedtuples don't have a custom __init__. All construction happens in __new__.
None
}
_ => {
// Fall back to NamedTupleFallback for other synthesized methods.
KnownClass::NamedTupleFallback
.to_class_literal(db)
.as_class_literal()?
.as_static()?
.own_class_member(db, inherited_generic_context, None, name)
.ignore_possibly_undefined()
}
}
}
/// A namedtuple created via the functional form `namedtuple(name, fields)` or
/// `NamedTuple(name, fields)`.
///
/// For example:
/// ```python
/// from collections import namedtuple
/// Point = namedtuple("Point", ["x", "y"])
///
/// from typing import NamedTuple
/// Person = NamedTuple("Person", [("name", str), ("age", int)])
/// ```
///
/// The type of `Point` would be `type[Point]` where `Point` is a `DynamicNamedTupleLiteral`.
#[salsa::interned(debug, heap_size = ruff_memory_usage::heap_size)]
#[derive(PartialOrd, Ord)]
pub struct DynamicNamedTupleLiteral<'db> {
/// The name of the namedtuple (from the first argument).
#[returns(ref)]
pub name: Name,
/// The fields as (name, type, default) tuples.
/// For `collections.namedtuple`, all types are `Any`.
/// For `typing.NamedTuple`, types come from the field definitions.
/// The third element is the default type, if any.
#[returns(ref)]
pub fields: Box<[(Name, Type<'db>, Option<Type<'db>>)]>,
/// Whether the fields are known statically.
///
/// When `true`, the fields were determined from a literal (list or tuple).
/// When `false`, the fields argument was dynamic (e.g., a variable),
/// and attribute lookups should return `Any` instead of failing.
pub has_known_fields: bool,
/// The anchor for this dynamic namedtuple, providing stable identity.
///
/// - `Definition`: The call is assigned to a variable. The definition
/// uniquely identifies this namedtuple and can be used to find the call.
/// - `ScopeOffset`: The call is "dangling" (not assigned). The offset
/// is relative to the enclosing scope's anchor node index.
pub anchor: DynamicClassAnchor<'db>,
}
impl get_size2::GetSize for DynamicNamedTupleLiteral<'_> {}
#[salsa::tracked]
impl<'db> DynamicNamedTupleLiteral<'db> {
/// Returns the definition where this namedtuple is created, if it was assigned to a variable.
pub(crate) fn definition(self, db: &'db dyn Db) -> Option<Definition<'db>> {
match self.anchor(db) {
DynamicClassAnchor::Definition(definition) => Some(definition),
DynamicClassAnchor::ScopeOffset { .. } => None,
}
}
/// Returns the scope in which this dynamic class was created.
pub(crate) fn scope(self, db: &'db dyn Db) -> ScopeId<'db> {
match self.anchor(db) {
DynamicClassAnchor::Definition(definition) => definition.scope(db),
DynamicClassAnchor::ScopeOffset { scope, .. } => scope,
}
}
/// Returns an instance type for this dynamic namedtuple.
pub(crate) fn to_instance(self, db: &'db dyn Db) -> Type<'db> {
Type::instance(db, ClassType::NonGeneric(self.into()))
}
/// Returns the range of the namedtuple call expression.
pub(crate) fn header_range(self, db: &'db dyn Db) -> TextRange {
let scope = self.scope(db);
let file = scope.file(db);
let module = parsed_module(db, file).load(db);
match self.anchor(db) {
DynamicClassAnchor::Definition(definition) => {
// For definitions, get the range from the definition's value.
// The namedtuple call is the value of the assignment.
definition
.kind(db)
.value(&module)
.expect("DynamicClassAnchor::Definition should only be used for assignments")
.range()
}
DynamicClassAnchor::ScopeOffset { offset, .. } => {
// For dangling calls, compute the absolute index from the offset.
let scope_anchor = scope.node(db).node_index().unwrap_or(NodeIndex::from(0));
let anchor_u32 = scope_anchor
.as_u32()
.expect("anchor should not be NodeIndex::NONE");
let absolute_index = NodeIndex::from(anchor_u32 + offset);
// Get the node and return its range.
let node: &ast::ExprCall = module
.get_by_index(absolute_index)
.try_into()
.expect("scope offset should point to ExprCall");
node.range()
}
}
}
/// Returns a [`Span`] pointing to the namedtuple call expression.
pub(super) fn header_span(self, db: &'db dyn Db) -> Span {
Span::from(self.scope(db).file(db)).with_range(self.header_range(db))
}
/// Compute the MRO for this namedtuple.
///
/// The MRO is `[self, tuple[field_types...], object]`.
/// For example, `namedtuple("Point", [("x", int), ("y", int)])` has MRO
/// `[Point, tuple[int, int], object]`.
#[salsa::tracked(returns(ref), heap_size = ruff_memory_usage::heap_size)]
pub(crate) fn mro(self, db: &'db dyn Db) -> Mro<'db> {
let self_base = ClassBase::Class(ClassType::NonGeneric(self.into()));
let tuple_class = self.tuple_base_class(db);
let object_class = KnownClass::Object
.to_class_literal(db)
.as_class_literal()
.expect("object should be a class literal")
.default_specialization(db);
Mro::from([
self_base,
ClassBase::Class(tuple_class),
ClassBase::Class(object_class),
])
}
/// Get the metaclass of this dynamic namedtuple.
///
/// Namedtuples always have `type` as their metaclass.
pub(crate) fn metaclass(self, db: &'db dyn Db) -> Type<'db> {
let _ = self;
KnownClass::Type.to_class_literal(db)
}
/// Compute the specialized tuple class that this namedtuple inherits from.
///
/// For example, `namedtuple("Point", [("x", int), ("y", int)])` inherits from `tuple[int, int]`.
pub(crate) fn tuple_base_class(self, db: &'db dyn Db) -> ClassType<'db> {
// If fields are unknown, return `tuple[Unknown, ...]` to avoid false positives
// like index-out-of-bounds errors.
if !self.has_known_fields(db) {
return TupleType::homogeneous(db, Type::unknown()).to_class_type(db);
}
let field_types = self.fields(db).iter().map(|(_, ty, _)| *ty);
TupleType::heterogeneous(db, field_types)
.map(|t| t.to_class_type(db))
.unwrap_or_else(|| {
KnownClass::Tuple
.to_class_literal(db)
.as_class_literal()
.expect("tuple should be a class literal")
.default_specialization(db)
})
}
/// Look up an instance member defined directly on this class (not inherited).
///
/// For dynamic namedtuples, instance members are the field names.
/// If fields are unknown (dynamic), returns `Any` for any attribute.
pub(super) fn own_instance_member(self, db: &'db dyn Db, name: &str) -> Member<'db> {
for (field_name, field_ty, _) in self.fields(db).as_ref() {
if field_name.as_str() == name {
return Member::definitely_declared(*field_ty);
}
}
if !self.has_known_fields(db) {
return Member::definitely_declared(Type::any());
}
Member::unbound()
}
/// Look up an instance member by name (including superclasses).
pub(crate) fn instance_member(self, db: &'db dyn Db, name: &str) -> PlaceAndQualifiers<'db> {
// First check own instance members.
let result = self.own_instance_member(db, name);
if !result.is_undefined() {
return result.inner;
}
// Fall back to the tuple base type for other attributes.
Type::instance(db, self.tuple_base_class(db)).instance_member(db, name)
}
/// Look up a class-level member by name.
pub(crate) fn class_member(
self,
db: &'db dyn Db,
name: &str,
policy: MemberLookupPolicy,
) -> PlaceAndQualifiers<'db> {
// First check synthesized members and fields.
let member = self.own_class_member(db, name);
if !member.is_undefined() {
return member.inner;
}
// Fall back to tuple class members.
let result = self
.tuple_base_class(db)
.class_literal(db)
.class_member(db, name, policy);
// If fields are unknown (dynamic) and the attribute wasn't found,
// return `Any` instead of failing.
if !self.has_known_fields(db) && result.place.is_undefined() {
return Place::bound(Type::any()).into();
}
result
}
/// Look up a class-level member defined directly on this class (not inherited).
///
/// This only checks synthesized members and field properties, without falling
/// back to tuple or other base classes.
pub(super) fn own_class_member(self, db: &'db dyn Db, name: &str) -> Member<'db> {
// Handle synthesized namedtuple attributes.
if let Some(ty) = self.synthesized_class_member(db, name) {
return Member::definitely_declared(ty);
}
// Check if it's a field name (returns a property descriptor).
for (field_name, field_ty, _) in self.fields(db).as_ref() {
if field_name.as_str() == name {
return Member::definitely_declared(create_field_property(db, *field_ty));
}
}
Member::default()
}
/// Generate synthesized class members for namedtuples.
fn synthesized_class_member(self, db: &'db dyn Db, name: &str) -> Option<Type<'db>> {
let instance_ty = self.to_instance(db);
// When fields are unknown, handle constructor and field-specific methods specially.
if !self.has_known_fields(db) {
match name {
// For constructors, return a gradual signature that accepts any arguments.
"__new__" | "__init__" => {
let signature = Signature::new(Parameters::gradual_form(), instance_ty);
return Some(Type::function_like_callable(db, signature));
}
// For other field-specific methods, fall through to NamedTupleFallback.
"_fields" | "_replace" | "__replace__" => {
return KnownClass::NamedTupleFallback
.to_class_literal(db)
.as_class_literal()?
.as_static()?
.own_class_member(db, None, None, name)
.ignore_possibly_undefined()
.map(|ty| {
ty.apply_type_mapping(
db,
&TypeMapping::ReplaceSelf {
new_upper_bound: instance_ty,
},
TypeContext::default(),
)
});
}
_ => {}
}
}
let result = synthesize_namedtuple_class_member(
db,
name,
instance_ty,
self.fields(db).iter().cloned(),
None,
);
// For fallback members from NamedTupleFallback, apply type mapping to handle
// `Self` types. The explicitly synthesized members (__new__, _fields, _replace,
// __replace__) don't need this mapping.
if matches!(name, "__new__" | "_fields" | "_replace" | "__replace__") {
result
} else {
result.map(|ty| {
ty.apply_type_mapping(
db,
&TypeMapping::ReplaceSelf {
new_upper_bound: instance_ty,
},
TypeContext::default(),
)
})
}
}
}
/// Performs member lookups over an MRO (Method Resolution Order).
///
/// This struct encapsulates the shared logic for looking up class and instance
@@ -5828,11 +5362,6 @@ impl<'db> QualifiedClassName<'db> {
let scope = class.scope(self.db);
(scope.file(self.db), scope.file_scope_id(self.db), 0)
}
ClassLiteral::DynamicNamedTuple(namedtuple) => {
// Dynamic namedtuples don't have a body scope; start from the enclosing scope.
let scope = namedtuple.scope(self.db);
(scope.file(self.db), scope.file_scope_id(self.db), 0)
}
};
let module_ast = parsed_module(self.db, file).load(self.db);

View File

@@ -69,7 +69,6 @@ pub(crate) fn enum_metadata<'db>(
// ```
return None;
}
ClassLiteral::DynamicNamedTuple(..) => return None,
};
// This is a fast path to avoid traversing the MRO of known classes

File diff suppressed because it is too large Load Diff

View File

@@ -42,9 +42,6 @@ impl<'db> Type<'db> {
ClassLiteral::Dynamic(_) => {
Type::NominalInstance(NominalInstanceType(NominalInstanceInner::NonTuple(class)))
}
ClassLiteral::DynamicNamedTuple(_) => {
Type::NominalInstance(NominalInstanceType(NominalInstanceInner::NonTuple(class)))
}
ClassLiteral::Static(class_literal) => {
let specialization = class.into_generic_alias().map(|g| g.specialization(db));
match class_literal.known(db) {

View File

@@ -500,9 +500,6 @@ impl<'db> MroIterator<'db> {
ClassLiteral::Dynamic(literal) => {
ClassBase::Class(ClassType::NonGeneric(literal.into()))
}
ClassLiteral::DynamicNamedTuple(literal) => {
ClassBase::Class(ClassType::NonGeneric(literal.into()))
}
}
}
@@ -527,11 +524,6 @@ impl<'db> MroIterator<'db> {
full_mro_iter.next();
full_mro_iter
}
ClassLiteral::DynamicNamedTuple(literal) => {
let mut full_mro_iter = literal.mro(self.db).iter();
full_mro_iter.next();
full_mro_iter
}
})
}
}

View File

@@ -163,6 +163,13 @@ export default function Chrome({
[workspace, files.index, onRemoveFile],
);
const handleChange = useCallback(
(content: string) => {
onChangeFile(workspace, content);
},
[onChangeFile, workspace],
);
const { defaultLayout, onLayoutChange } = useDefaultLayout({
groupId: "editor-diagnostics",
storage: localStorage,
@@ -221,7 +228,7 @@ export default function Chrome({
diagnostics={checkResult.diagnostics}
workspace={workspace}
onMount={handleEditorMount}
onChange={(content) => onChangeFile(workspace, content)}
onChange={handleChange}
onOpenFile={onSelectFile}
onVendoredFileChange={onSelectVendoredFile}
onBackToUserFile={handleBackToUserFile}

View File

@@ -58,7 +58,7 @@ export default function Playground() {
}
}, [files]);
const handleFileAdded = (workspace: Workspace, name: string) => {
const handleFileAdded = useCallback((workspace: Workspace, name: string) => {
let handle = null;
if (name === SETTINGS_FILE_NAME) {
@@ -68,69 +68,74 @@ export default function Playground() {
}
dispatchFiles({ type: "add", name, handle, content: "" });
};
}, []);
const handleFileChanged = (workspace: Workspace, content: string) => {
if (files.selected == null) {
return;
}
const handleFileChanged = useCallback(
(workspace: Workspace, content: string) => {
if (files.selected == null) {
return;
}
dispatchFiles({
type: "change",
id: files.selected,
content,
});
const handle = files.handles[files.selected];
const handle = files.handles[files.selected];
if (handle != null) {
updateFile(workspace, handle, content, setError);
} else if (fileName === SETTINGS_FILE_NAME) {
updateOptions(workspace, content, setError);
}
if (handle != null) {
updateFile(workspace, handle, content, setError);
} else if (fileName === SETTINGS_FILE_NAME) {
updateOptions(workspace, content, setError);
}
};
dispatchFiles({
type: "change",
id: files.selected,
content,
});
},
[fileName, files.handles, files.selected],
);
const handleFileRenamed = (
workspace: Workspace,
file: FileId,
newName: string,
) => {
if (newName.startsWith("/")) {
setError("File names cannot start with '/'.");
return;
}
if (newName.startsWith("vendored:")) {
setError("File names cannot start with 'vendored:'.");
return;
}
const handleFileRenamed = useCallback(
(workspace: Workspace, file: FileId, newName: string) => {
if (newName.startsWith("/")) {
setError("File names cannot start with '/'.");
return;
}
if (newName.startsWith("vendored:")) {
setError("File names cannot start with 'vendored:'.");
return;
}
const handle = files.handles[file];
let newHandle: FileHandle | null = null;
if (handle == null) {
updateOptions(workspace, null, setError);
} else {
workspace.closeFile(handle);
}
const handle = files.handles[file];
let newHandle: FileHandle | null = null;
if (handle == null) {
updateOptions(workspace, null, setError);
} else {
workspace.closeFile(handle);
}
if (newName === SETTINGS_FILE_NAME) {
updateOptions(workspace, files.contents[file], setError);
} else {
newHandle = workspace.openFile(newName, files.contents[file]);
}
if (newName === SETTINGS_FILE_NAME) {
updateOptions(workspace, files.contents[file], setError);
} else {
newHandle = workspace.openFile(newName, files.contents[file]);
}
dispatchFiles({ type: "rename", id: file, to: newName, newHandle });
};
dispatchFiles({ type: "rename", id: file, to: newName, newHandle });
},
[files.contents, files.handles],
);
const handleFileRemoved = (workspace: Workspace, file: FileId) => {
const handle = files.handles[file];
if (handle == null) {
updateOptions(workspace, null, setError);
} else {
workspace.closeFile(handle);
}
const handleFileRemoved = useCallback(
(workspace: Workspace, file: FileId) => {
const handle = files.handles[file];
if (handle == null) {
updateOptions(workspace, null, setError);
} else {
workspace.closeFile(handle);
}
dispatchFiles({ type: "remove", id: file });
};
dispatchFiles({ type: "remove", id: file });
},
[files.handles],
);
const handleFileSelected = useCallback((file: FileId) => {
dispatchFiles({ type: "selectFile", id: file });