Skip to content

Commit

Permalink
Avoid instance checks for future like objects
Browse files Browse the repository at this point in the history
Runtime typing.Protocol checks do more than they should

Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
mhidalgo-bdai committed May 16, 2024
1 parent db08f8a commit 50e2d42
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions bdai_ros2_wrappers/bdai_ros2_wrappers/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
T = TypeVar("T", covariant=True)


@runtime_checkable
class FutureLike(Awaitable[T], Protocol[T]):
"""A future-like awaitable object.
Expand Down Expand Up @@ -40,6 +39,7 @@ def cancelled(self) -> bool:
...


@runtime_checkable
class FutureConvertible(Awaitable[T], Protocol[T]):
"""An awaitable that is convertible to a future-like object."""

Expand All @@ -53,9 +53,9 @@ def as_future(self) -> FutureLike[T]:

def as_proper_future(instance: AnyFuture) -> FutureLike:
"""Return `instance` as a proper future-like object."""
if isinstance(instance, FutureLike):
return instance
return instance.as_future()
if isinstance(instance, FutureConvertible):
return instance.as_future()
return instance


def wait_for_future(
Expand Down

0 comments on commit 50e2d42

Please sign in to comment.