Skip to content
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

Accessing dunders via type object produces TypeVar clashes #14814

Open
oremanj opened this issue Mar 1, 2023 · 0 comments
Open

Accessing dunders via type object produces TypeVar clashes #14814

oremanj opened this issue Mar 1, 2023 · 0 comments
Labels
bug mypy got something wrong

Comments

@oremanj
Copy link
Contributor

oremanj commented Mar 1, 2023

Consider the following code:

from typing import AsyncIterator, TypeVar

T = TypeVar("T")

async def anext_via_instance(it: AsyncIterator[T]) -> T:
    return await it.__anext__()

async def anext_via_type(it: AsyncIterator[T]) -> T:
    anext = type(it).__anext__
    return await anext(it)

mypy happily accepts anext_via_instance, but in anext_via_type it fails with:

main.py:10: error: Incompatible return value type (got "_T_co", expected "T")  [return-value]
main.py:10: error: Argument 1 has incompatible type "AsyncIterator[T]"; expected "AsyncIterator[_T_co]"  [arg-type]

If I add a reveal_type(anext) in anext_via_type:

main.py:10: note: Revealed type is "def (self: typing.AsyncIterator[_T_co`1]) -> typing.Awaitable[_T_co`1]"

This behavior doesn't seem to be a recent change; it showed up on every version I spot-checked on the playground, and is still present on master. From the error message it looks like some kind of issue with TypeVar inference, but I'm not an expert.

(I'm trying to write something like anext_via_type in order to mimic the interpreter's behavior accessing a magic method in a specific circumstance; it won't accept an instance attribute, only a class attribute, so I don't want to accept an instance attribute either.)

@oremanj oremanj added the bug mypy got something wrong label Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant