python / python/mypy

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

Open
#9,756 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy on the issue's scratchpad.py example and confirm the contradictory Type[bytes] diagnostic. Trace the type-checking path involved in the list membership expression; done means the valid example reports no error, as described under Expected Behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.