Skip to content

Commit 02c4420

Browse files
authored
Merge pull request #14 from xoeye/chore/OP-1150-python-3-10
Typecats 2.0.0
2 parents 4ed3701 + bf1dfe5 commit 02c4420

7 files changed

+56
-14
lines changed

CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# v2.0.0rc1
1+
# v2.0.0
22

33
Breaking changes:
44

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ features. The 4 core features are:
9494

9595
try:
9696
TestCat.struc(dict(name='', age=0))
97-
except StructuringError as ve:
97+
except typecats.StructuringError as ve:
9898
print(ve)
9999
# Attribute "name" on class <class 'TestCat'> with type <class 'str'> cannot have empty value ''!
100100
```

tests/test_gen_converter.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import typing as ty
22

3-
try:
4-
from typing import Protocol
5-
except ImportError:
6-
from typing_extensions import Protocol # type: ignore
7-
83
from attr import Factory as fac
94
from typecats import Cat, unstruc
5+
from typecats._compat import Protocol
106
from typecats.patch import is_attrs_class
117

128

tests/test_wildcats.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import typing as ty
21
import json
2+
import typing as ty
33

44
import pytest
5-
65
from typecats import Cat
6+
from typecats.exceptions import StructuringError
77

88
from data_utils import ld
99

@@ -215,3 +215,31 @@ class Wildcat2(dict):
215215
wc.n.i = 8
216216

217217
assert wc != Wildcat2.struc(base)
218+
219+
220+
def test_wildcat_struc_with_wildcat_backwards_compatibility():
221+
@Cat
222+
class Nested(dict):
223+
i: int
224+
225+
@Cat
226+
class WithNested:
227+
nested: Nested
228+
229+
# This used to work with BaseConverter but stopped working with GenConverter.
230+
c = WithNested.struc(dict(nested=Nested(i=6)))
231+
assert c.nested.i == 6
232+
233+
234+
def test_wildcat_struc_with_non_wildcat_does_not_work():
235+
@Cat
236+
class Nested:
237+
i: int
238+
239+
@Cat
240+
class WithNested:
241+
nested: Nested
242+
243+
# Make sure that this particular case only works with wildcats
244+
with pytest.raises(StructuringError):
245+
WithNested.struc(dict(nested=Nested(i=6)))

typecats/__about__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""typecats"""
2-
__version__ = "2.0.0rc1"
2+
__version__ = "2.0.0"
33
__author__ = "Peter Gaultney"
44
__author_email__ = "pgaultney@xoi.io"

typecats/_compat.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import sys
22

3-
if sys.version_info[:2] <= (3, 7):
4-
from typing_extensions import Literal # noqa
3+
version_info = sys.version_info[0:3]
4+
is_py37 = version_info[:2] == (3, 7)
5+
is_py38_plus = version_info[:2] >= (3, 8)
6+
is_py39_plus = version_info[:2] >= (3, 9)
7+
is_py310_plus = version_info[:2] >= (3, 10)
8+
is_py311_plus = version_info[:2] >= (3, 11)
9+
10+
if is_py38_plus:
11+
from typing import Literal, Protocol # type: ignore # noqa
12+
else:
13+
from typing_extensions import Literal, Protocol # type: ignore # noqa
14+
15+
if is_py311_plus:
16+
ExceptionGroup = ExceptionGroup # noqa
17+
BaseExceptionGroup = BaseExceptionGroup # noqa
518
else:
6-
from typing import Literal # noqa
19+
from exceptiongroup import ExceptionGroup, BaseExceptionGroup # noqa

typecats/patch.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ def structure_wildcat_factory(gen_converter: GenConverter, cls):
2424
def structure_typecat(dictionary, Type):
2525
try:
2626
with _consolidate_exceptions(gen_converter, Type):
27-
res = base_structure_func(dictionary, Type)
27+
if is_wildcat(Type) and isinstance(dictionary, Type):
28+
# Backwards compatibility for Cat.struc({"field": Wildcat(...)}) which worked with
29+
# the legacy BaseConverter but no longer works with GenConverter
30+
res = gen_converter.structure_attrs_fromdict(dictionary, Type)
31+
else:
32+
res = base_structure_func(dictionary, Type)
2833
if is_wildcat(Type):
2934
enrich_structured_wildcat(res, dictionary, Type)
3035
return res

0 commit comments

Comments
 (0)