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

gh-103193: Speedup and inline inspect._is_type #103321

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Changes from all 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
13 changes: 4 additions & 9 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,13 +1791,6 @@ def _check_class(klass, attr):
return entry.__dict__[attr]
return _sentinel

def _is_type(obj):
try:
_static_getmro(obj)
except TypeError:
return False
return True

def _shadowed_dict(klass):
for entry in _static_getmro(klass):
dunder_dict = _get_dunder_dict_of_class(entry)
Expand All @@ -1821,8 +1814,10 @@ def getattr_static(obj, attr, default=_sentinel):
documentation for details.
"""
instance_result = _sentinel
if not _is_type(obj):
klass = type(obj)

objtype = type(obj)
if type not in _static_getmro(objtype):
klass = objtype
Comment on lines +1818 to +1820
Copy link
Member

Choose a reason for hiding this comment

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

This has equivalent semantics with less code and fewer assignments, but perhaps it muddies the semantics of the klass var a bit (since it will temporarily be "wrong" in the case that obj is already a type.) No strong feelings, whatever you prefer.

Suggested change
objtype = type(obj)
if type not in _static_getmro(objtype):
klass = objtype
klass = type(obj)
if type not in _static_getmro(klass):

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm. Yeah, I see what you mean, but I think I'd prefer to keep the semantics of klass clear here :)

dict_attr = _shadowed_dict(klass)
if (dict_attr is _sentinel or
type(dict_attr) is types.MemberDescriptorType):
Expand Down