8000 Use `TypeIs` is for `iscoroutinefunction` by max-muoto · Pull Request #12148 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Use TypeIs is for iscoroutinefunction #12148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

max-muoto
Copy link
Contributor
@max-muoto max-muoto commented Jun 17, 2024

Use TypeIs for iscoroutinefunction to provide more accurate types in the negative case.

#8009 (comment) talks about former issues with the negative cases before TypeIs.

@max-muoto max-muoto marked this pull request as draft June 17, 2024 02:53
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/web_urldispatcher.py:183:14: error: Left operand of "and" is always true  [redundant-expr]
+ aiohttp/web_urldispatcher.py:183:14: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-redundant-expr for more info

@max-muoto
Copy link
Contributor Author
max-muoto commented Jun 17, 2024

@JelleZijlstra It seems in this case:

if iscoroutinefunction(z):
    assert_type(z, Callable[[str, int], Coroutine[Any, Any, Any]])

The intersection type "(str, int) -> (str | Awaitable[bytes])" we now get is less accurate, since we know it's not awaitable. Curious if you have any thoughts or potential workarounds, since it would be a little bit of a regression.

Or perhaps it's just wroth the trade-off.

@JelleZijlstra
Copy link
Member

This is incorrect because iscoroutinefunction() does not (and cannot) return True for all callables that return a Coroutine.

@max-muoto
Copy link
Contributor Author

This is incorrect because iscoroutinefunction() does not (and cannot) return True for all callables that return a Coroutine.

I see, closing this out then!

@max-muoto max-muoto closed this Jun 17, 2024
@jmehnle
Copy link
Contributor
jmehnle commented Aug 13, 2024

Eeeek, does this mean that despite TypeIs we still don't have a solution for totally narrowing types in if inspect.iscoroutinefunction(f): ... / else: ... constructs?

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
2A8D
0