Skip to content

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

Closed

Description

@ixje

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

  • Mypy version used: 0.790
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.8.3
  • Operating system and version: OSX 10.15.7

Activity

  1. added
    bugmypy got something wrong
    on Nov 25, 2020
  2. hauntsaninja commented on Nov 25, 2020

    @hauntsaninja
    Collaborator

    Here's a shorter repro:

    [bytes, bytearray]
    

    See python/typing#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).

  3. JelleZijlstra commented on Nov 25, 2020

    @JelleZijlstra
    Member

    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].

  4. EugeneN commented on Oct 28, 2022

    @EugeneN

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

  5. EugeneN commented on Oct 28, 2022

    @EugeneN

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

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

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

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions