raise TypeError("A single constraint is not allowed")
msg = "TypeVar(name, constraint, ...): constraints must be types."
self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
- try:
- def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
- except (AttributeError, ValueError):
- def_mod = None
+ def_mod = _caller()
if def_mod != 'typing':
self.__module__ = def_mod
def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
self.__name__ = name
super().__init__(bound, covariant, contravariant)
- try:
- def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
- except (AttributeError, ValueError):
- def_mod = None
+ def_mod = _caller()
if def_mod != 'typing':
self.__module__ = def_mod
The abc and functools modules indiscriminately call isinstance() and
issubclass() on the whole MRO of a user class, which may contain protocols.
"""
- try:
- return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
- except (AttributeError, ValueError): # For platforms without _getframe().
- return True
+ return _caller(depth) in {'abc', 'functools', None}
_PROTO_ALLOWLIST = {
elif kwargs:
raise TypeError("Either list of fields or keywords"
" can be provided to NamedTuple, not both")
- try:
- module = sys._getframe(1).f_globals.get('__name__', '__main__')
- except (AttributeError, ValueError):
- module = None
- return _make_nmtuple(typename, fields, module=module)
+ return _make_nmtuple(typename, fields, module=_caller())
_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
" but not both")
ns = {'__annotations__': dict(fields)}
- try:
+ module = _caller()
+ if module is not None:
# Setting correct module is necessary to make typed dict classes pickleable.
- ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
- except (AttributeError, ValueError):
- pass
+ ns['__module__'] = module
return _TypedDictMeta(typename, (), ns, total=total)