bpo-38337: Change getattr to inspect.getattr_static#16521
Conversation
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). CLA MissingOur records indicate the following people have not signed the CLA: @jnsdrtlf For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day We also demand... A SHRUBBERY! You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
Sorry, something went wrong.
|
should we add a test case ? |
Sorry, something went wrong.
brandtbucher
left a comment
There was a problem hiding this comment.
Thanks @jnsdrtlf, and welcome to CPython! 😎
I have a minor change to suggest for the NEWS entry, but otherwise this looks like a good improvement.
I also agree that adding regression test is probably a good idea.
Sorry, something went wrong.
|
Actually, from looking at the test failures... I'm not sure if this is the desired behavior (and the docs are ambiguous). Maybe @1st1 can shed some light on the "correct" behavior of this function? |
Sorry, something went wrong.
|
Since |
Sorry, something went wrong.
|
Okay then. So this probably doesn't need a new test, but the ~4 failing ones should be updated! |
Sorry, something went wrong.
I think it needs a new test. Basically an object with a property and a test ensuring that the property was discovered by getmembers, but the getter code wasn't run. |
Sorry, something went wrong.
As suggested by @brandtbucher Co-Authored-By: Brandt Bucher <brandtbucher@gmail.com>
Sounds good! I will get back to work and write a test later today. |
Sorry, something went wrong.
Make sure inspect.getmembers won't call properties or other dynamic attributes
|
The test @types.DynamicClassAttribute
def eggs(self):
return 'spam'to be called. It asserts whether There is also some wierd behaviour when overwriting Failing tests:
|
Sorry, something went wrong.
This comment has been minimized.
This comment has been minimized.
|
Another inconsistent behaviour:
However, the following code does not return any properties: class Foo:
def __init__(self, data):
self._bar = data
@property
def bar(self):
print('--BAR--')
return self._bar
inspect.getmembers(Foo('test'), inspect.isdatadescriptor)Result Expected (new result) [('__class__', <attribute '__class__' of 'object' objects>), ('__dict__', <attribute '__dict__' of 'Foo' objects>), ('__weakref__', <attribute '__weakref__' of 'Foo' objects>), ('bar', <property object at 0x...>)]Is this correct so far? I am still confused about how |
Sorry, something went wrong.
|
Additionally, class Foo:
pass
inspect.getattr_static(Foo('test'), '__class__')returns class Foo:
pass
# Will return [] if getattr_static is used
inspect.getmembers(Foo('test'), inspect.isclass) |
Sorry, something went wrong.
|
In 3ac2093, I've changed
This feels more like a temporary fix - could be reverted to the first fix I've committed - although I am just not sure which is better. At this point, I am not sure what the actual expected behaviour should be. Someone should review this and the test case that is currently failing, as it expects a different behaviour (return the actual value of the property). |
Sorry, something went wrong.
1st1
left a comment
There was a problem hiding this comment.
Jonas, I thought about this PR for some time and I think we should reject it. Modifying getmembers in any way would break backwards compatibility in a very non-obvious way for many users. The ship to fix it has sailed.
We can add a new function getmembers_static that would use getattr_static instead of getattr. It must be a new distinct function, though.
Sorry, something went wrong.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Sorry, something went wrong.
|
@1st1 I share your thoughts on that. It does not seem to be obvious, what As for introducing a function
Is there a better place to discuss such questions? |
Sorry, something went wrong.
I think it would be better if you could post this to the python-ideas mailing list. I've very limited time and would hate to get this thing staled because of me. |
Sorry, something went wrong.
|
I added a link to the discussion on the bug tracker. |
Sorry, something went wrong.
|
This would be very helpful, running into the same issue with inspect.getmembers evaluating @Property decorated methods. getmembers_static would solve it. |
Sorry, something went wrong.
When calling
inspect.getmemberson a class that has a property (@property), the property will be called by thegetattrcall ingetmembers.Example:
Result:
Expected (new result):
https://bugs.python.org/issue38337