Skip to content

Commit

Permalink
"typing_extensions.Annotated" x 17.
Browse files Browse the repository at this point in the history
This commit is the next in a commit chain adding transparent support
for the third-party `typing_extensions.Annotated` type hint back-ported
to Python < 3.9, en route to resolving #34. Once finalized, this will
enable usage of beartype validators under Python < 3.9 via this hint.
Specifically, this commit continues disastrously breaking literally
everything by continuing to disembowel the feckless
`beartype._util.hint.data.pep.datapep` submodule and its untrustworthy
`beartype._util.hint.data.pep.proposal` crony subpackage in favour of
`beartype._util.hint.data.pep.sign`, which is the only subpackage left
standing. Save us from our reckless selves, GitHub! (*Imponderable ponderosa!*)
  • Loading branch information
leycec committed Jun 28, 2021
1 parent 3439bc9 commit e120577
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 259 deletions.
31 changes: 0 additions & 31 deletions beartype/_util/data/hint/pep/datapep.py

This file was deleted.

2 changes: 1 addition & 1 deletion beartype/_util/data/hint/pep/datapepattr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from beartype._util.utilobject import SENTINEL
from typing import Any

# ....................{ ATTRS ~ declare }....................
# ....................{ ATTRS }....................
# Default *ALL* signs (particularly signs with *NO* sane fallbacks) to a
# sentinel guaranteed to *NEVER* match.

Expand Down
24 changes: 24 additions & 0 deletions beartype/_util/data/hint/pep/datapepmodule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# --------------------( LICENSE )--------------------
# Copyright (c) 2014-2021 Beartype authors.
# See "LICENSE" for further details.

'''
Project-wide **typing module globals** (i.e., global constants pertaining to
modules declaring official PEP-compliant type hints).
This private submodule is *not* intended for importation by downstream callers.
'''

# ....................{ IMPORTS }....................
# See the "beartype.cave" submodule for further commentary.
__all__ = ['STAR_IMPORTS_CONSIDERED_HARMFUL']

# ....................{ TUPLES }....................
HINT_PEP_MODULE_NAMES = ('typing', 'typing_extensions')
'''
Tuple of the names of all **typing modules** (i.e., modules whose attributes
are usable for creating PEP-compliant type hints officially accepted by both
static and runtime type checkers) regardless of whether those modules are
actually importable under the active Python interpreter.
'''
38 changes: 38 additions & 0 deletions beartype/_util/data/hint/pep/datapeprepr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# --------------------( LICENSE )--------------------
# Copyright (c) 2014-2021 Beartype authors.
# See "LICENSE" for further details.

'''
Project-wide **bare PEP-compliant type hint representations** (i.e., global
constants pertaining to machine-readable strings returned by the :func:`repr`
builtin suffixed by *no* "["- and "]"-delimited subscription representations).
This private submodule is *not* intended for importation by downstream callers.
'''

# ....................{ IMPORTS }....................
from beartype._util.data.hint.pep.proposal.datapep484 import (
HINT_PEP484_BARE_ATTRS_DEPRECATED)

# See the "beartype.cave" submodule for further commentary.
__all__ = ['STAR_IMPORTS_CONSIDERED_HARMFUL']

# ....................{ SETS }....................
HINT_PEP_BARE_REPRS_DEPRECATED = (
HINT_PEP484_BARE_REPRS_DEPRECATED
)
'''
Frozen set of all **bare deprecated PEP-compliant type hint representations**
(i.e., machine-readable strings returned by the :func:`repr` builtin suffixed
by *no* "["- and "]"-delimited subscription representations for all obsoleted
hints, often by equivalent hints standardized under more recent PEPs).
'''

# ....................{ TUPLES }....................
HINT_PEP_MODULE_NAMES = ('typing', 'typing_extensions')
'''
Tuple of the names of all **typing modules** (i.e., modules declaring official
PEP-compliant type hints) regardless of whether those modules are actually
importable under the active Python interpreter.
'''
228 changes: 63 additions & 165 deletions beartype/_util/data/hint/pep/proposal/datapep484.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,12 @@

