Skip to content

Commit 8a5642e

Browse files
committed
add tests that fail on main
1 parent a7f7a5e commit 8a5642e

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

crates/ty_python_semantic/resources/mdtest/narrow/match.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,22 @@ def _(x: tuple[Literal["tag1"], A] | tuple[str, B]):
442442
# But we *can* narrow with inequality
443443
reveal_type(x) # revealed: tuple[str, B]
444444
```
445+
446+
and it is also restricted to `match` patterns that solely consist of value patterns:
447+
448+
```py
449+
class Config:
450+
MODE: str = "default"
451+
452+
def _(u: tuple[Literal["foo"], int] | tuple[Literal["bar"], str]):
453+
match u[0]:
454+
case Config.MODE | "foo":
455+
# Config.mode has type `str` (not a literal), which could match
456+
# any string value at runtime. We cannot narrow based on "foo" alone
457+
# because the actual match might have been against Config.mode.
458+
reveal_type(u) # revealed: tuple[Literal["foo"], int] | tuple[Literal["bar"], str]
459+
case "bar":
460+
# Since the previous case could match any string, this case can
461+
# still narrow to `tuple[Literal["bar"], str]` when `u[0]` equals "bar".
462+
reveal_type(u) # revealed: tuple[Literal["bar"], str]
463+
```

crates/ty_python_semantic/resources/mdtest/typed_dict.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,33 @@ def match_non_literal(u: Foo | NonLiteralTD):
23242324
reveal_type(u) # revealed: NonLiteralTD
23252325
```
23262326

2327+
and it is also restricted to `match` patterns that solely consist of value patterns:
2328+
2329+
```py
2330+
class Config:
2331+
MODE: str = "default"
2332+
2333+
class Foo(TypedDict):
2334+
tag: Literal["foo"]
2335+
data: int
2336+
2337+
class Bar(TypedDict):
2338+
tag: Literal["bar"]
2339+
data: str
2340+
2341+
def test_or_pattern_with_non_literal(u: Foo | Bar):
2342+
match u["tag"]:
2343+
case Config.MODE | "foo":
2344+
# Config.mode has type `str` (not a literal), which could match
2345+
# any string value at runtime. We cannot narrow based on "foo" alone
2346+
# because the actual match might have been against Config.mode.
2347+
reveal_type(u) # revealed: Foo | Bar
2348+
case "bar":
2349+
# Since the previous case could match any string, this case can
2350+
# still narrow to `Bar` when tag equals "bar".
2351+
reveal_type(u) # revealed: Bar
2352+
```
2353+
23272354
We can still narrow `Literal` tags even when non-`TypedDict` types are present in the union:
23282355

23292356
```py

0 commit comments

Comments
 (0)