bpo-45756: Fix mock triggers dynamic lookup via the descriptor protocol. by hongweipeng · Pull Request #31348 · python/cpython (original) (raw)

hongweipeng

@hongweipeng

sobolevn

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

Isn't it the same as #29901 ?

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

@hongweipeng

@hongweipeng

Isn't it the same as #29901 ?

No, it only skip property in #29901 .

@hongweipeng

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.

JelleZijlstra

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

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?

@f0k f0k mentioned this pull request

Feb 22, 2023

@carljm

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.