python / python/mypy

Same variable with a union type is not checked in "lockstep" within an expression

Open
#6,478 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive feature priority-2-low topic-union-types
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Test env:

  • mypy 0.670
  • Python 3.7.2

Test code

from typing import List, Union


def f(x: Union[int, List[int]]) -> None:  # `TypeVar` can fix it
    x + x

Run mypy with default option, will get

a.py:5: error: Unsupported operand types for + ("int" and "List[int]")
a.py:5: error: Unsupported operand types for + ("List[int]" and "int")
a.py:5: note: Both left and right operands are unions

But actually, both left and right operands are same variable. Only int + int or List[int] + List[int] here. It seems like mypy does not consider object identity. And I'm not sure this is a bug, because above code is meaningless.

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 the supplied Python 3.7 example with mypy's default options and compare the reported union combinations with the issue's expected behavior. Read the type-checking path for repeated operands in an expression, then establish whether object identity should affect the diagnostic and add regression coverage if that behavior is accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.