Skip to content

Conversation

sobolevn
Copy link
Member

@github-actions

This comment has been minimized.

_FINISHED: Literal["FINISHED"] # undocumented

def isfuture(obj: object) -> bool: ...
def isfuture(obj: object) -> TypeGuard[futures.Future]: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At runtime this allows any object with a specific attribute:

    return (hasattr(obj.__class__, '_asyncio_future_blocking') and
            obj._asyncio_future_blocking is not None)

I don't think that's enough for a TypeGuard.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs put it this way:

https://docs.python.org/3/library/asyncio-future.html#asyncio.isfuture

a Future-like object with a _asyncio_future_blocking attribute.

https://github.com/python/cpython/blob/c27a33132be101e246ae2584f1826477357138d6/Lib/asyncio/futures.py#L60-L63

# - Its presence is a marker to declare that a class implements
#   the Future protocol (i.e. is intended to be duck-type compatible).
#   The value must also be not-None, to enable a subclass to declare
#   that it is not compatible by setting this to None.

In my opinion, this is enough to be a TypeGuard

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc regarding _asyncio_future_blocking is correct.

I have another question. asyncio.Future is a generic type.
Should the typeguard reflect it, annotating at least Future[Any]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Thanks!

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 749d3db into python:master Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants