1
|
|
|
import typing |
2
|
|
|
|
3
|
|
|
from typish.classes._subscriptable_type import SubscriptableType |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def is_literal_type(cls: typing.Any) -> bool: |
7
|
|
|
""" |
8
|
|
|
Return whether cls is a Literal type. |
9
|
|
|
:param cls: the type that is to be checked. |
10
|
|
|
:return: True if cls is a Literal type. |
11
|
|
|
""" |
12
|
|
|
from typish.functions._get_simple_name import get_simple_name |
13
|
|
|
|
14
|
|
|
return get_simple_name(cls) == 'Literal' |
15
|
|
|
|
16
|
|
|
|
17
|
|
View Code Duplication |
class _LiteralMeta(SubscriptableType): |
|
|
|
|
18
|
|
|
""" |
19
|
|
|
A Metaclass that exists to serve Literal and alter the __args__ attribute. |
20
|
|
|
""" |
21
|
|
|
def __getattribute__(cls, item): |
22
|
|
|
""" |
23
|
|
|
This method makes sure that __args__ is a tuple, like with |
24
|
|
|
typing.Literal. |
25
|
|
|
:param item: the name of the attribute that is obtained. |
26
|
|
|
:return: the attribute. |
27
|
|
|
""" |
28
|
|
|
if item == '__args__': |
29
|
|
|
try: |
30
|
|
|
result = SubscriptableType.__getattribute__(cls, item), |
31
|
|
|
except AttributeError: |
32
|
|
|
# In case of Python 3.5 |
33
|
|
|
result = tuple() |
34
|
|
|
elif item in ('__origin__', '__name__', '_name'): |
35
|
|
|
result = 'Literal' |
36
|
|
|
else: |
37
|
|
|
result = SubscriptableType.__getattribute__(cls, item) |
38
|
|
|
return result |
39
|
|
|
|
40
|
|
|
def __instancecheck__(self, instance): |
41
|
|
|
return self.__args__ and self.__args__[0] == instance |
42
|
|
|
|
43
|
|
|
def __str__(self): |
44
|
|
|
return '{}[{}]'.format(self.__name__, self.__args__[0]) |
45
|
|
|
|
46
|
|
|
def __subclasscheck__(self, subclass: typing.Any) -> bool: |
47
|
|
|
return is_literal_type(subclass) |
48
|
|
|
|
49
|
|
|
|
50
|
|
View Code Duplication |
class LiteralAlias(type, metaclass=_LiteralMeta): |
|
|
|
|
51
|
|
|
""" |
52
|
|
|
This is a backwards compatible variant of typing.Literal (Python 3.8+). |
53
|
|
|
""" |
54
|
|
|
@staticmethod |
55
|
|
|
def from_literal(literal: typing.Any) -> typing.Type['LiteralAlias']: |
56
|
|
|
""" |
57
|
|
|
Create a LiteralAlias from the given typing.Literal. |
58
|
|
|
:param literal: the typing.Literal type. |
59
|
|
|
:return: a LiteralAlias type. |
60
|
|
|
""" |
61
|
|
|
from typish.functions._get_args import get_args |
62
|
|
|
|
63
|
|
|
args = get_args(literal) |
64
|
|
|
return LiteralAlias[args[0]] if args else LiteralAlias |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
# If Literal is available (Python 3.8+), then return that type instead. |
68
|
|
|
Literal = getattr(typing, 'Literal', LiteralAlias) |
69
|
|
|
|