Skip to content

Commit

Permalink
Fix error handling in issubclass
Browse files Browse the repository at this point in the history
Summary: Due to the use of `untype`, we were reporting two errors when issubclass got a first argument that isn't a type. We only need one.

Reviewed By: stroxler

Differential Revision: D68291059

fbshipit-source-id: 9d756ebaf22bc65bcf5cbf7c69d833d398ae4e56
  • Loading branch information
rchen152 authored and facebook-github-bot committed Jan 16, 2025
1 parent 005e900 commit 32d701c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pyre2/pyre2/bin/alt/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.distribute_narrow_op_over_tuple(&NarrowOp::IsSubclass, &right, v.range())
{
self.narrow(ty, &distributed_op)
} else if let Some(right) = self.unwrap_class_object_or_error(&right, v.range()) {
Type::type_form(self.intersect(&self.untype(ty.clone(), v.range()), &right))
} else if let Some(left) = self.untype_opt(ty.clone(), v.range())
&& let Some(right) = self.unwrap_class_object_or_error(&right, v.range())
{
Type::type_form(self.intersect(&left, &right))
} else {
ty.clone()
}
Expand All @@ -192,8 +194,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.distribute_narrow_op_over_tuple(&NarrowOp::IsSubclass, &right, v.range())
{
self.narrow(ty, &distributed_op.negate())
} else if let Some(right) = self.unwrap_class_object_or_error(&right, v.range()) {
Type::type_form(self.subtract(&self.untype(ty.clone(), v.range()), &right))
} else if let Some(left) = self.untype_opt(ty.clone(), v.range())
&& let Some(right) = self.unwrap_class_object_or_error(&right, v.range())
{
Type::type_form(self.subtract(&left, &right))
} else {
ty.clone()
}
Expand Down
9 changes: 9 additions & 0 deletions pyre2/pyre2/bin/test/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,15 @@ def f(x: type[B] | type[int]):
"#,
);

testcase!(
test_issubclass_error,
r#"
def f(x: int):
if issubclass(x, int): # E: EXPECTED int <: type
return True
"#,
);

testcase!(
test_typeguard_instance_method,
r#"
Expand Down

0 comments on commit 32d701c

Please sign in to comment.