python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.51k stars 2.83k forks source link

Confusing error: incompatible type "Type[bytes]"; expected "Type[bytes]" #9756

Open ixje opened 3 years ago

ixje commented 3 years ago

Bug Report

Error message seems to indicate it requires the type it is getting. I could be reading the error message wrong, but this one confuses me.

To Reproduce

  1. put the code in a file e.g. scratchpad.py

    from typing import Type
    
    def f(class_type: Type[object]):
       if class_type in [bytes, bytearray]:
           return True
  2. run mypy scratchpad.py

Expected Behavior no errors

Actual Behavior

scratchpad.py:4: error: List item 0 has incompatible type "Type[bytes]"; expected "Type[bytes]"

Your Environment

hauntsaninja commented 3 years ago

Here's a shorter repro:

[bytes, bytearray]

See https://github.com/python/typing/issues/552 for some more context.

A good workaround here is to use a tuple instead of a list (since tuples are heterogeneous, mypy won't need to perform a join).

JelleZijlstra commented 3 years ago

There's still something strange going on here that the linked issue doesn't fully explain, though surely the implicit promotion has something to do with it. The error doesn't repro with [float, int] or [int, float], even though these are also implicitly promoted, or with [bytearray, bytes].

EugeneN commented 2 years ago

the error reproduces with arbitrary types, in function arguments, assigning etc

EugeneN commented 2 years ago

Argument 3 to "..." has incompatible type "List[PlatformFees]"; expected "List[PlatformFees]" mypy(error)

Argument 2 to "..." has incompatible type "CIPSolution"; expected "CIPSolution" mypy(error)