Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Inline internal helper for more speed
  • Loading branch information
Ivan Levkivskyi authored and ilevkivskyi committed Feb 4, 2017
commit 423fb755fed7d8519e00c94f2b17847d3aa65eb1
22 changes: 8 additions & 14 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,19 +928,6 @@ def _next_in_mro(cls):
return next_in_mro


def _valid_for_check(cls):
"""An internal helper to prohibit isinstance([1], List[str]) etc."""
if cls is Generic:
raise TypeError("Class %r cannot be used with class "
"or instance checks" % cls)
if (
cls.__origin__ is not None and
sys._getframe(2).f_globals['__name__'] not in ['abc', 'functools']
):
raise TypeError("Parameterized generics cannot be used with class "
"or instance checks")


def _make_subclasshook(cls):
"""Construct a __subclasshook__ callable that incorporates
the associated __extra__ class in subclass checks performed
Expand Down Expand Up @@ -1210,7 +1197,14 @@ def __getitem__(self, params):
orig_bases=self.__orig_bases__)

def __subclasscheck__(self, cls):
_valid_for_check(self)
if self.__origin__ is not None:
if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']:
raise TypeError("Parameterized generics cannot be used with class "
"or instance checks")
return False
if self is Generic:
raise TypeError("Class %r cannot be used with class "
"or instance checks" % self)
return super(GenericMeta, self).__subclasscheck__(cls)

def __instancecheck__(self, instance):
Expand Down
22 changes: 8 additions & 14 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,19 +849,6 @@ def _next_in_mro(cls):
return next_in_mro


def _valid_for_check(cls):
"""An internal helper to prohibit isinstance([1], List[str]) etc."""
if cls is Generic:
raise TypeError("Class %r cannot be used with class "
"or instance checks" % cls)
if (
cls.__origin__ is not None and
sys._getframe(2).f_globals['__name__'] not in ['abc', 'functools']
):
raise TypeError("Parameterized generics cannot be used with class "
"or instance checks")


def _make_subclasshook(cls):
"""Construct a __subclasshook__ callable that incorporates
the associated __extra__ class in subclass checks performed
Expand Down Expand Up @@ -1137,7 +1124,14 @@ def __getitem__(self, params):
orig_bases=self.__orig_bases__)

def __subclasscheck__(self, cls):
_valid_for_check(self)
if self.__origin__ is not None:
if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']:
raise TypeError("Parameterized generics cannot be used with class "
"or instance checks")
return False
if self is Generic:
raise TypeError("Class %r cannot be used with class "
"or instance checks" % self)
return super().__subclasscheck__(cls)

def __instancecheck__(self, instance):
Expand Down