Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document union property key workaround #456

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions packages/smithy-core/src/smithy_core/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ class PropertyKey[T](Protocol):
Used with :py:class:`Context` to set and get typed values.

For a concrete implementation, see :py:class:`smithy_core.types.PropertyKey`.

Note that unions and other special types cannot easily be used here due to being
incompatible with ``type[T]``. PEP747 proposes a fix to this case, but it has not
yet been accepted. In the meantime, there is a workaround. The PropertyKey must
be assigned to an explicitly typed variable, and the ``value_type`` parameter of
the constructor must have a ``# type: ignore`` comment, like so:

.. code-block:: python

UNION_PROPERTY: PropertyKey[str | int] = PropertyKey(
key="union",
value_type=str | int # type: ignore
)

Type checkers will be able to use such a property as expected, and the
``value_type`` property may still be used in ``isinstance`` checks since it also
supports union types as of Python 3.10.
"""

key: str
Expand Down Expand Up @@ -151,11 +168,32 @@ class TypedProperties(Protocol):
properties = TypedProperties()
properties[foo] = "bar"

assert assert_type(properties[foo], str) == "bar
assert assert_type(properties["foo"], Any) == "bar

assert assert_type(properties[foo], str) == "bar"
assert assert_type(properties["foo"], Any) == "bar"

For a concrete implementation, see :py:class:`smithy_core.types.TypedProperties`.

Note that unions and other special types cannot easily be used here due to being
incompatible with ``type[T]``. PEP747 proposes a fix to this case, but it has not
yet been accepted. In the meantime, there is a workaround. The PropertyKey must
be assigned to an explicitly typed variable, and the ``value_type`` parameter of
the constructor must have a ``# type: ignore`` comment, like so:

.. code-block:: python

UNION_PROPERTY: PropertyKey[str | int] = PropertyKey(
key="union",
value_type=str | int # type: ignore
)

properties = TypedProperties()
properties[UNION_PROPERTY] = "foo"

assert assert_type(properties[UNION_PROPERTY], str | int) == "foo"

Type checkers will be able to use such a property as expected, and the
``value_type`` property may still be used in ``isinstance`` checks since it also
supports union types as of Python 3.10.
"""

@overload
Expand Down
46 changes: 43 additions & 3 deletions packages/smithy-core/src/smithy_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,25 @@ def format(self, *args: object, **kwargs: str) -> str:

@dataclass(kw_only=True, frozen=True, slots=True, init=False)
class PropertyKey[T](_PropertyKey[T]):
"""A typed property key."""
"""A typed property key.

Note that unions and other special types cannot easily be used here due to being
incompatible with ``type[T]``. PEP747 proposes a fix to this case, but it has not
yet been accepted. In the meantime, there is a workaround. The PropertyKey must
be assigned to an explicitly typed variable, and the ``value_type`` parameter of
the constructor must have a ``# type: ignore`` comment, like so:

.. code-block:: python

UNION_PROPERTY: PropertyKey[str | int] = PropertyKey(
key="union",
value_type=str | int # type: ignore
)

Type checkers will be able to use such a property as expected, and the
``value_type`` property may still be used in ``isinstance`` checks since it also
supports union types as of Python 3.10.
"""

key: str
"""The string key used to access the value."""
Expand Down Expand Up @@ -192,8 +210,30 @@ class TypedProperties(UserDict[str, Any], _TypedProperties):
properties = TypedProperties()
properties[foo] = "bar"

assert assert_type(properties[foo], str) == "bar
assert assert_type(properties["foo"], Any) == "bar
assert assert_type(properties[foo], str) == "bar"
assert assert_type(properties["foo"], Any) == "bar"

Note that unions and other special types cannot easily be used here due to being
incompatible with ``type[T]``. PEP747 proposes a fix to this case, but it has not
yet been accepted. In the meantime, there is a workaround. The PropertyKey must
be assigned to an explicitly typed variable, and the ``value_type`` parameter of
the constructor must have a ``# type: ignore`` comment, like so:

.. code-block:: python

UNION_PROPERTY: PropertyKey[str | int] = PropertyKey(
key="union",
value_type=str | int # type: ignore
)

properties = TypedProperties()
properties[UNION_PROPERTY] = "foo"

assert assert_type(properties[UNION_PROPERTY], str | int) == "foo"

Type checkers will be able to use such a property as expected, and the
``value_type`` property may still be used in ``isinstance`` checks since it also
supports union types as of Python 3.10.
"""

@overload
Expand Down
27 changes: 27 additions & 0 deletions packages/smithy-core/tests/unit/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,30 @@ def test_properties_typed_pop() -> None:
assert "foo" not in properties.data

assert properties.pop(foo_key) is None


def test_union_property() -> None:
properties = TypedProperties()
union: PropertyKey[str | int] = PropertyKey(
key="union",
value_type=str | int, # type: ignore
)

properties[union] = "foo"
assert assert_type(properties[union], str | int) == "foo"
assert assert_type(properties.get(union), str | int | None) == "foo"
assert assert_type(properties.get(union, b"foo"), str | int | bytes) == "foo"
assert assert_type(properties.pop(union), str | int | None) == "foo"
properties[union] = "foo"
assert assert_type(properties.pop(union, b"bar"), str | int | bytes) == "foo"

properties[union] = 1
assert assert_type(properties[union], str | int) == 1
assert assert_type(properties.get(union), str | int | None) == 1
assert assert_type(properties.get(union, b"foo"), str | int | bytes) == 1
assert assert_type(properties.pop(union), str | int | None) == 1
properties[union] = 1
assert assert_type(properties.pop(union, b"bar"), str | int | bytes) == 1

with pytest.raises(ValueError):
properties[union] = b"bar" # type: ignore