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

Issue with decorator and generic type arguments #7033

Closed
saulshanabrook opened this issue Jun 21, 2019 · 2 comments
Closed

Issue with decorator and generic type arguments #7033

saulshanabrook opened this issue Jun 21, 2019 · 2 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables

Comments

@saulshanabrook
Copy link

saulshanabrook commented Jun 21, 2019

I seem to be getting some odd incorrect types when I wrap a method in a decorator and that method takes generic parameters.

Here is a minimal example that should pass type checking, but fails on the last line with Incompatible return value type (got "Abstraction[V, V]", expected "Abstraction[T, V]"):

from __future__ import annotations


import typing

T = typing.TypeVar("T")
U = typing.TypeVar("U")
V = typing.TypeVar("V")


def decorator(f: T) -> T:
    ...


class Abstraction(typing.Generic[T, U]):
    @decorator
    def __add__(self, other: Abstraction[U, V]) -> Abstraction[T, V]:
        ...


def fn() -> Abstraction[T, V]:
    return Abstraction[T, U]() + Abstraction[U, V]()

And here is a slightly more realistic version of the same example, with the same error:

from __future__ import annotations


import typing

T = typing.TypeVar("T")
U = typing.TypeVar("U")
V = typing.TypeVar("V")


def decorator(f: T) -> T:
    ...


class Abstraction(typing.Generic[T, U]):
    def __init__(self, fn: typing.Callable[[T], U]):
        ...

    @decorator
    def __add__(self, other: Abstraction[U, V]) -> Abstraction[T, V]:
        ...


def fn(f1: typing.Callable[[T], U], f2: typing.Callable[[U], V]) -> Abstraction[T, V]:
    return Abstraction(f1) + Abstraction(f2)

I tested this with the last master commit:

$ mypy --version
mypy 0.720+dev.48916e63403645730a584d6898fbe925d513a841

I tried it with the new semantic analyzer on and off and I get the same result.

EDIT: Not that if I do not use the decorator both of these examples type check properly.

@ilevkivskyi
Copy link
Member

I am not sure this is a duplicate, but most likely related to #1317 (this is one of the oldest high-priority issues).

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables labels Jul 10, 2019
@AlexWaygood
Copy link
Member

The false-positive error is no longer emitted in mypy 0.750+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables
Projects
None yet
Development

No branches or pull requests

3 participants