Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/mypy_extensions.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author | shellac |
---|---|
date | Sat, 02 May 2020 07:14:21 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:26e78fe6e8c4 |
---|---|
1 """Defines experimental extensions to the standard "typing" module that are | |
2 supported by the mypy typechecker. | |
3 | |
4 Example usage: | |
5 from mypy_extensions import TypedDict | |
6 """ | |
7 | |
8 from typing import Any | |
9 | |
10 # NOTE: This module must support Python 2.7 in addition to Python 3.x | |
11 | |
12 import sys | |
13 # _type_check is NOT a part of public typing API, it is used here only to mimic | |
14 # the (convenient) behavior of types provided by typing module. | |
15 from typing import _type_check # type: ignore | |
16 | |
17 | |
18 def _check_fails(cls, other): | |
19 try: | |
20 if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools', 'typing']: | |
21 # Typed dicts are only for static structural subtyping. | |
22 raise TypeError('TypedDict does not support instance and class checks') | |
23 except (AttributeError, ValueError): | |
24 pass | |
25 return False | |
26 | |
27 | |
28 def _dict_new(cls, *args, **kwargs): | |
29 return dict(*args, **kwargs) | |
30 | |
31 | |
32 def _typeddict_new(cls, _typename, _fields=None, **kwargs): | |
33 total = kwargs.pop('total', True) | |
34 if _fields is None: | |
35 _fields = kwargs | |
36 elif kwargs: | |
37 raise TypeError("TypedDict takes either a dict or keyword arguments," | |
38 " but not both") | |
39 | |
40 ns = {'__annotations__': dict(_fields), '__total__': total} | |
41 try: | |
42 # Setting correct module is necessary to make typed dict classes pickleable. | |
43 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__') | |
44 except (AttributeError, ValueError): | |
45 pass | |
46 | |
47 return _TypedDictMeta(_typename, (), ns) | |
48 | |
49 | |
50 class _TypedDictMeta(type): | |
51 def __new__(cls, name, bases, ns, total=True): | |
52 # Create new typed dict class object. | |
53 # This method is called directly when TypedDict is subclassed, | |
54 # or via _typeddict_new when TypedDict is instantiated. This way | |
55 # TypedDict supports all three syntaxes described in its docstring. | |
56 # Subclasses and instances of TypedDict return actual dictionaries | |
57 # via _dict_new. | |
58 ns['__new__'] = _typeddict_new if name == 'TypedDict' else _dict_new | |
59 tp_dict = super(_TypedDictMeta, cls).__new__(cls, name, (dict,), ns) | |
60 | |
61 anns = ns.get('__annotations__', {}) | |
62 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type" | |
63 anns = {n: _type_check(tp, msg) for n, tp in anns.items()} | |
64 for base in bases: | |
65 anns.update(base.__dict__.get('__annotations__', {})) | |
66 tp_dict.__annotations__ = anns | |
67 if not hasattr(tp_dict, '__total__'): | |
68 tp_dict.__total__ = total | |
69 return tp_dict | |
70 | |
71 __instancecheck__ = __subclasscheck__ = _check_fails | |
72 | |
73 | |
74 TypedDict = _TypedDictMeta('TypedDict', (dict,), {}) | |
75 TypedDict.__module__ = __name__ | |
76 TypedDict.__doc__ = \ | |
77 """A simple typed name space. At runtime it is equivalent to a plain dict. | |
78 | |
79 TypedDict creates a dictionary type that expects all of its | |
80 instances to have a certain set of keys, with each key | |
81 associated with a value of a consistent type. This expectation | |
82 is not checked at runtime but is only enforced by typecheckers. | |
83 Usage:: | |
84 | |
85 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) | |
86 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK | |
87 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check | |
88 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') | |
89 | |
90 The type info could be accessed via Point2D.__annotations__. TypedDict | |
91 supports two additional equivalent forms:: | |
92 | |
93 Point2D = TypedDict('Point2D', x=int, y=int, label=str) | |
94 | |
95 class Point2D(TypedDict): | |
96 x: int | |
97 y: int | |
98 label: str | |
99 | |
100 The latter syntax is only supported in Python 3.6+, while two other | |
101 syntax forms work for Python 2.7 and 3.2+ | |
102 """ | |
103 | |
104 # Argument constructors for making more-detailed Callables. These all just | |
105 # return their type argument, to make them complete noops in terms of the | |
106 # `typing` module. | |
107 | |
108 | |
109 def Arg(type=Any, name=None): | |
110 """A normal positional argument""" | |
111 return type | |
112 | |
113 | |
114 def DefaultArg(type=Any, name=None): | |
115 """A positional argument with a default value""" | |
116 return type | |
117 | |
118 | |
119 def NamedArg(type=Any, name=None): | |
120 """A keyword-only argument""" | |
121 return type | |
122 | |
123 | |
124 def DefaultNamedArg(type=Any, name=None): | |
125 """A keyword-only argument with a default value""" | |
126 return type | |
127 | |
128 | |
129 def VarArg(type=Any): | |
130 """A *args-style variadic positional argument""" | |
131 return type | |
132 | |
133 | |
134 def KwArg(type=Any): | |
135 """A **kwargs-style variadic keyword argument""" | |
136 return type | |
137 | |
138 | |
139 # Return type that indicates a function does not return | |
140 class NoReturn: pass | |
141 | |
142 | |
143 def trait(cls): | |
144 return cls | |
145 | |
146 | |
147 def mypyc_attr(*attrs, **kwattrs): | |
148 return lambda x: x | |
149 | |
150 | |
151 # TODO: We may want to try to properly apply this to any type | |
152 # variables left over... | |
153 class _FlexibleAliasClsApplied: | |
154 def __init__(self, val): | |
155 self.val = val | |
156 | |
157 def __getitem__(self, args): | |
158 return self.val | |
159 | |
160 | |
161 class _FlexibleAliasCls: | |
162 def __getitem__(self, args): | |
163 return _FlexibleAliasClsApplied(args[-1]) | |
164 | |
165 | |
166 FlexibleAlias = _FlexibleAliasCls() |