-
Notifications
You must be signed in to change notification settings - Fork 277
fix infer x: T if x.__class__: type[T] #1163 #2475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ use num_traits::ToPrimitive; | |||||||||||||
| use pyrefly_config::error_kind::ErrorKind; | ||||||||||||||
| use pyrefly_graph::index::Idx; | ||||||||||||||
| use pyrefly_python::ast::Ast; | ||||||||||||||
| use pyrefly_python::dunder; | ||||||||||||||
| use pyrefly_types::class::Class; | ||||||||||||||
| use pyrefly_types::display::TypeDisplayContext; | ||||||||||||||
| use pyrefly_types::facet::FacetChain; | ||||||||||||||
|
|
@@ -506,6 +507,17 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |||||||||||||
| range: TextRange, | ||||||||||||||
| errors: &ErrorCollector, | ||||||||||||||
| ) -> Option<Type> { | ||||||||||||||
| if let FacetKind::Attribute(attr) = facet | ||||||||||||||
| && *attr == dunder::CLASS | ||||||||||||||
| { | ||||||||||||||
| match op { | ||||||||||||||
| AtomicNarrowOp::Is(v) | AtomicNarrowOp::Eq(v) => { | ||||||||||||||
| let right = self.expr_infer(v, errors); | ||||||||||||||
| return Some(self.narrow_isinstance(base, &right)); | ||||||||||||||
| } | ||||||||||||||
|
||||||||||||||
| } | |
| } | |
| AtomicNarrowOp::IsNot(v) | AtomicNarrowOp::NotEq(v) => { | |
| let right = self.expr_infer(v, errors); | |
| return Some(self.narrow_is_not_instance(base, &right)); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation should validate that the right-hand side of the comparison is a valid class object, similar to how
isinstancevalidates its second argument viacheck_type_is_class_object. Without validation, invalid comparisons likex.__class__ is 42won't produce helpful error messages.Consider adding validation by calling
check_type_is_class_objectwith appropriate parameters, similar to how it's done incall_isinstanceat special_calls.rs:287. This would ensure users get clear error messages for invalid usage.