|
| 1 | +# _typing.py - module for typing shims with reduced runtime overhead |
| 2 | +# |
| 3 | +# Copyright (C) 2025 David Salvisberg |
| 4 | +# |
| 5 | +# This library is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License as published by the Free Software Foundation; either |
| 8 | +# version 2.1 of the License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# This library is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +# Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public |
| 16 | +# License along with this library; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | +# 02110-1301 USA |
| 19 | + |
| 20 | +"""Compatibility shims for the Python typing module. |
| 21 | +
|
| 22 | +This module is designed in a way, such, that runtime use of |
| 23 | +annotations is possible starting with Python 3.9, but it still |
| 24 | +supports Python 3.6 - 3.8 if the package is used normally without |
| 25 | +introspecting the annotations of the API. |
| 26 | +
|
| 27 | +You should never import *from* this module, you should always import |
| 28 | +the entire module and then access the members via attribute access. |
| 29 | +
|
| 30 | +I.e. use the module like this: |
| 31 | +```python |
| 32 | +from stdnum import _typing as t |
| 33 | +
|
| 34 | +foo: t.Any = ... |
| 35 | +``` |
| 36 | +
|
| 37 | +Instead of like this: |
| 38 | +```python |
| 39 | +from stdnum._typing import Any |
| 40 | +
|
| 41 | +foo: Any = ... |
| 42 | +``` |
| 43 | +
|
| 44 | +The exception to that rule are `TYPE_CHECKING` `cast` and `deprecated` |
| 45 | +which can be used at runtime. |
| 46 | +""" |
| 47 | + |
| 48 | +from __future__ import annotations |
| 49 | + |
| 50 | + |
| 51 | +TYPE_CHECKING = False |
| 52 | +if TYPE_CHECKING: |
| 53 | + from collections.abc import Generator as Generator |
| 54 | + from collections.abc import Iterable as Iterable |
| 55 | + from collections.abc import Mapping as Mapping |
| 56 | + from collections.abc import Sequence as Sequence |
| 57 | + from typing import Any as Any |
| 58 | + from typing import IO as IO |
| 59 | + from typing import Literal as Literal |
| 60 | + from typing import cast as cast |
| 61 | + from typing_extensions import TypeAlias as TypeAlias |
| 62 | + from typing_extensions import deprecated as deprecated |
| 63 | + |
| 64 | + from stdnum._types import GSTINInfo as GSTINInfo |
| 65 | + from stdnum._types import IMSIInfo as IMSIInfo |
| 66 | + from stdnum._types import NumberValidationModule as NumberValidationModule |
| 67 | + from stdnum._types import PANInfo as PANInfo |
| 68 | +else: |
| 69 | + def cast(typ, val): |
| 70 | + """Cast a value to a type.""" |
| 71 | + return val |
| 72 | + |
| 73 | + class deprecated: # noqa: N801 |
| 74 | + """Simplified backport of `warnings.deprecated`. |
| 75 | +
|
| 76 | + This backport doesn't handle classes or async functions. |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self, message, category=DeprecationWarning, stacklevel=1): # noqa: D107 |
| 80 | + self.message = message |
| 81 | + self.category = category |
| 82 | + self.stacklevel = stacklevel |
| 83 | + |
| 84 | + def __call__(self, func): # noqa: D102 |
| 85 | + func.__deprecated__ = self.message |
| 86 | + |
| 87 | + if self.category is None: |
| 88 | + return func |
| 89 | + |
| 90 | + import functools |
| 91 | + import warnings |
| 92 | + |
| 93 | + @functools.wraps(func) |
| 94 | + def wrapper(*args, **kwargs): |
| 95 | + warnings.warn(self.message, category=self.category, stacklevel=self.stacklevel + 1) |
| 96 | + return func(*args, **kwargs) |
| 97 | + |
| 98 | + wrapper.__deprecated__ = self.message |
| 99 | + return wrapper |
| 100 | + |
| 101 | + def __getattr__(name): |
| 102 | + if name in {'Generator', 'Iterable', 'Mapping', 'Sequence'}: |
| 103 | + import collections.abc |
| 104 | + return getattr(collections.abc, name) |
| 105 | + elif name in {'Any', 'IO', 'Literal'}: |
| 106 | + import typing |
| 107 | + return getattr(typing, name) |
| 108 | + elif name == 'TypeAlias': |
| 109 | + import sys |
| 110 | + if sys.version_info >= (3, 10): |
| 111 | + import typing |
| 112 | + else: |
| 113 | + import typing_extensions as typing |
| 114 | + return getattr(typing, name) |
| 115 | + elif name in {'GSTINInfo', 'IMSIInfo', 'NumberValidationModule', 'PANInfo'}: |
| 116 | + import stdnum._types |
| 117 | + return getattr(stdnum._types, name) |
| 118 | + else: |
| 119 | + raise AttributeError(name) |
0 commit comments