# ....................{ IMPORTS }....................
import typing
from beartype._cave._cavefast import NoneType
from beartype._util.data.hint.pep.sign.datapepsigns import (
HintSignForwardRef,
HintSignGeneric,
HintSignNewType,
HintSignTypeVar,
)
from beartype._util.data.hint.pep.datapepmodule import HINT_PEP_MODULE_NAMES
from beartype._util.py.utilpyversion import (
IS_PYTHON_3_6,
IS_PYTHON_AT_LEAST_3_7,
IS_PYTHON_AT_LEAST_3_8,
IS_PYTHON_AT_LEAST_3_9,
IS_PYTHON_AT_LEAST_3_7,
)
from sys import version_info
from typing import (
AbstractSet,
Any,
AsyncGenerator,
AsyncIterable,
AsyncIterator,
Awaitable,
ByteString,
Callable,
ChainMap,
Collection,
Container,
ContextManager,
Coroutine,
Counter,
DefaultDict,
Deque,
Dict,
FrozenSet,
Generator,
Generic,
ItemsView,
Iterable,
Iterator,
KeysView,
List,
MappingView,
Mapping,
Match,
MutableMapping,
MutableSequence,
MutableSet,
Optional,
Pattern,
Reversible,
Sequence,
Set,
Tuple,
Type,
Union,
ValuesView,
)
from typing import Tuple

# See the "beartype.cave" submodule for further commentary.
__all__ = ['STAR_IMPORTS_CONSIDERED_HARMFUL']
Expand All @@ -91,119 +41,67 @@
:pep:`484`-compliant empty fixed-length tuple type hint.
'''

# ....................{ SETS ~ attr }....................
HINT_PEP484_BARE_ATTRS_DEPRECATED: FrozenSet[Any] = frozenset()
# ....................{ SETS }....................
HINT_PEP484_BARE_REPRS_DEPRECATED = frozenset(
# If the active Python interpreter targets Python >= 3.9 and thus
# supports PEP 585, list all bare PEP 484-compliant representations (e.g.,
# "typing.List") that have since been obsoleted by equivalent bare PEP
# 585-compliant representations (e.g., "list"), defined as...
(
# Each representation relative to each module name...
f'{typing_module_name}.{hint_bare_repr}'
# For each bare deprecated PEP 484-compliant representation...
for hint_bare_repr in {
'AbstractSet',
'AbstractSet',
'AsyncContextManager',
'AsyncGenerator',
'AsyncIterable',
'AsyncIterator',
'Awaitable',
'ByteString',
'Callable',
'ChainMap',
'Collection',
'Container',
'ContextManager',
'Coroutine',
'Counter',
'DefaultDict',
'Deque',
'Dict',
'FrozenSet',
'Generator',
'Hashable',
'ItemsView',
'Iterable',
'Iterator',
'KeysView',
'List',
'MappingView',
'Mapping',
'Match',
'MutableMapping',
'MutableSequence',
'MutableSet',
'OrderedDict',
'Pattern',
'Reversible',
'Sequence',
'Set',
'Sized',
'Tuple',
'Type',
'ValuesView',
}
# For the name of each top-level hinting module...
for typing_module_name in HINT_PEP_MODULE_NAMES
) if IS_PYTHON_AT_LEAST_3_9 else
# Else, the active Python interpreter only targets Python < 3.9 and thus
# fails to support PEP 585. In this case, deprecate nothing.
()
)
'''
Frozen set of all :pep:`484`-compliant **deprecated typing attributes** (i.e.,
:pep:`484`-compliant :mod:`typing` type hints obsoleted by more recent PEPs).
'''

# ....................{ INITIALIZERS }....................
def _init() -> None:
'''
Initialize this submodule.
'''

# ..................{ VARS }..................
# Submodule globals to be redefined below.
global \
HINT_PEP484_ATTRS_DEPRECATED, \
HINT_PEP484_ATTRS_ISINSTANCEABLE, \

# Set of all PEP 484-compliant unsubscripted attributes originating from an
# origin type.
_HINT_PEP484_ATTRS_TYPE_STDLIB: set = None # type: ignore[assignment]

# List of all signs uniquely identifying PEP 484-compliant type hints
# originating from an origin type.
__HINT_PEP484_ATTRS_TYPE_STDLIB_LIST = [
AbstractSet,
AsyncGenerator,
AsyncIterable,
AsyncIterator,
Awaitable,
ByteString,
Callable,
ChainMap,
Collection,
Container,
ContextManager,
Coroutine,
Counter,
DefaultDict,
Deque,
Dict,
FrozenSet,
Generator,
ItemsView,
Iterable,
Iterator,
KeysView,
List,
MappingView,
Mapping,
Match,
MutableMapping,
MutableSequence,
MutableSet,
Pattern,
Reversible,
Sequence,
Set,
Tuple,
Type,
ValuesView,
]

# If the active Python interpreter targets at least various Python
# versions, add PEP 484-specific signs introduced in those versions.
if IS_PYTHON_AT_LEAST_3_7:
__HINT_PEP484_ATTRS_TYPE_STDLIB_LIST.extend((
typing.AsyncContextManager,

# Although the Python 3.6-specific implementation of the "typing"
# module *DOES* technically supply these attributes, it does so
# only non-deterministically. For unknown reasons (whose underlying
# cause appears to be unwise abuse of private fields of the
# critical stdlib "abc.ABCMeta" metaclass), the "typing.Hashable"
# and "typing.Sized" abstract base classes (ABCs) spontaneously
# interchange themselves with the corresponding
# "collections.abc.Hashable" and "collections.abc.Sized" ABCs after
# indeterminate importations and/or reference to these ABCs.
#
# This issue is significantly concerning that we would ideally
# simply drop Python 3.6 support. Unfortunately, that would also
# mean dropping PyPy3 support, which has yet to stabilize Python
# 3.7 support. Ergo, we reluctantly preserve Python 3.6 and thus
# PyPy3 support for the interim.
typing.Hashable,
typing.Sized,
))

# If the active Python interpreter targets Python >= 3.7.2...
#
# Note that this is the *ONLY* test against Python >= 3.7.2 in the
# codebase and thus done manually rather than with a global boolean.
if version_info >= (3, 7, 2):
# Add the "typing.OrderedDict" sign first introduced by the patch
# release Python 3.7.2. Yes, this is insane. Yes, this is "typing".
__HINT_PEP484_ATTRS_TYPE_STDLIB_LIST.append(typing.OrderedDict)

# Coerce this list into a frozen set for subsequent constant-time lookup.
_HINT_PEP484_ATTRS_TYPE_STDLIB = frozenset(
__HINT_PEP484_ATTRS_TYPE_STDLIB_LIST)

# ..................{ SETS ~ signs }..................
# If the active Python interpreter targets at least Python >= 3.9 and thus
# supports PEP 585, add all signs uniquely identifying outdated PEP
# 484-compliant type hints (e.g., "typing.List[int]") that have since been
# obsoleted by the equivalent PEP 585-compliant type hints (e.g.,
# "list[int]"). Happily, this is exactly the set of all PEP 484-compliant
# signs uniquely identifying PEP 484-compliant type hints originating from
# origin types.
if IS_PYTHON_AT_LEAST_3_9:
HINT_PEP484_ATTRS_DEPRECATED = _HINT_PEP484_ATTRS_TYPE_STDLIB


# Initialize this submodule.
_init()
8 changes: 4 additions & 4 deletions beartype/_util/data/hint/pep/proposal/datapep544.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ def _init() -> None:
'''

