Skip to content

Commit 414a067

Browse files
Don't assert PropertyKey types
This removes runtime type assertions when inserting typed values into TypedProperties. It only would work on generic-less values, so it isn't worth the effort. Normal typing also has no runtime guarantees, so this isn't exactly a change of behavior.
1 parent 9235ea7 commit 414a067

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

packages/smithy-core/src/smithy_core/interfaces/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ class PropertyKey[T](Protocol):
132132
value_type=str | int # type: ignore
133133
)
134134
135-
Type checkers will be able to use such a property as expected, and the
136-
``value_type`` property may still be used in ``isinstance`` checks since it also
137-
supports union types as of Python 3.10.
135+
Type checkers will be able to use such a property as expected.
138136
"""
139137

140138
key: str
@@ -191,9 +189,7 @@ class TypedProperties(Protocol):
191189
192190
assert assert_type(properties[UNION_PROPERTY], str | int) == "foo"
193191
194-
Type checkers will be able to use such a property as expected, and the
195-
``value_type`` property may still be used in ``isinstance`` checks since it also
196-
supports union types as of Python 3.10.
192+
Type checkers will be able to use such a property as expected.
197193
"""
198194

199195
@overload

packages/smithy-core/src/smithy_core/types.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ class PropertyKey[T](_PropertyKey[T]):
176176
value_type=str | int # type: ignore
177177
)
178178
179-
Type checkers will be able to use such a property as expected, and the
180-
``value_type`` property may still be used in ``isinstance`` checks since it also
181-
supports union types as of Python 3.10.
179+
Type checkers will be able to use such a property as expected.
182180
"""
183181

184182
key: str
@@ -204,6 +202,8 @@ class TypedProperties(UserDict[str, Any], _TypedProperties):
204202
typing to get it, and those who don't care about typing to not have to think about
205203
it.
206204
205+
No runtime type assertion is performed.
206+
207207
..code-block:: python
208208
209209
foo = PropertyKey(key="foo", value_type=str)
@@ -231,9 +231,7 @@ class TypedProperties(UserDict[str, Any], _TypedProperties):
231231
232232
assert assert_type(properties[UNION_PROPERTY], str | int) == "foo"
233233
234-
Type checkers will be able to use such a property as expected, and the
235-
``value_type`` property may still be used in ``isinstance`` checks since it also
236-
supports union types as of Python 3.10.
234+
Type checkers will be able to use such a property as expected.
237235
"""
238236

239237
@overload
@@ -248,13 +246,7 @@ def __setitem__[T](self, key: _PropertyKey[T], value: T) -> None: ...
248246
@overload
249247
def __setitem__(self, key: str, value: Any) -> None: ...
250248
def __setitem__(self, key: str | _PropertyKey[Any], value: Any) -> None:
251-
if isinstance(key, _PropertyKey):
252-
if not isinstance(value, key.value_type):
253-
raise ValueError(
254-
f"Expected value type of {key.value_type}, but was {type(value)}"
255-
)
256-
key = key.key
257-
self.data[key] = value
249+
self.data[key if isinstance(key, str) else key.key] = value
258250

259251
def __delitem__(self, key: str | _PropertyKey[Any]) -> None:
260252
del self.data[key if isinstance(key, str) else key.key]

packages/smithy-core/tests/unit/test_types.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,6 @@ def test_properties_typed_set() -> None:
262262
properties[foo_key] = "foo"
263263
assert properties.data["foo"] == "foo"
264264

265-
with pytest.raises(ValueError):
266-
properties[foo_key] = b"foo" # type: ignore
267-
268265

269266
def test_properties_del() -> None:
270267
foo_key = PropertyKey(key="foo", value_type=str)
@@ -348,5 +345,10 @@ def test_union_property() -> None:
348345
properties[union] = 1
349346
assert assert_type(properties.pop(union, b"bar"), str | int | bytes) == 1
350347

351-
with pytest.raises(ValueError):
352-
properties[union] = b"bar" # type: ignore
348+
349+
def test_parametric_property() -> None:
350+
properties = TypedProperties()
351+
parametric = PropertyKey(key="parametric", value_type=dict[str, str])
352+
properties[parametric] = {"foo": "bar"}
353+
354+
assert assert_type(properties[parametric], dict[str, str]) == {"foo": "bar"}

0 commit comments

Comments
 (0)