python / python/mypy

`UNBOUND_TYPEVAR` error message should be more direct and shorter

Open
#20,922 17 comments 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

*Notes:

The last 3 examples get the same indirect and long error messages of UNBOUND_TYPEVAR as shown below:

*Notes:

  • A parameter is a variable defined in a function header to receive an argument.
  • An argument is a value passed to a function parameter.

A function returning TypeVar should receive at least one argument containing the same TypeVar

With T for x:
def func[T](x: T) -> T:
    return x
# No error
Without T for x:
def func[T](x) -> T:
    return x
# error: A function returning TypeVar should receive at least one argument containing the same TypeVar
With no parameters:
from typing import cast

def func[T]() -> T:
    return cast(T, 'Hello')
# error: A function returning TypeVar should receive at least one argument containing the same TypeVar
With T1 for x and T2 for return type:
def func[T1, T2](x: T1) -> T2:
    return x
# error: A function returning TypeVar should receive at least one argument containing the same TypeVar

So, the same indirect and long error messages of UNBOUND_TYPEVAR should be changed:

From:

A function returning TypeVar should receive at least one argument containing the same TypeVar

To more direct and shorter one below:

A function returning TypeVar should have at least one same TypeVar parameter

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 mypy test.py examples and locate where the UNBOUND_TYPEVAR diagnostic is generated and tested. Update the message to the shorter wording requested, then verify that the three failing examples produce the new diagnostic without changing the valid example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
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.