Skip to content

Commit

Permalink
Deep dict type-checking x 5.
Browse files Browse the repository at this point in the history
This commit is the epochal last commit in a commit chain deeply
type-checking the first key-value pair of each **mapping type hint**
(e.g., of the form `dict[..., ...]`, `collections.defaultdict[...,
...]`, `collections.abc.Mapping[..., ...]`,
`collections.abc.MutableMapping[..., ...]`,
`collections.abc.OrderedDict[..., ...]`, `typing.Dict[..., ...]`,
`typing.Mapping[..., ...]`, `typing.MutableMapping[..., ...]`, or
`typing.OrderedDict[..., ...]`) in `O(1)` time, partially resolving
long-standing feature requests #167 kindly submitted sometime during my
most recent past life by ardent typing fiend @langfield *and*
#2021 kindly submitted sometime during the past life immediately
preceding my most recent past life by Equinor ASA bear bro @jondequinor
(Jonas Grønås). Specifically, this commit exhaustively exercises this
type-checking with PEP 585-compliant type hints and sample data.
**BOOM**! (*Basking basilisks!*)
  • Loading branch information
leycec committed Mar 14, 2024
1 parent eb4f6fb commit 9f227bd
Show file tree
Hide file tree
Showing 4 changed files with 366 additions and 31 deletions.
27 changes: 27 additions & 0 deletions beartype_test/a00_unit/data/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Iterator,
)
from beartype._util.func.utilfuncmake import make_func
from collections import defaultdict
from contextlib import contextmanager
from enum import Enum
from functools import (
Expand Down Expand Up @@ -517,6 +518,32 @@ def function_module_name_none() -> None:
Absolute filename of the current submodule, declared purely for convenience.
'''

# ....................{ DICTS }....................
def _default_dict_str_factory() -> str:
'''
Arbitrary factory function returning arbitrary strings.
'''

return 'His steps to the sea-shore. A swan was there,'


default_dict_int_to_str = defaultdict(_default_dict_str_factory)
'''
Non-empty default dictionary mapping integers to strings, initialized with an
arbitrary factory function returning arbitrary strings.
'''
default_dict_int_to_str[0] = 'Beside a sluggish stream among the reeds.'
default_dict_int_to_str[1] # == 'His steps to the sea-shore. A swan was there,'


default_dict_str_to_str = defaultdict(_default_dict_str_factory)
'''
Non-empty default dictionary mapping strings to strings, initialized with an
arbitrary factory function returning arbitrary strings.
'''
default_dict_str_to_str['His eyes pursued its flight.'] = (
'Thou hast a home,')

# ....................{ SETS ~ callable }....................
CALLABLES_PYTHON = frozenset((
function,
Expand Down

0 comments on commit 9f227bd

Please sign in to comment.