Skip to content

Commit 160ea6a

Browse files
committed
release 2.21
1 parent eb96b80 commit 160ea6a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/structures.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,22 @@ This is done by calling the class method "from_trusted_data". For example:
917917
The method "from_trusted_data" is similar to "from_other_class" method, but is much faster, since it bypasses the
918918
validation. The difference in speed is x10-x25, based on the complexity of the Structure.
919919

920+
Another option is to declare that instantiation of this Structure should always trust the input, and then
921+
any following instantiation will shortcut the validation logic. It has the same effect as "from_trusted_data".
922+
923+
.. code-block:: python
924+
925+
Person.trust_supplied_values(True)
926+
927+
# this will be fast...
928+
person = Person(
929+
first_name=f"joe-1",
930+
last_name="smith",
931+
role=Role.admin,
932+
....
933+
)
934+
935+
920936
921937
Global Defaults
922938
===============

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
license="MIT",
4747
long_description=long_description,
4848
url="http://github.com/loyada/typedpy",
49-
download_url="https://github.com/loyada/typedpy/archive/v2.20.4.tar.gz",
49+
download_url="https://github.com/loyada/typedpy/archive/v2.21.1.tar.gz",
5050
keywords=["testing", "type-safe", "strict", "schema", "validation"],
51-
version="2.20.4",
51+
version="2.21.1",
5252
)
5353

5454
# coverage run --source=typedpy/ setup.py test

tests/test_trusted_instantiation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ def test_nested_trusted_instantiation():
150150
assert employee == trusted_employee
151151

152152

153+
def test_trusted_instantiation_different_style():
154+
start = time.time()
155+
validated_policies = build_policies(hard_limit=20, codes=[1, 2, 3], time_days=7)
156+
time_1 = time.time()
157+
Policy.trust_supplied_values(True)
158+
fast_policies = build_policies(hard_limit=20, codes=[1, 2, 3], time_days=7)
159+
time_2 = time.time()
160+
print(f"validated policies took: {time_1 - start}")
161+
print(f"fast policies took: {time_2 - time_1}")
162+
print(f"ratio: {(time_1 - start) / (time_2 - time_1)}")
163+
assert validated_policies == fast_policies
164+
165+
153166
def test_nested_trusted_instantiation_with_dict():
154167
employee = create_employee()
155168
trusted_employee = Employee.from_trusted_data(employee.to_other_class(dict))

0 commit comments

Comments
 (0)