-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[red-knot] Descriptor protocol #15966
Comments
An important case to consider here that isn't shown in the example in summary is where the |
Yes, thanks. This is already covered in the provisional tests (#15972). |
## Summary This is a first step towards creating a test suite for [descriptors](https://docs.python.org/3/howto/descriptor.html). It does not (yet) aim to be exhaustive. relevant ticket: #15966 ## Test Plan Compared desired behavior with the runtime behavior and the behavior of existing type checkers. --------- Co-authored-by: Mike Perlov <[email protected]>
@carljm @sharkdp sorry, for delay Here is my thinking, proposed plan & rough design. Guiding principles I've used:
Design
Obviously there are going to be a lot of minute details in actual implementation, but overall it feels like descriptors do not introduce any special needs really. PlanThe plan, is to start with known classes and avoid bothering with full inference of descriptor dunder method signatures. Implement the non-data descriptors first, using the most popular builtins. Then expand to set only data descriptor and build-up support for known descriptor classes. Then tackle everything else. Assuming red_knot is targeting "an average" python user, I'd suggest implementing in the following order based on frequency of use:
PS. I can theoretically augment this with samples for each step, but this can also be done in individual issues. |
Thank you!
Both sound reasonable 👍
👍
Maybe? Probably? But I wouldn't focus too much on use cases for now and rather get the basics straight. I think we could even defer proper decorator support until after we implemented the descriptor protocol. We might this for issues like #15961, but I'm not sure why it would be required here. Yes, those are examples of descriptors, but I don't see why we need to recognize them as such by introducing
Our
It would be great if we could defer decorators and slotted classes for now. |
@sharkdp sounds like you'd prefer going the other way around - first support custom descriptors by checking special methods, then handle common cases, like Overall this means changing:
and methods that it calls ( The main "obstacle" I see right now is that Funny enough, but implementing However, maybe a reasonable scope for one PR could be:
here: ruff/crates/red_knot_python_semantic/src/types.rs Lines 4300 to 4311 in 6e34f74
Does this better match your thinking? |
We need to understand the descriptor protocol in order to infer proper types for attribute accesses like in this simple example:
References: Python Documentation: Descriptor Guide
part of: #14164
The text was updated successfully, but these errors were encountered: