ruff/crates/ty_python_semantic/src
Douglas Creager ce43dbab58
[ty] Promote literals when inferring class specializations from constructors (#18102)
This implements the stopgap approach described in
https://github.com/astral-sh/ty/issues/336#issuecomment-2880532213 for
handling literal types in generic class specializations.

With this approach, we will promote any literal to its instance type,
but _only_ when inferring a generic class specialization from a
constructor call:

```py
class C[T]:
    def __init__(self, x: T) -> None: ...

reveal_type(C("string"))  # revealed: C[str]
```

If you specialize the class explicitly, we still use whatever type you
provide, even if it's a literal:

```py
from typing import Literal

reveal_type(C[Literal[5]](5))  # revealed: C[Literal[5]]
```

And this doesn't apply at all to generic functions:

```py
def f[T](x: T) -> T:
    return x

reveal_type(f(5))  # revealed: Literal[5]
```

---

As part of making this happen, we also generalize the `TypeMapping`
machinery. This provides a way to apply a function to type, returning a
new type. Complicating matters is that for function literals, we have to
apply the mapping lazily, since the function's signature is not created
until (and if) someone calls its `signature` method. That means we have
to stash away the mappings that we want to apply to the signatures
parameter/return annotations once we do create it. This requires some
minor `Cow` shenanigans to continue working for partial specializations.
2025-05-19 15:42:54 -04:00
..
module_resolver [ty] Migrate the namespace package module resolver tests to mdtests (#18133) 2025-05-16 19:56:33 +02:00
semantic_index Revert "[ty] Better control flow for boolean expressions that are inside if (#18010)" (#18150) 2025-05-17 08:27:32 -04:00
types [ty] Promote literals when inferring class specializations from constructors (#18102) 2025-05-19 15:42:54 -04:00
util Use #[expect(lint)] over #[allow(lint)] where possible (#17822) 2025-05-03 21:20:31 +02:00
ast_node_ref.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
db.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
dunder_all.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
lib.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
lint.rs [ty] Ignore possibly-unresolved-reference by default (#17934) 2025-05-08 17:44:56 +02:00
list.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
module_name.rs Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
node_key.rs Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
program.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
python_platform.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
semantic_index.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
semantic_model.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
site_packages.rs [ty] Add regression test for fixed pyvenv.cfg parsing bug (#18157) 2025-05-17 21:10:15 +00:00
suppression.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
symbol.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
types.rs [ty] Promote literals when inferring class specializations from constructors (#18102) 2025-05-19 15:42:54 -04:00
unpack.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00