Skip to content

Commit

Permalink
Todo Use TypeGuard for HassJob type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Dec 13, 2024
1 parent 29757da commit e62d623
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
NotRequired,
Self,
TypedDict,
TypeGuard,
cast,
overload,
reveal_type,
)

from propcache import cached_property, under_cached_property
Expand Down Expand Up @@ -362,6 +364,12 @@ def cancel_on_shutdown(self) -> bool | None:
"""Return if the job should be cancelled on shutdown."""
return self._cancel_on_shutdown

@staticmethod
def is_coroutinefunc[**_Q, _Rx](
job: HassJob[_Q, Coroutine[Any, Any, _Rx] | _Rx],
) -> TypeGuard[HassJob[_Q, Coroutine[Any, Any, _Rx]]]:
return job.job_type is HassJobType.Coroutinefunction

def __repr__(self) -> str:
"""Return the job."""
return f"<Job {self.name} {self.job_type} {self.target}>"
Expand Down Expand Up @@ -752,9 +760,12 @@ def _async_add_hass_job[_R](
# if TYPE_CHECKING to avoid the overhead of constructing
# the type used for the cast. For history see:
# https://github.com/home-assistant/core/pull/71960
if hassjob.job_type is HassJobType.Coroutinefunction:
reveal_type(hassjob)
# if hassjob.job_type is HassJobType.Coroutinefunction:
if HassJob.is_coroutinefunc(hassjob):
if TYPE_CHECKING:
hassjob = cast(HassJob[..., Coroutine[Any, Any, _R]], hassjob)
reveal_type(hassjob)
task = create_eager_task(
hassjob.target(*args), name=hassjob.name, loop=self.loop
)
Expand Down

0 comments on commit e62d623

Please sign in to comment.