some cleanup

This commit is contained in:
Eric Mark Martin 2025-11-17 20:45:01 -05:00
parent bf9857e056
commit 27b7152159
4 changed files with 8 additions and 14 deletions

View file

@ -16,9 +16,6 @@ def f(*args: Unpack[Ts]) -> tuple[Unpack[Ts]]:
reveal_type(args) # revealed: tuple[@Todo(`Unpack[]` special form), ...]
return args
def g() -> TypeGuard[int]:
return True
def i(callback: Callable[Concatenate[int, P], R_co], *args: P.args, **kwargs: P.kwargs) -> R_co:
reveal_type(args) # revealed: P@i.args
reveal_type(kwargs) # revealed: P@i.kwargs

View file

@ -12,17 +12,14 @@ from typing_extensions import TypeGuard, TypeIs
def _(
a: TypeGuard[str],
b: TypeIs[str | int],
c: TypeGuard[Intersection[complex, Not[int], Not[float]]],
c: TypeGuard[bool],
d: TypeIs[tuple[TypeOf[bytes]]],
e: TypeGuard, # error: [invalid-type-form]
f: TypeIs, # error: [invalid-type-form]
):
reveal_type(a) # revealed: TypeGuard[str]
reveal_type(b) # revealed: TypeIs[str | int]
# not `TypeGuard[complex & ~int & ~float]`: `complex` in argument position
# means `complex & int & float` semantically so `Intersection[complex,
# Not[int], Not[float]]` means `complex` semantically
reveal_type(c) # revealed: TypeGuard[complex]
reveal_type(c) # revealed: TypeGuard[bool]
reveal_type(d) # revealed: TypeIs[tuple[<class 'bytes'>]]
reveal_type(e) # revealed: Unknown
reveal_type(f) # revealed: Unknown

View file

@ -14750,7 +14750,8 @@ impl<'db> TypeGuardType<'db> {
}
impl<'db> VarianceInferable<'db> for TypeGuardType<'db> {
// TODO: comment
// `TypeGuard` is covariant in its type parameter. See the `TypeGuard`
// section of mdtest/generics/pep695/variance.md for details.
fn variance_of(self, db: &'db dyn Db, typevar: BoundTypeVarInstance<'db>) -> TypeVarVariance {
self.return_type(db).variance_of(db, typevar)
}

View file

@ -288,10 +288,9 @@ impl ClassInfoConstraintFunction {
/// in the second clobbers the first.
#[derive(Hash, PartialEq, Debug, Eq, Clone, Copy)]
struct Conjunction<'db> {
/// The intersected constraints (represented as an intersection type)
/// The intersected constraints (represented as a type to intersect the guard with)
constraint: Type<'db>,
/// If any constraint in this conjunction is a `TypeGuard`, this is Some and
/// contains the union of all `TypeGuard` types in this conjunction
/// If any constraint in this conjunction is a `TypeGuard[T]`, this is `Some(T)`
typeguard: Option<Type<'db>>,
}
@ -343,10 +342,10 @@ impl<'db> Conjunction<'db> {
///
/// - `f(x) or g(x)` where f returns `TypeIs[A]` and g returns `TypeGuard[B]`
/// => `[Conjunction { constraint: A, typeguard: None }, Conjunction { constraint: object, typeguard: Some(B) }]`
/// => evaluates to `A | B`
/// => evaluates to `(P & A) | B`, where `P` is our previously-known type
#[derive(Hash, PartialEq, Debug, Eq, Clone)]
struct NarrowingConstraint<'db> {
/// Disjunctions of conjunctions (DNF)
/// Disjunction of conjunctions (DNF)
disjuncts: SmallVec<[Conjunction<'db>; 4]>,
}