Skip to content

Commit fefae68

Browse files
author
Ivan Levkivskyi
committed
Inline internal helper for more speed
1 parent d7ef1c0 commit fefae68

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

python2/typing.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -928,19 +928,6 @@ def _next_in_mro(cls):
928928
return next_in_mro
929929

930930

931-
def _valid_for_check(cls):
932-
"""An internal helper to prohibit isinstance([1], List[str]) etc."""
933-
if cls is Generic:
934-
raise TypeError("Class %r cannot be used with class "
935-
"or instance checks" % cls)
936-
if (
937-
cls.__origin__ is not None and
938-
sys._getframe(2).f_globals['__name__'] not in ['abc', 'functools']
939-
):
940-
raise TypeError("Parameterized generics cannot be used with class "
941-
"or instance checks")
942-
943-
944931
def _make_subclasshook(cls):
945932
"""Construct a __subclasshook__ callable that incorporates
946933
the associated __extra__ class in subclass checks performed
@@ -1208,7 +1195,14 @@ def __getitem__(self, params):
12081195
orig_bases=self.__orig_bases__)
12091196

12101197
def __subclasscheck__(self, cls):
1211-
_valid_for_check(self)
1198+
if self.__origin__ is not None:
1199+
if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']:
1200+
raise TypeError("Parameterized generics cannot be used with class "
1201+
"or instance checks")
1202+
return False
1203+
if self is Generic:
1204+
raise TypeError("Class %r cannot be used with class "
1205+
"or instance checks" % self)
12121206
return super(GenericMeta, self).__subclasscheck__(cls)
12131207

12141208
def __instancecheck__(self, instance):

src/typing.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -849,19 +849,6 @@ def _next_in_mro(cls):
849849
return next_in_mro
850850

851851

852-
def _valid_for_check(cls):
853-
"""An internal helper to prohibit isinstance([1], List[str]) etc."""
854-
if cls is Generic:
855-
raise TypeError("Class %r cannot be used with class "
856-
"or instance checks" % cls)
857-
if (
858-
cls.__origin__ is not None and
859-
sys._getframe(2).f_globals['__name__'] not in ['abc', 'functools']
860-
):
861-
raise TypeError("Parameterized generics cannot be used with class "
862-
"or instance checks")
863-
864-
865852
def _make_subclasshook(cls):
866853
"""Construct a __subclasshook__ callable that incorporates
867854
the associated __extra__ class in subclass checks performed
@@ -1135,7 +1122,14 @@ def __getitem__(self, params):
11351122
orig_bases=self.__orig_bases__)
11361123

11371124
def __subclasscheck__(self, cls):
1138-
_valid_for_check(self)
1125+
if self.__origin__ is not None:
1126+
if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']:
1127+
raise TypeError("Parameterized generics cannot be used with class "
1128+
"or instance checks")
1129+
return False
1130+
if self is Generic:
1131+
raise TypeError("Class %r cannot be used with class "
1132+
"or instance checks" % self)
11391133
return super().__subclasscheck__(cls)
11401134

11411135
def __instancecheck__(self, instance):

0 commit comments

Comments
 (0)