Skip to content

Commit 36a5ee2

Browse files
yangdanny97meta-codesync[bot]
authored andcommitted
do not narrow classdef in issubset check
Summary: untype_opt and unwrap_class_object_silently both promote ClassDef to ClassType. This means that the type of `list` gets narrowed to `list[Any]` after the intersection. This diff removes that behavior if both sides are ClassDefs closes #1164 Reviewed By: stroxler Differential Revision: D84313272 fbshipit-source-id: 866ea3930dae7046a31a9b843dea12ff95310e1a
1 parent 7a7f4c1 commit 36a5ee2

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pyrefly/lib/alt/narrow.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
179179
fn narrow_issubclass(&self, left: &Type, right: &Type, range: TextRange) -> Type {
180180
let mut res = Vec::new();
181181
for right in self.as_class_info(right.clone()) {
182-
if let Some(left) = self.untype_opt(left.clone(), range)
182+
if matches!(left, Type::ClassDef(_)) && matches!(right, Type::ClassDef(_)) {
183+
res.push(self.intersect(left, &right))
184+
} else if let Some(left) = self.untype_opt(left.clone(), range)
183185
&& let Some(right) = self.unwrap_class_object_silently(&right)
184186
{
185187
res.push(Type::type_form(self.intersect(&left, &right)))
@@ -193,7 +195,9 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
193195
fn narrow_is_not_subclass(&self, left: &Type, right: &Type, range: TextRange) -> Type {
194196
let mut res = Vec::new();
195197
for right in self.as_class_info(right.clone()) {
196-
if let Some(left) = self.untype_opt(left.clone(), range)
198+
if matches!(left, Type::ClassDef(_)) && matches!(right, Type::ClassDef(_)) {
199+
res.push(self.subtract(left, &right))
200+
} else if let Some(left) = self.untype_opt(left.clone(), range)
197201
&& let Some(right) = self.unwrap_class_object_silently(&right)
198202
{
199203
res.push(Type::type_form(self.subtract(&left, &right)))

pyrefly/lib/test/narrow.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,3 +1796,11 @@ def test(a: int, b: str, c: bytes):
17961796
assert_type(c, Literal[b""])
17971797
"#,
17981798
);
1799+
1800+
testcase!(
1801+
test_do_not_narrow_class_name,
1802+
r#"
1803+
assert issubclass(list, object)
1804+
x: list[int] = [1]
1805+
"#,
1806+
);

0 commit comments

Comments
 (0)