-
Notifications
You must be signed in to change notification settings - Fork 237
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
spec: clarify the mro linearization of Any #1672
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 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 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. SinceAny
is 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 anAny
base 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__
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
Any
type is meaningful here. Thenon_existent_attr
examples don't "model runtime" either -- at runtime with the actual runtimeAny
type in the MRO, those examples raiseAttributeError
.The point of having
Any
in the MRO is that for typing purposes it might represent any concrete type, which might have any actual attribute. IfAny
in this case represented a concrete type that provides thetriggers_getattr
attribute, then__getattr__
would not be invoked. So for the same reason thatnon_existent_attr
works and producesAny
in these examples,triggers_getattr
should work and also produceAny
, because checking the types in the MRO for thetriggers_getattr
attribute occurs before consulting__getattr__
, andAny
must 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
Any
on 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
A
withAny
. 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)