Skip to content

Commit cd3c417

Browse files
author
Bogdan Kalashnikov
committed
DUnion merge
1 parent a34d7e6 commit cd3c417

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

attrs_api/dynamic_typing.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def __init__(self, t: Union[type, BaseType]):
1414
def __str__(self):
1515
return f"{self.__class__.__name__}[{self.type}]"
1616

17+
def __eq__(self, other):
18+
return isinstance(other, self.__class__) and self.type == other.type
19+
1720

1821
class Many(BaseType):
1922
__slots__ = ["types"]
@@ -25,13 +28,21 @@ def __str__(self):
2528
items = ', '.join(map(str, self.types))
2629
return f"{self.__class__.__name__}[{items}]"
2730

31+
def __eq__(self, other):
32+
return isinstance(other, self.__class__) and all(t1 == t2 for t1, t2 in zip(self.types, other.types))
33+
2834

2935
class DOptional(Single):
3036
pass
3137

3238

3339
class DUnion(Many):
34-
pass
40+
def __init__(self, *types: Union[type, BaseType]):
41+
unique_types = []
42+
for t in types:
43+
if t not in unique_types:
44+
unique_types.append(t)
45+
super().__init__(*unique_types)
3546

3647

3748
class DTuple(Many):

attrs_api/generator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ def _detect_type(self, value, convert_dict=True) -> META_TYPE:
8080
t = self._detect_type(item, convert_dict)
8181
types.append(t)
8282
if len(types) > 1:
83-
return DList(DUnion(*types))
83+
union = DUnion(*types)
84+
if len(union.types) == 1:
85+
return DList(*union.types)
86+
return DList(union)
8487
else:
8588
return DList(*types)
8689
else:

testing_tools/data.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@
2929
},
3030
{
3131
"null_or_bool": True,
32-
"str_number": "8"
32+
"str_number": "8",
33+
"3d_array": [
34+
[
35+
[0, 1, 2],
36+
[3, 4, 5],
37+
[6, 7, 8]
38+
],
39+
[
40+
[0, 1, 2],
41+
[3, 4, 5],
42+
[6, 7, 8]
43+
],
44+
[
45+
[0, 1, 2],
46+
[3, 4, 5],
47+
[6, 7, 8]
48+
]
49+
]
3350
},
3451
]

0 commit comments

Comments
 (0)