FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-103193: Speedup and inline `inspect._is_type` by AlexWaygood · Pull Request #103321 · python/cpython · GitHub

/ cpython Public
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 4 additions & 9 deletions Lib/inspect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Back | FazBrowse Home | New Git URL