Repository navigation
Filter overloaded __new__ by cls when converting a class to a callable
#5195
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
Open
i098
wants to merge
4
commits into
meta:main
Choose a base branch
from
i098:fix/pyrefly-5053
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
db49c58
Add a failing test for overloaded `__new__` converted to a callable
i098 18e5dff
Filter overloaded `__new__` by `cls` when converting a class to a cal…
i098 8b7edec
Share the receiver-based overload filter in a helper
i098 d112463
Filter overloaded `__init__` by `self` when converting a class to a c…
i098 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2569,13 +2569,47 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { | |
| self.bind_bound_method_type(&m.func, &m.obj, skip_instantiation, is_subset) | ||
| } | ||
|
|
||
| /// Bind `__new__` to `cls`, dropping overloads whose first parameter (`cls`) cannot accept | ||
| /// the class object. Type parameters are treated as `Any` on both sides, so overloads that | ||
| /// depend on their own type parameters, or on the free type parameters of a bare generic | ||
| /// class, are kept. If every overload would be dropped, the original overload is kept so | ||
| /// that the callable stays usable. | ||
| pub fn bind_dunder_new(&self, t: &Type, cls: ClassType) -> Option<Type> { | ||
| self.bind_function( | ||
| t, | ||
| &self.heap.mk_type_of(self.heap.mk_class_type(cls)), | ||
| false, | ||
| &mut |a, b| self.is_subset_eq(a, b), | ||
| ) | ||
| let cls_type = self.heap.mk_type_of(self.heap.mk_class_type(cls)); | ||
|
Contributor
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. nit: This works, but it's a little hard to read due to the length and the nesting. Here are some suggestions from an AI review for tightening it up:
Sketch of the helper (not compiled): /// Drop overloads whose first parameter cannot accept `receiver`. Type parameters on both
/// sides are replaced with `Any`, so an overload is dropped only if no specialization of it
/// could accept the receiver. If every overload would be dropped, `func` is returned unchanged.
fn filter_overloads_by_receiver(&self, func: Type, receiver: &Type) -> Type {
let Type::Overload(overload) = &func else {
return func;
};
let any = self.heap.mk_any_implicit();
let erase = |ty: &Type| {
let mut ty = ty.clone();
ty.subst_mut_fn(&mut |_| Some(any.clone()));
ty
};
let receiver = erase(receiver);
let signatures = overload.signatures.iter().filter(|sig| {
let (OverloadType::Function(f) | OverloadType::Forall(Forall { body: f, .. })) = sig;
f.signature
.get_first_param()
.is_none_or(|param| self.is_subset_eq(&receiver, &erase(param)))
});
match Vec1::try_from_vec(signatures.cloned().collect()) {
Ok(signatures) => self.heap.mk_overload(Overload { signatures, metadata: overload.metadata.clone() }),
Err(_) => func,
}
} |
||
| let any = self.heap.mk_any_implicit(); | ||
| let erase_tparams = |ty: &Type| { | ||
| let mut qs = SmallSet::new(); | ||
| ty.collect_quantifieds(&mut qs); | ||
| ty.clone().subst(&qs.iter().map(|q| (*q, &any)).collect()) | ||
| }; | ||
| let t = match t { | ||
| Type::Overload(overload) => { | ||
| let receiver = erase_tparams(&cls_type); | ||
| let applicable: Vec<_> = overload | ||
| .signatures | ||
| .iter() | ||
| .filter(|sig| { | ||
| let func = match sig { | ||
| OverloadType::Function(f) => f, | ||
| OverloadType::Forall(forall) => &forall.body, | ||
| }; | ||
| func.signature.get_first_param().is_none_or(|cls_param| { | ||
| self.is_subset_eq(&receiver, &erase_tparams(cls_param)) | ||
| }) | ||
| }) | ||
| .cloned() | ||
| .collect(); | ||
| match Vec1::try_from_vec(applicable) { | ||
| Ok(signatures) => self.heap.mk_overload(Overload { | ||
| signatures, | ||
| metadata: overload.metadata.clone(), | ||
| }), | ||
| Err(_) => t.clone(), | ||
| } | ||
| } | ||
| _ => t.clone(), | ||
| }; | ||
| self.bind_function(&t, &cls_type, false, &mut |a, b| self.is_subset_eq(a, b)) | ||
| } | ||
|
|
||
| /// Bind a `__init__` method for constructor callable conversion. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This fixes only half of the problem;
__init__has the same issue: