python / python/mypy

isinstance(x, Tuple) fails when equivalent expressions don't

Open
#6,680 11 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive priority-2-low topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

reproducer

from typing import Any, Dict, Tuple

x = (1,2)
isinstance(x, tuple)
isinstance(x, Tuple[Any, ...])
isinstance(x, Tuple)

observed behavior

$ pipenv run mypy test.py 
test.py:6: error: Argument 2 to "isinstance" has incompatible type "_SpecialForm"; expected "Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]]"

expected behavior
The python documentation states

A plain Tuple is equivalent to Tuple[Any, ...], and in turn to tuple.

If the above is true, IMO either all of the examples should raise errors, or none of them should. Only the third example is raising an error right now. I'm not quite sure what the error means in any case.

environment
master as of 2019-04-15

$ pipenv graph
mypy==0.710+dev.2c8a809693cf5be0b29d272d7f0711521bd4a55a
  - mypy-extensions [required: >=0.4.0,<0.5.0, installed: 0.4.1]
  - typed-ast [required: >=1.3.1,<1.4.0, installed: 1.3.1]

As always, thanks for all the work you do here!

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 with the provided isinstance reproducer and compare mypy's handling of tuple, Tuple[Any, ...], and plain Tuple. Trace the type-checking entry point for isinstance calls and determine the intended behavior from the linked Python documentation. Done means the three equivalent forms receive consistent, documented diagnostics, with tests covering the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.