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-30533:Add function inspect.getmembers_static that does not call properties or dynamic properties. #20911

Merged
merged 5 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def isabstract(object):
return True
return False

def getmembers(object, predicate=None):
def _getmembers(object, predicate, getter):
"""Return all members of an object as (name, value) pairs sorted by name.
Optionally, only return members that satisfy a given predicate."""
Copy link
Contributor

Choose a reason for hiding this comment

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

if this function is converted to an internal implementation detail with non-optional predicate and getter args, need to either update the docstring to match the change, or drop the docstring altogether

if isclass(object):
Expand All @@ -466,7 +466,7 @@ def getmembers(object, predicate=None):
# like calling their __get__ (see bug #1785), so fall back to
# looking in the __dict__.
try:
value = getattr(object, key)
value = getter(object, key)
# handle the duplicate key
if key in processed:
raise AttributeError
Expand All @@ -485,6 +485,17 @@ def getmembers(object, predicate=None):
results.sort(key=lambda pair: pair[0])
return results

def getmembers(object, predicate=None):
"""Return all members of an object as (name, value) pairs sorted by name.
Optionally, only return members that satisfy a given predicate."""
return _getmembers(object, predicate, getattr)

def getmembers_static(object, predicate=None):
"""Return all members of an object as (name, value) pairs sorted by name
without calling properties and other dynamic. Optionally, only return
members that satisfy a given predicate."""
hongweipeng marked this conversation as resolved.
Show resolved Hide resolved
return _getmembers(object, predicate, getattr_static)

Attribute = namedtuple('Attribute', 'name kind defining_class object')

def classify_class_attrs(cls):
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,23 @@ def eggs(self):
self.assertIn(('eggs', 'scrambled'), inspect.getmembers(A))
self.assertIn(('eggs', 'spam'), inspect.getmembers(A()))

def test_getmembers_static(self):
class A:
@property
def name(self):
raise NotImplementedError
@types.DynamicClassAttribute
def eggs(self):
raise NotImplementedError

a = A()
instance_members = inspect.getmembers_static(a)
class_members = inspect.getmembers_static(A)
self.assertIn(('name', inspect.getattr_static(a, 'name')), instance_members)
self.assertIn(('eggs', inspect.getattr_static(a, 'eggs')), instance_members)
self.assertIn(('name', inspect.getattr_static(A, 'name')), class_members)
self.assertIn(('eggs', inspect.getattr_static(A, 'eggs')), class_members)

def test_getmembers_with_buggy_dir(self):
class M(type):
def __dir__(cls):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`inspect.getmembers_static` , it return all members without
calling properties and other dynamic. Patch by Weipeng Hong.
hongweipeng marked this conversation as resolved.
Show resolved Hide resolved