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

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

Closed
orsinium opened this issue Nov 14, 2023 · 7 comments · Fixed by #16502
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@orsinium
Copy link

orsinium commented Nov 14, 2023

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
@orsinium orsinium added the bug mypy got something wrong label Nov 14, 2023
@orsinium orsinium changed the title mypy 1.7.0, ParamSpec: Cannot infer type argument 1 of "function_name" when using keyword arguments. mypy 1.7.0, ParamSpec: Cannot infer type argument 1 of "function_name" when using keyword arguments. Nov 14, 2023
@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Nov 14, 2023
JukkaL pushed a commit that referenced this issue Nov 22, 2023
Fixes #16485

My initial implementation of imprecise constraints fallback was really
fragile and ad-hoc, and I now see several edge case scenarios where we
may end up using imprecise constraints for a `ParamSpec` while some
precise ones are available. So I re-organized it: now we just infer
everything as normally, and filter out imprecise (if needed) at the very
end, when we have the full picture. I also fix an accidental omission in
`expand_type()`.
@q0w
Copy link

q0w commented Nov 24, 2023

@ilevkivskyi @orsinium
mypy 1.7.1
Its still failing

parametrize(_test, Case(1, 2), Case(3, b=4)) # error: Cannot infer type argument 1 of "parametrize"  [misc]
parametrize(_test, Case("1", "2")) # error: Cannot infer type argument 1 of "parametrize"

@orsinium
Copy link
Author

Confirming, the issue is still here on 1.7.1. @ilevkivskyi could you re-open the issue, please? I can't re-open it, I can only make a new one.

image

@q0w
Copy link

q0w commented Nov 27, 2023

@orsinium could you please open a new one?

@ilevkivskyi
Copy link
Member

Open a new one and get a ban for spamming. Can you make at least some effort, or you can only complain?

@q0w
Copy link

q0w commented Nov 27, 2023

What effort do you want? Contribute to fix a bug?

@AlexWaygood
Copy link
Member

@q0w, @orsinium: the commit that fixed this issue (3e6b552) was not included in mypy 1.7.1. It will be included in mypy 1.8.

@q0w
Copy link

q0w commented Nov 27, 2023

Sorry for this noise.

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.

4 participants