python / python/mypy

Bug - Type aliasing typing.Tuple is treated as if the empty Tuple is being aliased

Open
#8,715 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-alias
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

This is a bug report.

Type aliasing typing.Tuple is treated as if the empty tuple (typing.Tuple[()]) is being aliased.

Here is some sample code that converts Polar coordinates into Cartesian:

import math
import typing

Tuple = typing.Tuple


def cartesian(r: int, θ: int) -> Tuple[float, float]:
    rad = math.radians(θ)
    return r*math.cos(rad), r*math.sin(rad)


r = 12
θ = 195

x,y = cartesian(r, θ)
print(f"({r}, {θ}°) = ({x:.2f}, {y:.2f})")

$ python3 example.py 
(12, 195°) = (-11.59, -3.11)
$ mypy example.py 
example.py:7: error: Bad number of arguments for type alias, expected: 0, given: 2
Found 1 error in 1 file (checked 1 source file)

What is the actual behavior/output?

example.py:7: error: Bad number of arguments for type alias, expected: 0, given: 2
Found 1 error in 1 file (checked 1 source file)

What is the behavior/output you expect?

Success: no issues found in 1 source file

Such as when from typing import Tuple is used instead.

What are the versions of mypy and Python you are using?

I'm using mypy version 0.77 installed using pip and Python 3.7.7 instead via homebrew on macOS Catalina 10.15.4.

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)

None.

Additional details

It might be related to #8273. I know Tuple and Callable are both mentioned in PEP 484 as being different.

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 example.py reproduction and run mypy without additional flags, comparing the aliased typing.Tuple annotation with the direct import described in the report. The work is done when the aliased form type-checks successfully with the expected “Success: no issues found” output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.