KEMBAR78
mypy 1.7.0, ParamSpec: `Cannot infer type argument 1 of "function_name"` when using keyword arguments. · Issue #16485 · python/mypy · GitHub
Skip to content

mypy 1.7.0, ParamSpec: Cannot infer type argument 1 of "function_name" when using keyword arguments. #16485

@orsinium

Description

@orsinium

Bug Report

The issues with ParamSpec inference for pytypest first appeared in mypy 1.6.0, see #16301. In 1.7.0, it was fixed for some cases but not all.

This specific issue happens when a function accepts multiple classes using ParamSpec and some of them pass an argument as keyword one and some pass the same argument as a positional one.

To Reproduce

from typing import Callable, Generic
from typing_extensions import ParamSpec

P = ParamSpec('P')

class Case(Generic[P]):
    def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
        pass

def _test(a: int, b: int = 0) -> None:
    assert a

def parametrize(
    func: Callable[P, None],
    *cases: Case[P],
    **named_cases: Case[P],
) -> Callable[[], None]:
    raise NotImplementedError

# this one works:
parametrize(_test, Case(1, 2), Case(3, 4))

# this one also works:
parametrize(_test, Case(1, b=2), Case(3, b=4))

# and this is also fine:
parametrize(_test, Case(1, 2), Case(3))

# but this one fails inference:
parametrize(_test, Case(1, 2), Case(3, b=4))

Expected Behavior

It shouldn't matter how an argument is passed into ParamSpec. If it can be passed into the target function described by the ParamSpec, it should pass type checking.

Actual Behavior

For the last line of the code:

Cannot infer type argument 1 of "parametrize"  [misc]

The same happens if instead of *cases: Case[P] the function accepts case1: Case[P], case2: Case[P].

Your Environment

  • Mypy version used: 1.7.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • 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