python / python/mypy

Add lint against empty Union

Open
#9,998 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

From the documentation it indicates that Union must contain at least one item, however this doesn't seem to be implemented in mypy (and in fact, mypy seems to treat an empty union as NoReturn) -- the treating it as NoReturn is actually kinda cool, but probably not what was intended (?)

my twitch chat is telling me that pyright also considers Union[()] as NoReturn, maybe this just needs cpython documentation

To Reproduce

from typing import Union

def f() -> Union:
    print('hello hello')

def g() -> Union[()]:
    print('hello hello')
$ mypy --version
mypy 0.800
$ mypy t.py
t.py:3: error: Implicit return in function which does not return
t.py:6: error: Implicit return in function which does not return
Found 2 errors in 1 file (checked 1 source file)

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 Union and Union[()] reproducer in the issue and inspect how mypy reports the implicit returns; no source file or test is identified. First determine whether the intended fix is a mypy diagnostic or a CPython documentation change, then verify that the chosen outcome distinguishes an empty Union from the current NoReturn behavior.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.