-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Treat Callable dunder members as bound method descriptors
#20860
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
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 |
|---|---|---|
|
|
@@ -2014,7 +2014,29 @@ impl<'db> ClassLiteral<'db> { | |
| name: &str, | ||
| policy: MemberLookupPolicy, | ||
| ) -> PlaceAndQualifiers<'db> { | ||
| self.class_member_inner(db, None, name, policy) | ||
| fn into_function_like_callable<'d>(db: &'d dyn Db, ty: Type<'d>) -> Type<'d> { | ||
| match ty { | ||
| Type::Callable(callable_ty) => { | ||
| Type::Callable(CallableType::new(db, callable_ty.signatures(db), true)) | ||
| } | ||
| Type::Union(union) => { | ||
| union.map(db, |element| into_function_like_callable(db, *element)) | ||
| } | ||
| Type::Intersection(intersection) => intersection | ||
| .map_positive(db, |element| into_function_like_callable(db, *element)), | ||
|
Comment on lines
+2025
to
+2026
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. of course, if we just represented function-like callables as ...anyway, sorry for beating this drum, I'll try to shut up about beautiful refactors we could do until the beta's out 🙈
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If things wouldn't always turn out to be more complicated than I initially think, I would have already started that project. But I'm almost certain this would reveal at least three issues with our intersection handling, lead to unexpected performance regressions, and probably uncover a new salsa concurrency bug. So it has to wait a little longer 😄. |
||
| _ => ty, | ||
| } | ||
| } | ||
|
|
||
| let mut member = self.class_member_inner(db, None, name, policy); | ||
|
|
||
| // We generally treat dunder attributes with `Callable` types as function-like callables. | ||
| // See `callables_as_descriptors.md` for more details. | ||
| if name.starts_with("__") && name.ends_with("__") { | ||
| member = member.map_type(|ty| into_function_like_callable(db, ty)); | ||
| } | ||
|
|
||
| member | ||
| } | ||
|
|
||
| fn class_member_inner( | ||
|
|
||
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 previous example here would have failed at runtime.