Skip to content

Type erased with TypeAlias on higher ordered functions #16530

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
tyoung-patreon opened this issue Nov 20, 2023 · 1 comment
Closed

Type erased with TypeAlias on higher ordered functions #16530

tyoung-patreon opened this issue Nov 20, 2023 · 1 comment
Labels
bug mypy got something wrong

Comments

@tyoung-patreon
Copy link

Bug Report

Mypy appears to lose type information simply by using a TypeAlias with generics. I'm trying to define a re-usable type for Decorator throughout my codebase so that I don't have to recreate Callable[[Callable[P, A]], Callable[P, A]] and can instead just use Decorator[P, A]. However, when I do this the type is silently replaced with Never.

To Reproduce

from typing import Callable
from typing import TypeVar
from typing_extensions import ParamSpec
from typing_extensions import TypeAlias

A = TypeVar("A")
P = ParamSpec("P")

Decorator: TypeAlias = Callable[[Callable[P, A]], Callable[P, A]]


def my_decorator(f: Callable[P, A]) -> Callable[P, A]:
    return f


def my_decorator_factory() -> Decorator[P, A]:
    return my_decorator


@my_decorator_factory()
def x(i: int) -> int:
    return i


reveal_type(my_decorator)
reveal_type(my_decorator_factory)
reveal_type(my_decorator_factory())
reveal_type(x)

# Revealed type is "def [P, A] (f: def (*P.args, **P.kwargs) -> A`-2) -> def (*P.args, **P.kwargs) -> A`-2"
# Revealed type is "def [P, A] () -> def (def (*P.args, **P.kwargs) -> A`-2) -> def (*P.args, **P.kwargs) -> A`-2"
# Revealed type is "def (def (*Never, **Never) -> Never) -> def (*Never, **Never) -> Never"
# Revealed type is "def (*Never, **Never) -> Never"

Expected Behavior

I expect mypy to to reveal that x has a type of Revealed type is "def (i: builtins.int) -> builtins.int". This is what I get when I do not use the TypeAlias for Decorator

Actual Behavior

Instead I get that Revealed type is "def (*Never, **Never) -> Never".

Your Environment

  • Mypy version used: 1.7.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9
@tyoung-patreon tyoung-patreon added the bug mypy got something wrong label Nov 20, 2023
@ilevkivskyi
Copy link
Member

Duplicate of #3924

@ilevkivskyi ilevkivskyi marked this as a duplicate of #3924 Nov 22, 2023
@ilevkivskyi ilevkivskyi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 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

2 participants