Skip to content

Concatenate false positive (no problem with pyright) #14168

Closed

Description

@supersergiy

Bug Report

Concatenate causes a false positive in the following example:

To Reproduce
Playground Link

from typing import Protocol
from typing_extensions import ParamSpec, Concatenate

P = ParamSpec("P")

class A(Protocol[P]):
    def foo(self, a: int, *args: P.args, **kwargs: P.kwargs):
        ...
        
class B(Protocol[P]):
    def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs):
        ...
    
def bar(b: B[P]) -> A[Concatenate[int, P]]:
    return b # error

Expected Behavior

No type error expected, which works with pyright (pyright playground)

Actual Behavior

# main.py:15: error: Incompatible return value type (got "B[P]", expected "A[[int, **P]]")  [return-value]
# main.py:15: note: Following member(s) of "B[P]" have conflicts:
# main.py:15: note:     Expected:
# main.py:15: note:         def foo(self, a: int, *args: [int, **P.args], **kwargs: [int, **P.kwargs]) -> Any
# main.py:15: note:     Got:
# main.py:15: note:         def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs) -> Any
# Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991

Activity

  1. added
    bug
    on Nov 23, 2022
  2. added
    topic-paramspec
    on Nov 23, 2022
  3. ilevkivskyi commented on Aug 16, 2023

    @ilevkivskyi
    Member

    On master mypy emits a better error now:

    error: Incompatible return value type (got "B[P]", expected "A[[int, **P]]")  [return-value]
    note: Following member(s) of "B[P]" have conflicts:
    note:     Expected:
    note:         def foo(a: int, int, /, *args: P.args, **kwargs: P.kwargs) -> Any
    note:     Got:
    note:         def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs) -> Any
    

    I think this should be allowed, function with named argument should be fine where a function with positional-only argument is expected (unless I am missing something). And this error also hints a workaround: making a and b arguments in both foo positional-only (e.g. using __a and __b argument names) makes the error go away.

  4. ilevkivskyi commented on Aug 16, 2023

    @ilevkivskyi
    Member

    OK, after reading the code, it looks like there is some unsafety here. Consider for example A[Concatenate[int, ...]] (btw I think we don't support this syntax yet) or similar, then the signature of A.foo is

    def foo(self, a: int, int, *args: Any, **kwargs: Any)

    and foo(1, 2, b="what?") is a valid call for this signature (because second argument is positional-only, so b will map to **kwargs), while it is obviously not valid for B. This is a super rare corner-case however, and funny part is that this check should be only enabled in --strict mode, but it was accidentally always on in few places.

  5. hauntsaninja commented on Aug 19, 2023

    @hauntsaninja
    Collaborator

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

  6. supersergiy commented on Aug 20, 2023

    @supersergiy
    Author

    @hauntsaninja yup it is, why?

  7. hauntsaninja commented on Aug 20, 2023

    @hauntsaninja
    Collaborator

    Where can I find it — is it https://github.com/ZettaAI/zetta_utils? I'd like to add it to https://github.com/hauntsaninja/mypy_primer . This will let us detect regressions or measure future improvements on your code. I think we could use more coverage of real world ParamSpec usage.

  8. supersergiy commented on Aug 20, 2023

    @supersergiy
    Author

    Yup, that's the one. Happy if it helps

  9. hauntsaninja commented on Aug 24, 2023

    @hauntsaninja
    Collaborator

    It has already helped :-) #15953 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic-paramspec

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions