Skip to content

Commit ab1e8e1

Browse files
1.6.0: Address bug identified in #41
1 parent 584c7bf commit ab1e8e1

34 files changed

+3180
-454
lines changed
11.7 KB
Binary file not shown.

dist/type_enforced-1.6.0.tar.gz

18.1 KB
Binary file not shown.

docs/0.0.16/type_enforced.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/0.0.16/type_enforced/enforcer.html

Lines changed: 4 additions & 3 deletions
Large diffs are not rendered by default.

docs/1.1.1/type_enforced.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/1.1.1/type_enforced/enforcer.html

Lines changed: 4 additions & 3 deletions
Large diffs are not rendered by default.

docs/1.2.0/type_enforced.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/1.2.0/type_enforced/enforcer.html

Lines changed: 4 additions & 3 deletions
Large diffs are not rendered by default.

docs/1.3.0/type_enforced.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/1.3.0/type_enforced/enforcer.html

Lines changed: 4 additions & 3 deletions
Large diffs are not rendered by default.

docs/1.3.0/type_enforced/utils.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/1.4.0/type_enforced.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/1.4.0/type_enforced/enforcer.html

Lines changed: 4 additions & 3 deletions
Large diffs are not rendered by default.

docs/1.4.0/type_enforced/utils.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/1.5.0/type_enforced.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/1.5.0/type_enforced/enforcer.html

Lines changed: 4 additions & 3 deletions
Large diffs are not rendered by default.

docs/1.5.0/type_enforced/utils.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/1.6.0/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="refresh" content="0; url=./type_enforced.html"/>
6+
</head>
7+
</html>

docs/1.6.0/search.js

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/1.6.0/type_enforced.html

Lines changed: 274 additions & 0 deletions
Large diffs are not rendered by default.

docs/1.6.0/type_enforced/enforcer.html

Lines changed: 960 additions & 0 deletions
Large diffs are not rendered by default.

docs/1.6.0/type_enforced/utils.html

Lines changed: 1226 additions & 0 deletions
Large diffs are not rendered by default.

docs/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/type_enforced.html

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

docs/type_enforced/enforcer.html

Lines changed: 406 additions & 403 deletions
Large diffs are not rendered by default.

docs/type_enforced/utils.html

Lines changed: 103 additions & 3 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
1212

1313
[project]
1414
name = "type_enforced"
15-
version = "1.5.0"
15+
version = "1.6.0"
1616
description = "A pure python type enforcer for python type annotations"
1717
authors = [
1818
{name="Connor Makowski", email="conmak@mit.edu"}

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = type_enfoced
3-
version = 1.5.0
3+
version = 1.6.0
44
description_file = README.md
55

66
[options]

test/test_fn_17.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type_enforced
2+
3+
4+
@type_enforced.Enforcer
5+
def my_fn(x: [list[str] | list[int] | list[list[int]]]) -> None:
6+
pass
7+
8+
9+
def inv_my_fn(x: [list[int] | list[str] | list[list[int]]]) -> None:
10+
pass
11+
12+
13+
success = True
14+
15+
try:
16+
my_fn([1, 2, 3]) # Passes
17+
my_fn(["a", "b", "c"]) # Passes
18+
my_fn([[1, 2], [3, 4]]) # Passes
19+
20+
inv_my_fn([1, 2, 3]) # Passes
21+
inv_my_fn(["a", "b", "c"]) # Passes
22+
inv_my_fn([[1, 2], [3, 4]]) # Passes
23+
except:
24+
success = False
25+
26+
# TODO: In the next major version, add support to enforce
27+
# multiple different types of nested lists but not mixed types
28+
# try:
29+
# my_fn([1, "a", 3])
30+
# success = False
31+
# except:
32+
# pass
33+
34+
35+
if success:
36+
print(f"test_fn_17.py passed")
37+
else:
38+
print(f"test_fn_17.py failed")

test/test_fn_18.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type_enforced
2+
from typing import Literal
3+
4+
5+
@type_enforced.Enforcer
6+
def my_fn(a: Literal["bar"] | int) -> None:
7+
pass
8+
9+
10+
success = True
11+
# TODO: In the next major version, add support to enforce combinations of literals as types
12+
# and not constraints.
13+
# try:
14+
# my_fn("bar") # Passes
15+
# my_fn(1) # Passes
16+
# except:
17+
# success = False
18+
19+
20+
if success:
21+
print(f"test_fn_18.py passed")
22+
else:
23+
print(f"test_fn_18.py failed")

type_enforced/enforcer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
)
99
from typing import Type, Union, Sized, Literal, Callable
1010
from functools import update_wrapper, wraps
11-
from type_enforced.utils import Partial, GenericConstraint
11+
from type_enforced.utils import Partial, GenericConstraint, DeepMerge
1212

1313
# Python 3.10+ has a UnionType object that is used to represent Union types
1414
try:
@@ -84,8 +84,9 @@ def __get_checkable_type__(self, item_annotation):
8484
elif hasattr(valid_type, "__origin__"):
8585
# Special code for iterable types (e.g. list, tuple, dict, set) including typing iterables
8686
if valid_type.__origin__ in [list, tuple, dict, set]:
87-
valid_types[valid_type.__origin__] = (
88-
self.__get_checkable_type__(valid_type.__args__)
87+
valid_types[valid_type.__origin__] = DeepMerge(
88+
original=valid_types.get(valid_type.__origin__, {}),
89+
update=self.__get_checkable_type__(valid_type.__args__),
8990
)
9091
# Handle any generic aliases
9192
elif isinstance(valid_type, GenericAlias):

type_enforced/utils.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import types, re
1+
import types, re, copy
22
from functools import update_wrapper
33

44

@@ -231,3 +231,35 @@ def __init__(
231231
self.__constraint_checks__[f"Not Equal To ({ne})"] = (
232232
lambda x: x != ne
233233
)
234+
235+
236+
def DeepMerge(original: dict, update: dict):
237+
"""
238+
Merge two dictionaries together, recursively merging any nested dictionaries and extending any nested lists.
239+
240+
Required Arguments:
241+
242+
- `original`:
243+
- What: The original dictionary to merge the update into.
244+
- Type: dict
245+
- `update`:
246+
- What: The dictionary to merge into the original dictionary.
247+
- Type: dict
248+
"""
249+
original = copy.deepcopy(original)
250+
for key, value in update.items():
251+
if (
252+
key in original
253+
and isinstance(original[key], dict)
254+
and isinstance(value, dict)
255+
):
256+
DeepMerge(original[key], value)
257+
elif (
258+
key in original
259+
and isinstance(original[key], list)
260+
and isinstance(value, list)
261+
):
262+
original[key].extend(value)
263+
else:
264+
original[key] = value
265+
return original

update_documentation.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
VERSION="1.5.0"
3-
OLD_DOC_VERSIONS="1.4.0 1.3.0 1.2.0 1.1.1 0.0.16"
2+
VERSION="1.6.0"
3+
OLD_DOC_VERSIONS="1.5.0 1.4.0 1.3.0 1.2.0 1.1.1 0.0.16"
44

55
rm -r ./docs
66
python3 -m virtualenv venv

update_version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
python -m build
2-
twine upload dist/*
2+
python -m twine upload dist/*

0 commit comments

Comments
 (0)