mirror of https://github.com/astral-sh/ruff
fix panic
This commit is contained in:
parent
5b0a93b3c0
commit
7c34ed3374
|
|
@ -133,6 +133,17 @@ def _[T](x: A | B):
|
||||||
reveal_type(x) # revealed: A[int] | B
|
reveal_type(x) # revealed: A[int] | B
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Narrowing for tuple
|
||||||
|
|
||||||
|
An early version of <https://github.com/astral-sh/ruff/pull/19920> caused us to crash on this:
|
||||||
|
|
||||||
|
```py
|
||||||
|
def _(val):
|
||||||
|
if type(val) is tuple:
|
||||||
|
# TODO: better would be `Unknown & tuple[object, ...]`
|
||||||
|
reveal_type(val) # revealed: Unknown & tuple[Unknown, ...]
|
||||||
|
```
|
||||||
|
|
||||||
## Limitations
|
## Limitations
|
||||||
|
|
||||||
```py
|
```py
|
||||||
|
|
|
||||||
|
|
@ -353,13 +353,18 @@ impl<'db> GenericContext<'db> {
|
||||||
db: &'db dyn Db,
|
db: &'db dyn Db,
|
||||||
types: Box<[Type<'db>]>,
|
types: Box<[Type<'db>]>,
|
||||||
) -> Specialization<'db> {
|
) -> Specialization<'db> {
|
||||||
assert_eq!(self.variables(db).len(), types.len());
|
match self.inner(db) {
|
||||||
debug_assert!(
|
GenericContextInner::Tuple { .. } => {
|
||||||
matches!(self.inner(db), GenericContextInner::NonTuple(_)),
|
assert_eq!(types.len(), 1);
|
||||||
"Should never call `GenericContext::specialize` on a tuple context"
|
let tuple = TupleType::homogeneous(db, types[0]);
|
||||||
);
|
Specialization::new(db, self, SpecializationInner::Tuple(tuple))
|
||||||
|
}
|
||||||
|
GenericContextInner::NonTuple(variables) => {
|
||||||
|
assert_eq!(variables.len(), types.len());
|
||||||
Specialization::new(db, self, SpecializationInner::NonTuple(types))
|
Specialization::new(db, self, SpecializationInner::NonTuple(types))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a specialization of this generic context for the `tuple` class.
|
/// Creates a specialization of this generic context for the `tuple` class.
|
||||||
pub(crate) fn specialize_tuple(
|
pub(crate) fn specialize_tuple(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue