Skip to content
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

bpo-45756: Fix mock triggers dynamic lookup via the descriptor protocol. #31348

Closed
wants to merge 2 commits into from

Conversation

hongweipeng
Copy link
Contributor

@hongweipeng hongweipeng commented Feb 15, 2022

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it the same as #29901 ?

@hongweipeng
Copy link
Contributor Author

Isn't it the same as #29901 ?

I think this is a good example of using inspect.getmembers_static.

@hongweipeng
Copy link
Contributor Author

Isn't it the same as #29901 ?

No, it only skip property in #29901 .

@hongweipeng
Copy link
Contributor Author

This PR is similar to #22209 .

I check the difference between inspect.getmembers_static(obj) and for k in dir(obj) ... inspect.getattr_static(obj, k) .

I found that they behave differently when __dir__ is defined and the attribute is not exist.

class A:
    def __dir__(self):
        return ['__class__', 'no_member']

obj = A()
inspect.getmembers_static(obj)      # ['__class__']
[(k, inspect.getattr_static(obj, k)) for k in dir(obj)] # AttributeError

I don't know which behavior is more reasonable.

for attr in dir(spec):
if iscoroutinefunction(getattr(spec, attr, None)):
_spec_asyncs.append(attr)
for attr, _ in inspect.getmembers_static(spec, iscoroutinefunction):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about async classmethods? Would those still get recognized as coroutines?

@carljm
Copy link
Member

carljm commented May 5, 2023

Thanks for the PR! I think we should consolidate the discussion to #22209. If we do this, we should prefer continuing to use dir and using getattr_static (as done in #22209), rather than switching to getmembers_static, because switching away from dir causes extra avoidable backward-incompatibility.

Also, I think the async-classmethod case is not fixable using getmembers_static, but is fixable via getattr_static.

Closing as duplicate of #22209.

@carljm carljm closed this May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants