Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion pyrefly/lib/alt/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use ruff_python_ast::Expr;
use ruff_python_ast::ExprNumberLiteral;
use ruff_python_ast::Int;
use ruff_python_ast::Number;
use ruff_python_ast::Operator;
use ruff_python_ast::name::Name;
use ruff_text_size::Ranged;
use ruff_text_size::TextRange;
Expand Down Expand Up @@ -219,6 +220,35 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.unions(res)
}

fn allow_negative_isinstance_classinfo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to specifically disallow type[T] rather than trying to enumerate everything that is allowed? This implementation makes me a bit nervous, since it seems easy to miss cases that we should allow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me check that

&self,
classinfo: &Expr,
errors: &ErrorCollector,
) -> bool {
match classinfo {
Expr::BinOp(binary_expression) if binary_expression.op == Operator::BitOr => {
self.allow_negative_isinstance_classinfo(&binary_expression.left, errors)
&& self.allow_negative_isinstance_classinfo(&binary_expression.right, errors)
}
Expr::Starred(starred_expression) => {
self.allow_negative_isinstance_classinfo(&starred_expression.value, errors)
}
Expr::Tuple(tuple_expression) => tuple_expression
.elts
.iter()
.all(|element| self.allow_negative_isinstance_classinfo(element, errors)),
Expr::NoneLiteral(_) => true,
// Default case
_ => matches!(
self.expr_infer(classinfo, errors),
Type::ClassDef(_)
| Type::TypeAlias(_)
| Type::Type(box Type::SpecialForm(_))
| Type::None
),
}
}

fn narrow_is_not_instance(&self, left: &Type, right: &Type) -> Type {
let mut res = Vec::new();
for right in self.as_class_info(right.clone()) {
Expand Down Expand Up @@ -671,7 +701,11 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
}
AtomicNarrowOp::IsNotInstance(v) => {
let right = self.expr_infer(v, errors);
self.narrow_is_not_instance(ty, &right)
if self.allow_negative_isinstance_classinfo(v, errors) {
self.narrow_is_not_instance(ty, &right)
} else {
ty.clone()
}
}
AtomicNarrowOp::TypeEq(v) => {
// If type(X) == Y then X can't be a subclass of Y
Expand Down
12 changes: 12 additions & 0 deletions pyrefly/lib/test/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,18 @@ def f[T](x, y: type[T]) -> T:
"#,
);

testcase!(
test_isinstance_type_classinfo_no_negative_narrow,
r#"
from typing import assert_type
def f(cls: type[int], x: int | str) -> None:
if isinstance(x, cls):
assert_type(x, int)
else:
assert_type(x, int | str)
"#,
);

testcase!(
test_isinstance_type_self,
r#"
Expand Down