# ..................{ VERSIONS }..................
# If the active Python interpreter does *NOT* target at least Python >= 3.8
# and thus fails to support PEP 544, silently reduce to a noop.
# If the active Python interpreter only targets Python < 3.8 and thus fails
# to support PEP 544, silently reduce to a noop.
if not IS_PYTHON_AT_LEAST_3_8:
return
# Else, the active Python interpreter targets at least Python >= 3.8 and
# thus supports PEP 593.
# Else, the active Python interpreter targets Python >= 3.8 and thus
# supports PEP 593.

# ..................{ IMPORTS }..................
# Defer Python version-specific imports.
Expand Down
8 changes: 4 additions & 4 deletions beartype/_util/data/hint/pep/proposal/datapep585.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def _init() -> None:
'''

# ..................{ VERSIONS }..................
# If the active Python interpreter does *NOT* target at least Python >= 3.9
# and thus fails to support PEP 585, silently reduce to a noop.
# If the active Python interpreter only targets Python < 3.9 and thus fails
# to support PEP 585, silently reduce to a noop.
if not IS_PYTHON_AT_LEAST_3_9:
return
# Else, the active Python interpreter targets at least Python >= 3.9 and
# thus supports PEP 585.
# Else, the active Python interpreter targets Python >= 3.9 and thus
# supports PEP 585.

# ..................{ IMPORTS }..................
# Defer Python >= 3.9-specific imports.
Expand Down

0 comments on commit e120577

Please sign in to comment.