-
Notifications
You must be signed in to change notification settings - Fork 283
spec: clarify the mro linearization of Any #1672
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e16dee5
spec: clarify the mro linearization of Any
mikeshardmind f38876b
add basic conformance tests
mikeshardmind 8a1633f
missing attr check
mikeshardmind 911c024
Add more test cases around Any
mikeshardmind 0b94612
Address Feedback from carljm
mikeshardmind 09ea47b
Update conformance/tests/specialtypes_any.py
mikeshardmind 9db07eb
Fixing issues with iter examples, + swap a placement
mikeshardmind 7f011ca
Add notes on some of the examples, fix an incongruence
mikeshardmind 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
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
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.
I don't think this is correct. The
__getattr__method is called only if the attribute isn't present in the object's dictionary. SinceAnyis in the MRO (even as the last item), then all attributes are effectively "found in the object's dictionary". So this will evaluate toAny.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 is correct, one of the bases provided provides a
__getattr__definitionThere 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.
But
__getattr__isn't applicable here. This method is invoked only if the attribute is not present. But in this case, it is "present" because all attributes are present on anAnybase class.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.
At runtime, this is definitely invoked, runtime doesn't see Any and do something special for
__getattr__, I've modeled this on accurately typing runtime for a known implementation of__getattr__Uh oh!
There was an error while loading. Please reload this page.
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.
I don't think "modeling this on runtime" with the actual runtime
Anytype is meaningful here. Thenon_existent_attrexamples don't "model runtime" either -- at runtime with the actual runtimeAnytype in the MRO, those examples raiseAttributeError.The point of having
Anyin the MRO is that for typing purposes it might represent any concrete type, which might have any actual attribute. IfAnyin this case represented a concrete type that provides thetriggers_getattrattribute, then__getattr__would not be invoked. So for the same reason thatnon_existent_attrworks and producesAnyin these examples,triggers_getattrshould work and also produceAny, because checking the types in the MRO for thetriggers_getattrattribute occurs before consulting__getattr__, andAnymust be considered to provide all attributes.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.
What about going the other way on this? You could specify that a class that inherits from Any must only have a
__getattr__definition if the definition is typed to return AnyThere 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.
Can someone outline or link to the motivating case with mocks that would be broken if the spec just gave
Anyon these getattr cases?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.
It could be resolved that way, taking this over to discourse.
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 has nothing to do with whether the unknown class implements
__getattr__. It has to do with whether the unknown class has an attribute calledtriggers_getattr. We must assume it does, in which case the presence of a__getattr__method is irrelevant.Consider the following:
Note that
B.__getattr__is never invoked at runtime in this case, and the type checker accordingly never simulates a call to__getattr__when evaluating the type of expressionc.foo.Now, we replace
AwithAny. Here again, the type checker should assume that__getattr__will not be invoked.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.
(github isn't loading reviews in realtime right now for anyone viewing this after the fact)