comment here

This commit is contained in:
ton
2023-03-18 20:03:34 +07:00
commit 4553a0a589
14513 changed files with 2685043 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
"""
PRIVATE MODULE: do not import (from) it directly.
This module contains functionality for supporting the compatibility of jsons
with multiple Python versions.
"""
import sys
import typing
from enum import Enum
from jsons._cache import cached
class Flag(Enum):
"""
This is a light version of the Flag enum type that was introduced in
Python3.6. It supports the use of pipes for members (Flag.A | Flag.B).
"""
@classmethod
def _get_inst(cls, value):
try:
result = cls(value)
except ValueError:
pseudo_member = object.__new__(cls)
pseudo_member._value_ = value
contained = [elem.name for elem in cls if elem in pseudo_member]
pseudo_member._name_ = '|'.join(contained)
result = pseudo_member
return result
def __or__(self, other: 'Flag') -> 'Flag':
new_value = other.value | self.value
return self._get_inst(new_value)
def __contains__(self, item: 'Flag') -> bool:
return item.value == self.value & item.value
__ror__ = __or__
@cached
def tuple_with_ellipsis(tup: type) -> bool:
# Python3.5: Tuples have __tuple_use_ellipsis__
# Python3.7: Tuples have __args__
use_el = getattr(tup, '__tuple_use_ellipsis__', None)
if use_el is None:
use_el = tup.__args__[-1] is ...
return use_el
@cached
def get_union_params(un: type) -> list:
# Python3.5: Unions have __union_params__
# Python3.7: Unions have __args__
return getattr(un, '__union_params__', getattr(un, '__args__', None))
@cached
def get_naked_class(cls: type) -> type:
# Python3.5: typing classes have __extra__
# Python3.6: typing classes have __extra__
# Python3.7: typing classes have __origin__
# Return the non-generic class (e.g. dict) of a generic type (e.g. Dict).
return getattr(cls, '__extra__', getattr(cls, '__origin__', cls))
@cached
def get_type_hints(callable_: callable, fallback_ns=None):
# Python3.5: get_type_hints raises on classes without explicit constructor.
# Python3.10: get_type_hints on classes does not take the constructor.
try:
result = typing.get_type_hints(callable_)
except AttributeError:
result = {}
except NameError:
# attempt to resolve in global namespace - this works around an
# issue in 3.7 whereby __init__ created by dataclasses fails
# to find it's context. See https://bugs.python.org/issue34776
if fallback_ns is not None:
context_dict = sys.modules[fallback_ns].__dict__
result = typing.get_type_hints(callable_, globalns=context_dict)
if sys.version_info.minor >= 10 and type(callable_) is type:
annotations_from_init = typing.get_type_hints(callable_.__init__)
if 'return' in annotations_from_init:
# Python3.10: 'return' is a key that holds the returning type.
del annotations_from_init['return']
result = {**result, **annotations_from_init}
return result