python / python/mypy

Incorrect type inference with Union[Any, Any]

Open
#10,425 1 comment 0 reactions 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

Mypy infers an overly specific type for code that (for example) adds 1 to an argument of type Union[Any, Any].

To Reproduce

Run mypy against the following code:

from typing import Any, Union

def function(arg: Union[Any, Any]) -> None:
    reveal_type(1 + arg)

Expected Behavior

The revealed type is Any or Union[Any, Any].

Actual Behavior

The revealed type is builtins.int, which is incorrect (e.g. when arg is a float, or a numpy array).

Note that reveal_type(arg + 1) does return Any.

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.7.3
  • Operating system and version: Ubuntu 18.04

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

No source file or test path is named. Start by running mypy on the supplied Python snippet and compare reveal_type for 1 + arg with arg + 1, then trace the type-inference path for Union[Any, Any]. Done means a regression test captures the expected Any result and the relevant type-checker tests pass.

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
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.