From c066bf0127482fc18c1337b50502cc63bdf805b5 Mon Sep 17 00:00:00 2001 From: David Peter Date: Thu, 15 May 2025 17:13:47 +0200 Subject: [PATCH] =?UTF-8?q?[ty]=20`type[=E2=80=A6]`=20is=20always=20assign?= =?UTF-8?q?able=20to=20`type`=20(#18121)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Model that `type[C]` is always assignable to `type`, even if `C` is not fully static. closes https://github.com/astral-sh/ty/issues/312 ## Test Plan * New Markdown tests * Property tests --- .../resources/mdtest/type_properties/is_assignable_to.md | 6 ++++++ crates/ty_python_semantic/src/types.rs | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/crates/ty_python_semantic/resources/mdtest/type_properties/is_assignable_to.md b/crates/ty_python_semantic/resources/mdtest/type_properties/is_assignable_to.md index d70151109d..6cfc89bd44 100644 --- a/crates/ty_python_semantic/resources/mdtest/type_properties/is_assignable_to.md +++ b/crates/ty_python_semantic/resources/mdtest/type_properties/is_assignable_to.md @@ -196,6 +196,12 @@ static_assert(is_assignable_to(type[Any], Meta)) static_assert(is_assignable_to(type[Unknown], Meta)) static_assert(is_assignable_to(Meta, type[Any])) static_assert(is_assignable_to(Meta, type[Unknown])) + +class AnyMeta(metaclass=Any): ... + +static_assert(is_assignable_to(type[AnyMeta], type)) +static_assert(is_assignable_to(type[AnyMeta], type[object])) +static_assert(is_assignable_to(type[AnyMeta], type[Any])) ``` ## Heterogeneous tuple types diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index 77da747c46..ee59b8bf63 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -1516,6 +1516,15 @@ impl<'db> Type<'db> { true } + // Every `type[...]` is assignable to `type` + (Type::SubclassOf(_), _) + if KnownClass::Type + .to_instance(db) + .is_assignable_to(db, target) => + { + true + } + // All `type[...]` types are assignable to `type[Any]`, because `type[Any]` can // materialize to any `type[...]` type. //