Correct HomeAssistantError __str__ implementation and test (#113991)

Correct HomeAssistant sub class implementation and test
This commit is contained in:
Jan Bouwhuis 2024-03-22 13:39:36 +01:00 committed by GitHub
parent ee2e98b475
commit 9ca253213c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 21 deletions

View File

@ -64,15 +64,6 @@ class HomeAssistantError(Exception):
return self._message
if not self.generate_message:
# Initialize self._message to the string repr of the class
# to prevent a recursive loop.
self._message = (
f"Parent class {self.__class__.__name__} is missing __str__ method"
)
# If the there is an other super class involved,
# we want to call its __str__ method.
# If the super().__str__ method is missing in the base_class
# the call will be recursive and we return our initialized default.
self._message = super().__str__()
return self._message

View File

@ -138,7 +138,6 @@ async def test_home_assistant_error_subclass(hass: HomeAssistant) -> None:
translation_placeholders: dict[str, str] | None = None,
) -> None:
super().__init__(
self,
translation_domain=translation_domain,
translation_key=translation_key,
translation_placeholders=translation_placeholders,
@ -158,7 +157,6 @@ async def test_home_assistant_error_subclass(hass: HomeAssistant) -> None:
translation_placeholders: dict[str, str] | None = None,
) -> None:
super().__init__(
self,
translation_domain=translation_domain,
translation_key=translation_key,
translation_placeholders=translation_placeholders,
@ -199,18 +197,12 @@ async def test_home_assistant_error_subclass(hass: HomeAssistant) -> None:
translation_key="bla",
translation_placeholders={"bla": "Bla"},
)
assert (
str(exc.value)
== "Parent class _SubExceptionConstructor is missing __str__ method"
)
assert str(exc.value) == "Bla from cache"
with pytest.raises(HomeAssistantError) as exc:
raise _SubExceptionConstructor(
"custom arg",
)
assert (
str(exc.value)
== "Parent class _SubExceptionConstructor is missing __str__ method"
)
assert str(exc.value) == ""
# A subclass with a constructor that generates the message
with pytest.raises(HomeAssistantError) as exc:
@ -244,7 +236,7 @@ async def test_home_assistant_error_subclass(hass: HomeAssistant) -> None:
)
assert str(exc.value) == "Bla from cache"
# A subclass with and ExceptionGroup subclass requires a message to be passed.
# A subclass with an ExceptionGroup subclass requires a message to be passed.
# As we pass args, we will not generate the message.
# The __str__ constructor defaults to that of the super class.
with pytest.raises(HomeAssistantError) as exc:
@ -263,7 +255,7 @@ async def test_home_assistant_error_subclass(hass: HomeAssistant) -> None:
)
assert str(exc.value) == "group message (2 sub-exceptions)"
# A subclass with and ExceptionGroup subclass requires a message to be passed.
# A subclass with an ExceptionGroup subclass requires a message to be passed.
# The `generate_message` flag is set.`
# The __str__ constructor will return the generated message.
with pytest.raises(HomeAssistantError) as exc: