diff --git a/src/pyfly/container/registry.py b/src/pyfly/container/registry.py index c9c52ec..518752e 100644 --- a/src/pyfly/container/registry.py +++ b/src/pyfly/container/registry.py @@ -59,7 +59,7 @@ def display_name(self) -> str: return self.name impl = self.impl_type direct = getattr(impl, "__name__", None) - if direct is not None: + if isinstance(direct, str): return direct args = get_args(impl) if args: diff --git a/tests/container/test_registration_display_name.py b/tests/container/test_registration_display_name.py index ab85754..f818240 100644 --- a/tests/container/test_registration_display_name.py +++ b/tests/container/test_registration_display_name.py @@ -30,11 +30,9 @@ def test_display_name_is_union_safe() -> None: assert reg.display_name == "_A | _B" # no AttributeError -def test_display_name_handles_arbitrary_typing_construct() -> None: - from typing import Optional - - # Optional[_A] (== Union[_A, None]) and similar typing constructs must never - # raise; the exact rendering varies by Python version, but it is always a - # usable, non-empty name. - name = Registration(impl_type=Optional[_A]).display_name +def test_display_name_handles_optional_union() -> None: + # ``_A | None`` (the PEP 604 form of Optional) is also a ``types.UnionType`` + # and must never raise; the exact rendering may vary by Python version, but + # it is always a usable, non-empty name. + name = Registration(impl_type=(_A | None)).display_name assert isinstance(name, str) and name