diff --git a/crates/ty_python_semantic/resources/mdtest/conditional/match.md b/crates/ty_python_semantic/resources/mdtest/conditional/match.md index e819af61a3876e..47d93cb9352ef5 100644 --- a/crates/ty_python_semantic/resources/mdtest/conditional/match.md +++ b/crates/ty_python_semantic/resources/mdtest/conditional/match.md @@ -444,7 +444,10 @@ then uses `isinstance` to check the match. If the match pattern is not an instance of `type`, we raise a diagnostic: ```py -def _(val): +from typing import Any +from ty_extensions import Intersection + +def _(val, Valid1: type | Any, Valid2: Intersection[type, Any], Valid3: type[Any], Valid4: type[int]): Invalid1 = "foo" match val: @@ -455,7 +458,16 @@ def _(val): match val: # error: [invalid-match-pattern] "`` cannot be used in a class pattern because it is not a type" - case Invalid2(): ... + case Invalid2(): + pass + case Valid1(): # fine + pass + case Valid2(): # fine + pass + case Valid3(): # fine + pass + case Valid4(): # fine + pass ``` We also raise a diagnostic if the class cannot be used with `isinstance`: diff --git a/crates/ty_python_semantic/src/types/infer/builder.rs b/crates/ty_python_semantic/src/types/infer/builder.rs index fbfd739aec8f42..a69e3d9df826e1 100644 --- a/crates/ty_python_semantic/src/types/infer/builder.rs +++ b/crates/ty_python_semantic/src/types/infer/builder.rs @@ -5070,9 +5070,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { protocol_class, ); } - } else if !cls_ty.is_dynamic() - && !cls_ty.is_subtype_of(self.db(), KnownClass::Type.to_instance(self.db())) - { + } else if !cls_ty.is_assignable_to(self.db(), KnownClass::Type.to_instance(self.db())) { report_invalid_class_match_pattern(&self.context, &*pattern.cls, cls_ty); } }