Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pkg_resources/_vendor/packaging/_typing.py: 0%
4 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:52 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:52 +0000
1"""For neatly implementing static typing in packaging.
3`mypy` - the static type analysis tool we use - uses the `typing` module, which
4provides core functionality fundamental to mypy's functioning.
6Generally, `typing` would be imported at runtime and used in that fashion -
7it acts as a no-op at runtime and does not have any run-time overhead by
8design.
10As it turns out, `typing` is not vendorable - it uses separate sources for
11Python 2/Python 3. Thus, this codebase can not expect it to be present.
12To work around this, mypy allows the typing import to be behind a False-y
13optional to prevent it from running at runtime and type-comments can be used
14to remove the need for the types to be accessible directly during runtime.
16This module provides the False-y guard in a nicely named fashion so that a
17curious maintainer can reach here to read this.
19In packaging, all static-typing related imports should be guarded as follows:
21 from packaging._typing import TYPE_CHECKING
23 if TYPE_CHECKING:
24 from typing import ...
26Ref: https://github.com/python/mypy/issues/3216
27"""
29__all__ = ["TYPE_CHECKING", "cast"]
31# The TYPE_CHECKING constant defined by the typing module is False at runtime
32# but True while type checking.
33if False: # pragma: no cover
34 from typing import TYPE_CHECKING
35else:
36 TYPE_CHECKING = False
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.
42if TYPE_CHECKING: # pragma: no cover
43 # not executed at runtime
44 from typing import cast
45else:
46 # executed at runtime
47 def cast(type_, value): # noqa
48 return value