comparison env/lib/python3.9/site-packages/packaging/_typing.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """For neatly implementing static typing in packaging.
2
3 `mypy` - the static type analysis tool we use - uses the `typing` module, which
4 provides core functionality fundamental to mypy's functioning.
5
6 Generally, `typing` would be imported at runtime and used in that fashion -
7 it acts as a no-op at runtime and does not have any run-time overhead by
8 design.
9
10 As it turns out, `typing` is not vendorable - it uses separate sources for
11 Python 2/Python 3. Thus, this codebase can not expect it to be present.
12 To work around this, mypy allows the typing import to be behind a False-y
13 optional to prevent it from running at runtime and type-comments can be used
14 to remove the need for the types to be accessible directly during runtime.
15
16 This module provides the False-y guard in a nicely named fashion so that a
17 curious maintainer can reach here to read this.
18
19 In packaging, all static-typing related imports should be guarded as follows:
20
21 from packaging._typing import TYPE_CHECKING
22
23 if TYPE_CHECKING:
24 from typing import ...
25
26 Ref: https://github.com/python/mypy/issues/3216
27 """
28
29 __all__ = ["TYPE_CHECKING", "cast"]
30
31 # The TYPE_CHECKING constant defined by the typing module is False at runtime
32 # but True while type checking.
33 if False: # pragma: no cover
34 from typing import TYPE_CHECKING
35 else:
36 TYPE_CHECKING = False
37
38 # typing's cast syntax requires calling typing.cast at runtime, but we don't
39 # want to import typing at runtime. Here, we inform the type checkers that
40 # we're importing `typing.cast` as `cast` and re-implement typing.cast's
41 # runtime behavior in a block that is ignored by type checkers.
42 if TYPE_CHECKING: # pragma: no cover
43 # not executed at runtime
44 from typing import cast
45 else:
46 # executed at runtime
47 def cast(type_, value): # noqa
48 return value