mirror of https://github.com/astral-sh/ruff
Add regression test and comment for `GenericContext::from_type_params`
This commit is contained in:
parent
7c34ed3374
commit
c95cdb3d1d
|
|
@ -617,5 +617,34 @@ class C[T](C): ...
|
||||||
class D[T](D[int]): ...
|
class D[T](D[int]): ...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Tuple as a PEP-695 generic class
|
||||||
|
|
||||||
|
Our special handling for `tuple` does not break if `tuple` is defined as a PEP-695 generic class in
|
||||||
|
typeshed:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[environment]
|
||||||
|
python-version = "3.12"
|
||||||
|
typeshed = "/typeshed"
|
||||||
|
```
|
||||||
|
|
||||||
|
`/typeshed/stdlib/builtins.pyi`:
|
||||||
|
|
||||||
|
```pyi
|
||||||
|
class tuple[T]: ...
|
||||||
|
```
|
||||||
|
|
||||||
|
`/typeshed/stdlib/typing_extensions.pyi`:
|
||||||
|
|
||||||
|
```pyi
|
||||||
|
def reveal_type(obj, /): ...
|
||||||
|
```
|
||||||
|
|
||||||
|
`main.py`:
|
||||||
|
|
||||||
|
```py
|
||||||
|
reveal_type((1, 2, 3)) # revealed: tuple[Literal[1], Literal[2], Literal[3]]
|
||||||
|
```
|
||||||
|
|
||||||
[crtp]: https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
|
[crtp]: https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
|
||||||
[f-bound]: https://en.wikipedia.org/wiki/Bounded_quantification#F-bounded_quantification
|
[f-bound]: https://en.wikipedia.org/wiki/Bounded_quantification#F-bounded_quantification
|
||||||
|
|
|
||||||
|
|
@ -148,6 +148,12 @@ impl<'db> GenericContext<'db> {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
match known_class {
|
match known_class {
|
||||||
|
// As of 15/08/2025, this branch is never taken, since `tuple` in typeshed
|
||||||
|
// does not have any PEP-695 type parameters. However, it will presumably
|
||||||
|
// at some point have its stub definition rewritten to use PEP-695 type parameters,
|
||||||
|
// and a custom typeshed could also reasonably define `tuple` in `builtins.pyi`
|
||||||
|
// with PEP-695 type parameters. (We have a regression test for this in
|
||||||
|
// `generics/pep695/classes.md`.)
|
||||||
Some(KnownClass::Tuple) => {
|
Some(KnownClass::Tuple) => {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
variables.len(),
|
variables.len(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue