Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Various false positives with error: Unexpected "..." when using ellipsis to fill in ParamSpec-parametrised types #15318

Closed
@bzoracler

Description

@bzoracler

Bug Report & To Reproduce

False-positive-emitting behaviour of a type-alias of typing.Callable[P, R] vs a callback Protocol[P, R] is inconsistent when

  • Comparing with each other;
  • In the context of a stand-alone expression vs being in a typing.TypeVar(bound=...) expression;
  • When being accessed from a module as opposed to a straight import.

Source module

# my_module.py

import typing as t

P = t.ParamSpec("P")
R = t.TypeVar("R", covariant=True)
CallableAlias = t.Callable[P, R]

class CallableProtocol(t.Protocol[P, R]):
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
        ...


t.Callable[..., t.Any]  # OK
CallableAlias[..., t.Any]  # mypy: Type application is only supported for generic classes [misc] \
                           # mypy: Unexpected "..." [misc]
CallableProtocol[..., t.Any]  # OK

C0T = t.TypeVar("C0T", bound=t.Callable[..., t.Any])
C1T = t.TypeVar("C1T", bound=CallableAlias[..., t.Any])  # mypy: Unexpected "..." [misc]
C2T = t.TypeVar("C2T", bound=CallableProtocol[..., t.Any])

# All OK
c0: t.Callable[..., t.Any]
c1: CallableAlias[..., t.Any]
c2: CallableProtocol[..., t.Any]

Module importing from source module

import typing as t
import my_module as m
from my_module import CallableAlias, CallableProtocol

t.Callable[..., t.Any]  # OK
m.CallableAlias[..., t.Any]  # mypy: Type application is only supported for generic classes [misc] \
                             # mypy: Unexpected "..."
m.CallableProtocol[..., t.Any]  # mypy: Unexpected "..." [misc]
CallableAlias[..., t.Any]  # mypy: Type application is only supported for generic classes [misc] \
                           # mypy: Unexpected "..."
CallableProtocol[..., t.Any]  # OK

C0T = t.TypeVar("C0T", bound=t.Callable[..., t.Any])  # OK
C1T = t.TypeVar("C1T", bound=m.CallableAlias[..., t.Any])  # mypy: Unexpected "..."
C2T = t.TypeVar("C2T", bound=m.CallableProtocol[..., t.Any])  # mypy: Unexpected "..."
C3T = t.TypeVar("C3T", bound=CallableAlias[..., t.Any])  # mypy: Unexpected "..."
C4T = t.TypeVar("C4T", bound=CallableProtocol[..., t.Any])  # OK

# All OK
c0: t.Callable[..., t.Any]
c1: t.CallableAlias[..., t.Any]
c2: t.CallableProtocol[..., t.Any]
c3: CallableAlias[..., t.Any]
c4: CallableProtocol[..., t.Any]

Expected Behavior
No errors emitted by mypy

Your Environment

  • Mypy version used: v1.3.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
    [mypy]
    strict = True
    warn_unreachable = True
    enable_error_code = redundant-expr, truthy-bool, ignore-without-code, unused-awaitable
    disallow_untyped_calls = False
    warn_unused_configs = True
  • Python version used: 3.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-paramspecPEP 612, ParamSpec, Concatenate

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions