Skip to content

Commit 78d8780

Browse files
committed
Fix type(...) narrowing with final dataclasses
1 parent 01b2318 commit 78d8780

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎crates/zuban_python/src/type_/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ impl Type {
718718
pub fn is_final(&self, db: &Database) -> bool {
719719
match self {
720720
Type::Class(c) => c.class(db).use_cached_class_infos(db).is_final,
721+
Type::Dataclass(d) => d.class(db).use_cached_class_infos(db).is_final,
721722
_ => false,
722723
}
723724
}

‎crates/zuban_python/tests/mypylike/tests/narrowing.test‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ if int == type(x):
136136
else:
137137
reveal_type(x) # N: Revealed type is "Any"
138138

139+
[case narrowing_type_of_final_dataclass]
140+
from typing import final, assert_type
141+
from dataclasses import dataclass
142+
143+
@dataclass
144+
@final
145+
class D:
146+
x: int
147+
148+
def f(x: D | None):
149+
if type(x) is D:
150+
assert_type(x, D)
151+
else:
152+
assert_type(x, None)
153+
139154
[case avoid_narrowing_of_len_with_big_unpack]
140155
from typing import Tuple
141156
from typing_extensions import Unpack

0 commit comments

Comments
 (0)