[Python-checkins] gh-103193: Speedup and inline `inspect._is_type` (#103321)

AlexWaygood webhook-mailer at python.org
Thu Apr 6 16:49:31 EDT 2023


https://github.com/python/cpython/commit/dca7d174f1dc3f9e67c7451a27bc92dc5a733008
commit: dca7d174f1dc3f9e67c7451a27bc92dc5a733008
branch: main
author: Alex Waygood <Alex.Waygood at Gmail.com>
committer: AlexWaygood <Alex.Waygood at Gmail.com>
date: 2023-04-06T21:49:24+01:00
summary:

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

Improve performance of `inspect.getattr_static`

files:
M Lib/inspect.py

diff --git a/Lib/inspect.py b/Lib/inspect.py
index a317f0ca7488..4242b40c2a08 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -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)
@@ -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
         dict_attr = _shadowed_dict(klass)
         if (dict_attr is _sentinel or
             type(dict_attr) is types.MemberDescriptorType):



More information about the Python-checkins mailing list