Skip to content

Commit 832ff4e

Browse files
committed
in 3.6 types can have __args__ = None, as opposed to missing attribute
1 parent 3a77b31 commit 832ff4e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/cloudformation_cli_python_lib/recast.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ def _field_to_type(field: Any, key: str, classes: Dict[str, Any]) -> Any:
9090
# Assuming this is a generic object created by typing.Union
9191
try:
9292
possible_types = field.__args__
93+
if not possible_types:
94+
raise InvalidRequest(f"Cannot process type {field} for field {key}")
9395
except AttributeError:
9496
raise InvalidRequest(f"Cannot process type {field} for field {key}")
9597
# Assuming that the union is generated from typing.Optional, so only
9698
# contains one type and None
9799
# pylint: disable=unidiomatic-typecheck
98-
fields = [t for t in possible_types if type(None) != t] if possible_types else []
100+
fields = [t for t in possible_types if type(None) != t]
99101
if len(fields) != 1:
100102
raise InvalidRequest(f"Cannot process type {field} for field {key}")
101103
field = fields[0]

tests/lib/recast_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ def test_field_to_type_unhandled_types():
121121
assert str(excinfo.value).startswith("Cannot process type ")
122122

123123

124+
def test_field_to_type_unhandled_types_36():
125+
k = "key"
126+
127+
class SixType:
128+
__args__ = None
129+
130+
with pytest.raises(InvalidRequest) as excinfo:
131+
_field_to_type(SixType, k, {})
132+
assert str(excinfo.value).startswith("Cannot process type ")
133+
134+
135+
def test_field_to_type_unhandled_types_37():
136+
k = "key"
137+
138+
class SevenType:
139+
pass
140+
141+
with pytest.raises(InvalidRequest) as excinfo:
142+
_field_to_type(SevenType, k, {})
143+
assert str(excinfo.value).startswith("Cannot process type ")
144+
145+
124146
def test_get_forward_ref_type():
125147
with patch("cloudformation_cli_python_lib.recast.typing") as mock_typing:
126148
mock_typing.ForwardRef = "3.7+"

0 commit comments

Comments
 (0)