Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions crates/ty_python_semantic/resources/mdtest/conditional/match.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -455,7 +458,16 @@ def _(val):

match val:
# error: [invalid-match-pattern] "`<types.UnionType special-form 'int | str'>` 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`:
Expand Down
4 changes: 1 addition & 3 deletions crates/ty_python_semantic/src/types/infer/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Loading