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

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

Closed
bzoracler opened this issue May 28, 2023 · 2 comments · Fixed by #15905
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@bzoracler
Copy link
Contributor

bzoracler commented May 28, 2023

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
@bzoracler bzoracler added the bug mypy got something wrong label May 28, 2023
@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label May 28, 2023
ilevkivskyi added a commit that referenced this issue Aug 19, 2023
Fixes #14761
Fixes #15318
Fixes #14656
Fixes #13518

I noticed there is a bunch of inconsistencies in `semanal`/`typeanal`
for ParamSpecs, so I decided do a small cleanup. Using this opportunity
I also allow `Concatenate[int, ...]` (with literal Ellipsis), and reduce
verbosity of some errors.

cc @A5rocks
@hauntsaninja
Copy link
Collaborator

@bzoracler is the project you were running into this on open source?

@bzoracler
Copy link
Contributor Author

Unfortunately not, I ran into this in my own code.

I tend to minimise the number of import statements, so I think my code sees a higher frequency of module imports and attribute accesses directly through modules than the average open-source code that would appear on something like mypy-primer.

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-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants