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

Type inference for protocol is confused with keyword-only, reordered arguments #8317

Open
Elscouta opened this issue Jan 23, 2020 · 2 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-protocols

Comments

@Elscouta
Copy link

Elscouta commented Jan 23, 2020

  • Are you reporting a bug, or opening a feature request?

I'm reporting a bug.

  • Please insert below the code you are checking with mypy,
from typing import TypeVar
from typing_extensions import Protocol

TA = TypeVar("TA", contravariant=True)

class P(Protocol[TA]):
    def __call__(self, *, a: TA, b: int) -> None: ...

def misordered(*, b: int, a: TA) -> None: ... # Should be seen as Protocol[TA]
def ordered(*, a: TA, b: int) -> None: ...      # Should be seen as Protocol[TA]
def explicit(*, b: int, a: str) -> None: ...    # Should be seen as Protocol[str]
    
    
def supports_protocol_int(a: P[int]) -> None: ... 
def supports_protocol_str(a: P[str]) -> None: ...
    
supports_protocol_int(misordered) # Works, since TA = int does not cause problem
supports_protocol_str(misordered) # BUG: Does not work. TA is determined to be int.
supports_protocol_int(ordered)    # Works.
supports_protocol_str(ordered)    # Works.
supports_protocol_int(explicit)   # Returns an error (as expected)
supports_protocol_str(explicit)   # Works.
  • What is the actual behavior/output?
main.py:18: error: Argument 1 to "supports_protocol_str" has incompatible type "Callable[[NamedArg(int, 'b'), NamedArg(TA, 'a')], None]"; expected "P[str]"
main.py:21: error: Argument 1 to "supports_protocol_int" has incompatible type "Callable[[NamedArg(int, 'b'), NamedArg(str, 'a')], None]"; expected "P[int]"
Found 2 errors in 1 file (checked 1 source file)
  • What is the behavior/output you expect?

Only the second error is correct.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

This was tested on the "real code" with mypy-0.760 and mypy-0.720. The minimal example was run on mypy-play.net, mypy latest, as of 23/01/2020.

  • What are the mypy flags you are using? (For example --strict-optional)

No special settings were done on mypy-play.net

@Elscouta Elscouta changed the title Type inference for protocol is confused when with keyword-only, reordered arguments Type inference for protocol is confused with keyword-only, reordered arguments Jan 23, 2020
@ilevkivskyi
Copy link
Member

Yeah, looks like a bug to me. Order of keyword-only arguments should not affect type inference, only their names. Should be probably not hard to fix.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Jan 25, 2020
@TH3CHARLie
Copy link
Collaborator

TH3CHARLie commented Jan 27, 2020

Does this require to modify unify_generic_callable to support unify generic by keyword?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-protocols
Projects
None yet
Development

No branches or pull requests

4 participants