Skip to content

Incompatible types for fixed-length list/tuple creation #10606

Closed
@headtr1ck

Description

@headtr1ck

Bug Report

When creating lists or tuples of a fixed (initial) length using multiplication with a constant (i.e. 3*[1]) mypy evaluates the type as "Tuple[XXX, ...]", even though the length of the list/tuple is known.
This leads to incompatibilities when assigning values to existing variables that were constructed "classically" (i.e. [1, 1, 1]).

I don't know if this is intentional or actually a bug.
If the type will be changed to "Tuple[XXX, XXX, XXX]" this could lead to long expressions for long lists (i.e. 1e4*[1]).
Is it possible to remove the incompatibility or will this lead to other inconsistencies?

To Reproduce

Here is a minimal example on how to reproduce the problem:

var = (1, 2, 3)
var = 3 * (1,)

I know, that this is quite useless code, but a more often encountered code example with the same problem would be:

if cond:
  var = (1, 2, 3)
else:
  var = 3 * (1,)

Expected Behavior

Both expressions should evaluate to Tuple[int, int, int] OR be compatible.

Actual Behavior

This raises:
Incompatible types in assignment (expression has type "Tuple[int, ...]", variable has type "Tuple[int, int, int]")

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: /
  • Mypy configuration options from mypy.ini (and other config files): /
  • Python version used: 3.9.1
  • Operating system and version: redhat enterprise linux 7

Comment

I have no idea what the operation 5*(1,) is called, so I could not find anything about this problem. Sorry, if this was asked/resolved before.

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