KEMBAR78
Fix bug in constraints solver regarding `ParamSpec` upper bounds by AlexWaygood · Pull Request #12938 · python/mypy · GitHub
Skip to content

Conversation

AlexWaygood
Copy link
Member

Description

Fixes #12930.

Currently, mypy erroneously emits two errors on this snippet:

from typing_extensions import ParamSpec
from typing import Callable, TypeVar, Iterator, Iterable, Tuple

P = ParamSpec("P")
T = TypeVar("T")

def enumerate(x: Iterable[T]) -> Iterator[Tuple[int, T]]: ...

def foo(func: Callable[P, T]) -> Callable[P, T]:
    def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
        y = enumerate(args)  # E: Need type annotation for "y"  # E: Argument 1 to "enumerate" has incompatible type "P.args"; expected "Iterable[<nothing>]"
        return func(*args, **kwargs)
    return _inner

This is because of a bug in constraints.py: ParamSpecArgs and ParamSpecKwargs have upper bounds following #12668 (tuple[object, ...] and dict[str, object] respectively), but the constraints solver isn't currently aware of that fact. This PR fixes that bug.

Test Plan

I added a test to one of the existing test cases.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 4, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 8153e47 into python:master Jun 4, 2022
@AlexWaygood AlexWaygood deleted the enumerate branch June 4, 2022 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ParamspecArgs cannot be used with enumerate

2 participants