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

a decorator that modifies another decorator gives an incorrect type error #9265

Open
glyph opened this issue Aug 4, 2020 · 2 comments
Open
Labels
bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults

Comments

@glyph
Copy link

glyph commented Aug 4, 2020

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?

Bug.

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Any, Callable, TypeVar

_FirstDecoratee = TypeVar("_FirstDecoratee", bound=Callable[..., Any])
_SecondDecoratee = TypeVar("_SecondDecoratee", bound=Callable[..., Any])


def basic_decorator(decoratee: _FirstDecoratee) -> _FirstDecoratee:
    "decorate a function"
    return decoratee


def decorator_transformer(
    decorator: Callable[[_SecondDecoratee], _SecondDecoratee]
) -> Callable[[_SecondDecoratee], _SecondDecoratee]:
    "take a decorator and return a version of it with the same signature"
    def modified_decorator(decoratee: _SecondDecoratee) -> _SecondDecoratee:
        return decoratee
    return modified_decorator


@basic_decorator
def unstacked() -> None:
    "no decorator stack: no error"


@decorator_transformer(basic_decorator)
def stacked() -> None:
    "decorator stack: this should be the same, but it's an error"
  • What is the actual behavior/output?
t.py:26: error: Argument 1 has incompatible type "Callable[[], None]"; expected "_FirstDecoratee"
  • What is the behavior/output you expect?

No error.

  • What are the versions of mypy and Python you are using?

mypy 0.782
Python 3.8.5

  • Do you see the same issue after installing mypy from Git master?

Yes (0.790+dev.ffd9d1cdff4af3b482d4dd1f871fd1dc5b39eebb)

  • What are the mypy flags you are using? (For example --strict-optional)
warn_redundant_casts=True
warn_unused_ignores=True
strict_optional=True
strict_equality=True
no_implicit_optional=True
disallow_untyped_defs=True
disallow_any_generics=True
@glyph
Copy link
Author

glyph commented Aug 4, 2020

Is this a duplicate of #3924 ? Or the issue Jukka alluded to in #8978 (comment), "a known very old bug that generic functions when passed as arguments to another generic functions don't work"?

@glyph
Copy link
Author

glyph commented Aug 4, 2020

The workaround seems to be

def decorator_transformer(
    decorator: Callable[[_FirstDecoratee], _FirstDecoratee]
) -> Callable[[_SecondDecoratee], _SecondDecoratee]:

which sacrifices a little bit of strictness but manages to mostly maintain the desired shape

@AlexWaygood AlexWaygood added bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults labels Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults
Projects
None yet
Development

No branches or pull requests

2 participants