python / python/mypy

Mypy seems to ignore explicitly given type parameter when a overload containing Never matches

Open
#16,424 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

When trying to create an object from a generic class and having overloads, one of which is parameterized with Never, initializing the class with the signature compatible with the overload for Never seems to result in a type parameterized by Never, regardless of the type being specified explicitly (k in the playground). Typing the variable directly seems to resolve this issue (w in the playground).
Pyright seems to give the same expected error in both instances.
I am sorry if this explanation is a bit long-winded, I don't really know how to explain it more succinctly.

To Reproduce

https://mypy-play.net/?mypy=master&python=3.12&gist=9ca64c2cde9f88a1000b10217cd5b2bd&flags=strict

Expected Behavior

Both the w and the k case from the playground should have the same error.

Actual Behavior

Only the w case actually gives an error, and the k case passes without issue, i.e. Test[int]() results in a variable of type Test[Never] for some reason, rather than mypy complaining that the signature is not right.

main.py:25: error: No overload variant of "Test" matches argument type "int"  [call-overload]
main.py:25: note: Possible overload variants:
main.py:25: note:     def __init__(self, t: None = ...) -> Test[NoReturn]
main.py:25: note:     def __init__(self, t: str) -> Test[str]
main.py:27: error: Incompatible types in assignment (expression has type "Test[NoReturn]", variable has type "Test[int]")  [assignment]
main.py:30: note: Revealed type is "__main__.Test[builtins.int]"
main.py:31: note: Revealed type is "__main__.Test[Never]"
main.py:32: note: Revealed type is "__main__.Test[Never]"
main.py:33: note: Revealed type is "__main__.Test[builtins.int]"
main.py:34: note: Revealed type is "Any"
main.py:36: note: Revealed type is "__main__.Test[builtins.int]"
main.py:37: note: Revealed type is "__main__.Test[Never]"
Found 2 errors in 1 file (checked 1 source file)

Your Environment

I don't believe any of this matters in this instance because the same issue happens on the playground as well.

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 linked mypy playground and reproduce the example in main.py under strict mode with Python 3.12. Trace how overload resolution handles the explicitly supplied Test[int] when the overload parameterized by Never matches. Done means the w and k cases produce the same error, as described in Expected Behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.