Skip to content

Incompatible types in assignment Tuple[bool, bool, bool, int] #6805

Description

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

  • Please insert below the code you are checking with mypy...

from typing import Tuple 

_behaviors = dict(headless=False, verbose=False,
                 dryrun=False, timeout=30)

print(type(_behaviors['headless']))  # bool
print(type(_behaviors['verbose']))  # bool
print(type(_behaviors['dryrun']))  # bool
print(type(_behaviors['timeout']))  # int

options: Tuple[bool, bool, bool, int] = (
        _behaviors['headless'], _behaviors['verbose'],
        _behaviors['dryrun'], _behaviors['timeout'])
  • What is the actual behavior/output?

sample.py:11: error: Incompatible types in assignment (expression has type "Tuple[int, int, int, int]", variable has type "Tuple[bool, bool, bool, int]")

  • What is the behavior/output you expect?
    I wouldn't expect mypy to see 'options' variable as Tuple[int, int, int, int], but rather Tuple[bool, bool, bool, int]

  • What are the versions of mypy and Python you are using?
    Python 3.7.1
    mypy 0.701

  • Do you see the same issue after installing mypy from Git master?
    yes

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

Screenshot attached for some visuals. (The small script above is a simplified repro that still causes the error.)

mypy-possible-bug

Activity

  1. JelleZijlstra commented on May 8, 2019

    @JelleZijlstra
    Member

    This is because mypy infers the type of _behaviors as Dict[str, int] (bool is a subclass of int). This is similar to #3816.

    In your case, you should probably be using a TypedDict or explicitly annotate _behaviors as Dict[str, Any].

  2. daevski commented on May 8, 2019

    @daevski
    Author

    Thank you. I knew I was missing something; it was too simple.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions