Skip to content

Commit 4a0f6a6

Browse files
committed
[ty] Move Type::subtyping_is_always_reflexive to types::relation
1 parent 6ecfe28 commit 4a0f6a6

2 files changed

Lines changed: 47 additions & 47 deletions

File tree

crates/ty_python_semantic/src/types.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,53 +1837,6 @@ impl<'db> Type<'db> {
18371837
}
18381838
}
18391839

1840-
/// Return `true` if subtyping is always reflexive for this type; `T <: T` is always true for
1841-
/// any `T` of this type.
1842-
///
1843-
/// This is true for fully static types, but also for some types that may not be fully static.
1844-
/// For example, a `ClassLiteral` may inherit `Any`, but its subtyping is still reflexive.
1845-
///
1846-
/// This method may have false negatives, but it should not have false positives. It should be
1847-
/// a cheap shallow check, not an exhaustive recursive check.
1848-
const fn subtyping_is_always_reflexive(self) -> bool {
1849-
match self {
1850-
Type::Never
1851-
| Type::FunctionLiteral(..)
1852-
| Type::BoundMethod(_)
1853-
| Type::WrapperDescriptor(_)
1854-
| Type::KnownBoundMethod(_)
1855-
| Type::DataclassDecorator(_)
1856-
| Type::DataclassTransformer(_)
1857-
| Type::ModuleLiteral(..)
1858-
| Type::LiteralValue(_)
1859-
| Type::SpecialForm(_)
1860-
| Type::KnownInstance(_)
1861-
| Type::AlwaysFalsy
1862-
| Type::AlwaysTruthy
1863-
| Type::PropertyInstance(_)
1864-
// `T` is always a subtype of itself,
1865-
// and `T` is always a subtype of `T | None`
1866-
| Type::TypeVar(_)
1867-
// might inherit `Any`, but subtyping is still reflexive
1868-
| Type::ClassLiteral(_)
1869-
=> true,
1870-
Type::Dynamic(_)
1871-
| Type::NominalInstance(_)
1872-
| Type::ProtocolInstance(_)
1873-
| Type::GenericAlias(_)
1874-
| Type::SubclassOf(_)
1875-
| Type::Union(_)
1876-
| Type::Intersection(_)
1877-
| Type::Callable(_)
1878-
| Type::BoundSuper(_)
1879-
| Type::TypeIs(_)
1880-
| Type::TypeGuard(_)
1881-
| Type::TypedDict(_)
1882-
| Type::TypeAlias(_)
1883-
| Type::NewTypeInstance(_) => false,
1884-
}
1885-
}
1886-
18871840
pub(crate) fn try_upcast_to_callable(self, db: &'db dyn Db) -> Option<CallableTypes<'db>> {
18881841
match self {
18891842
Type::Callable(callable) => Some(CallableTypes::one(callable)),

crates/ty_python_semantic/src/types/relation.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,53 @@ impl TypeRelation {
226226

227227
#[salsa::tracked]
228228
impl<'db> Type<'db> {
229+
/// Return `true` if subtyping is always reflexive for this type; `T <: T` is always true for
230+
/// any `T` of this type.
231+
///
232+
/// This is true for fully static types, but also for some types that may not be fully static.
233+
/// For example, a `ClassLiteral` may inherit `Any`, but its subtyping is still reflexive.
234+
///
235+
/// This method may have false negatives, but it should not have false positives. It should be
236+
/// a cheap shallow check, not an exhaustive recursive check.
237+
const fn subtyping_is_always_reflexive(self) -> bool {
238+
match self {
239+
Type::Never
240+
| Type::FunctionLiteral(..)
241+
| Type::BoundMethod(_)
242+
| Type::WrapperDescriptor(_)
243+
| Type::KnownBoundMethod(_)
244+
| Type::DataclassDecorator(_)
245+
| Type::DataclassTransformer(_)
246+
| Type::ModuleLiteral(..)
247+
| Type::LiteralValue(_)
248+
| Type::SpecialForm(_)
249+
| Type::KnownInstance(_)
250+
| Type::AlwaysFalsy
251+
| Type::AlwaysTruthy
252+
| Type::PropertyInstance(_)
253+
// `T` is always a subtype of itself,
254+
// and `T` is always a subtype of `T | None`
255+
| Type::TypeVar(_)
256+
// might inherit `Any`, but subtyping is still reflexive
257+
| Type::ClassLiteral(_)
258+
=> true,
259+
Type::Dynamic(_)
260+
| Type::NominalInstance(_)
261+
| Type::ProtocolInstance(_)
262+
| Type::GenericAlias(_)
263+
| Type::SubclassOf(_)
264+
| Type::Union(_)
265+
| Type::Intersection(_)
266+
| Type::Callable(_)
267+
| Type::BoundSuper(_)
268+
| Type::TypeIs(_)
269+
| Type::TypeGuard(_)
270+
| Type::TypedDict(_)
271+
| Type::TypeAlias(_)
272+
| Type::NewTypeInstance(_) => false,
273+
}
274+
}
275+
229276
/// Return true if this type is a subtype of type `target`.
230277
///
231278
/// See [`TypeRelation::Subtyping`] for more details.

0 commit comments

Comments
 (0)