-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for type narrowing of a class pattern when the specifie…
…d class is `type()` or a subtype thereof and the subject contains a `type[X]` whose metaclass potentially matches the pattern. This addresses #5573. (#5576) Co-authored-by: Eric Traut <[email protected]>
- Loading branch information
1 parent
12e9c31
commit 1da8c18
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/pyright-internal/src/tests/samples/matchClass4.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# This sample tests the case where a subject is narrowed against a | ||
# class pattern that includes a type() or subclass thereof and | ||
# the subject contains a type[T]. | ||
|
||
|
||
class MyMeta(type): | ||
pass | ||
|
||
|
||
class A: | ||
pass | ||
|
||
|
||
class B(A, metaclass=MyMeta): | ||
pass | ||
|
||
|
||
def func1(subj: type[A]): | ||
match subj: | ||
case type(): | ||
reveal_type(subj, expected_text="type[A]") | ||
case _: | ||
reveal_type(subj, expected_text="Never") | ||
|
||
|
||
def func2(subj: type[A]): | ||
match subj: | ||
case MyMeta(): | ||
reveal_type(subj, expected_text="type[A]") | ||
case _: | ||
reveal_type(subj, expected_text="type[A]") | ||
|
||
|
||
def func3(subj: type[B]): | ||
match subj: | ||
case MyMeta(): | ||
reveal_type(subj, expected_text="type[B]") | ||
case _: | ||
reveal_type(subj, expected_text="Never") | ||
|
||
|
||
def func4(subj: type[B] | type[int]): | ||
match subj: | ||
case MyMeta(): | ||
reveal_type(subj, expected_text="type[B] | type[int]") | ||
case _: | ||
reveal_type(subj, expected_text="type[int]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters