Skip to content

Commit 1f339c0

Browse files
authored
Add regression test for dataclass typeguard (#19214)
Closes #19139
1 parent 5727d33 commit 1f339c0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

test-data/unit/check-dataclasses.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,3 +2609,12 @@ class B2(B1): # E: A NamedTuple cannot be a dataclass
26092609
pass
26102610

26112611
[builtins fixtures/tuple.pyi]
2612+
2613+
[case testDataclassesTypeGuard]
2614+
import dataclasses
2615+
2616+
raw_target: object
2617+
2618+
if isinstance(raw_target, type) and dataclasses.is_dataclass(raw_target):
2619+
reveal_type(raw_target) # N: Revealed type is "type[dataclasses.DataclassInstance]"
2620+
[builtins fixtures/tuple.pyi]

test-data/unit/check-typeguard.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,22 @@ def handle(model: Model) -> int:
777777
return process_model(model)
778778
return 0
779779
[builtins fixtures/tuple.pyi]
780+
781+
782+
[case testOverloadedTypeGuardType]
783+
from __future__ import annotations
784+
from typing_extensions import TypeIs, Never, overload
785+
786+
class X: ...
787+
788+
@overload # E: An overloaded function outside a stub file must have an implementation
789+
def is_xlike(obj: Never) -> TypeIs[X | type[X]]: ... # type: ignore
790+
@overload
791+
def is_xlike(obj: type) -> TypeIs[type[X]]: ...
792+
@overload
793+
def is_xlike(obj: object) -> TypeIs[X | type[X]]: ...
794+
795+
raw_target: object
796+
if isinstance(raw_target, type) and is_xlike(raw_target):
797+
reveal_type(raw_target) # N: Revealed type is "type[__main__.X]"
798+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)