Skip to content

Instantly share code, notes, and snippets.

@pitrou
Created December 7, 2017 18:00
Show Gist options
  • Save pitrou/a14a012871e852fdfaddbd8141d59611 to your computer and use it in GitHub Desktop.
Save pitrou/a14a012871e852fdfaddbd8141d59611 to your computer and use it in GitHub Desktop.
def _undo_type_conversion(obj):
"""
Undo the conversion in typing._type_check().
"""
if obj is type(None):
return None
else:
return obj
def _reduce_typing_generic(self):
import abc
ns = dict(self.__dict__)
tvars = ns.pop('__parameters__')
args = ns.pop('__args__')
origin = ns.pop('__origin__')
extra = ns.pop('__extra__')
orig_bases = ns.pop('__orig_bases__', None)
assert origin is not None
# Remove all generated stuff in namespace
# Hopefully we are not missing anything here
for k in list(ns):
if k.startswith('__') and k.endswith('__'):
del ns[k]
elif k.startswith('_abc_'):
del ns[k]
elif k == '_gorg':
del ns[k]
# Fixup args
args = tuple(map(_undo_type_conversion, args))
# XXX sometimes extra is stuffed into bases, but not always, and I
# don't know how to detect it
# (try e.g. typing.Generator[int, None, None])
bases = self.__bases__
#if type(extra) is abc.ABCMeta:
#bases = tuple(x for x in bases if x is not extra)
return (self.__class__, (self.__name__, bases, ns,
tvars, args, origin, extra, orig_bases))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment