[ty] implement disjointness of Callable vs SpecialForm (#18503)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary

Fixes https://github.com/astral-sh/ty/issues/557

## Test Plan

Stable property tests succeed with a million iterations. Added mdtests.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Carl Meyer 2025-06-10 13:25:08 -07:00 committed by GitHub
parent eb60bd64fd
commit a2de81cb27
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 109 additions and 0 deletions

View file

@ -1926,6 +1926,15 @@ impl<'db> Type<'db> {
true
}
(Type::Callable(_), Type::SpecialForm(special_form))
| (Type::SpecialForm(special_form), Type::Callable(_)) => {
// A callable type is disjoint from special form types, except for special forms
// that are callable (like TypedDict and collection constructors).
// Most special forms are type constructors/annotations (like `typing.Literal`,
// `typing.Union`, etc.) that are subscripted, not called.
!special_form.is_callable()
}
(
Type::Callable(_) | Type::DataclassDecorator(_) | Type::DataclassTransformer(_),
instance @ Type::NominalInstance(NominalInstanceType { class, .. }),

View file

@ -247,6 +247,59 @@ impl SpecialFormType {
self.class().to_class_literal(db)
}
/// Return true if this special form is callable at runtime.
/// Most special forms are not callable (they are type constructors that are subscripted),
/// but some like `TypedDict` and collection constructors can be called.
pub(super) const fn is_callable(self) -> bool {
match self {
// TypedDict can be called as a constructor to create TypedDict types
Self::TypedDict
// Collection constructors are callable
// TODO actually implement support for calling them
| Self::ChainMap
| Self::Counter
| Self::DefaultDict
| Self::Deque
| Self::OrderedDict => true,
// All other special forms are not callable
Self::Annotated
| Self::Literal
| Self::LiteralString
| Self::Optional
| Self::Union
| Self::NoReturn
| Self::Never
| Self::Tuple
| Self::List
| Self::Dict
| Self::Set
| Self::FrozenSet
| Self::Type
| Self::Unknown
| Self::AlwaysTruthy
| Self::AlwaysFalsy
| Self::Not
| Self::Intersection
| Self::TypeOf
| Self::CallableTypeOf
| Self::Callable
| Self::TypingSelf
| Self::Final
| Self::ClassVar
| Self::Concatenate
| Self::Unpack
| Self::Required
| Self::NotRequired
| Self::TypeAlias
| Self::TypeGuard
| Self::TypeIs
| Self::ReadOnly
| Self::Protocol
| Self::Generic => false,
}
}
/// Return the repr of the symbol at runtime
pub(super) const fn repr(self) -> &'static str {
match self {