python / python/mypy

Weird formatting of __new__ signature in error messages

Open
#5,498 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

priority-1-normal topic-error-reporting topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

from typing import Tuple, Dict, Any


class MyMeta(type):
    def __new__(cls, name: str, bases: Tuple[type, ...], dict: Dict[str, Any]) -> type:
        return type.__new__(name, bases, dict)

This MyMeta.__new__ has exactly same signature with type.__new__ in typeshed:
https://github.com/python/typeshed/blob/master/stdlib/3/builtins.pyi#L88
But mypy thinks that it's incorrect and offers me to write two same cls args:

test.py:6: note: Possible overload variants:
test.py:6: note:     def __new__(cls, cls: Any, o: object) -> type
test.py:6: note:     def __new__(cls, cls: Any, name: str, bases: Tuple[type, ...], namespace: Dict[str, Any]) -> type

No mypy flags was used, mypy --version is 0.620

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

Reproduce the example in test.py with mypy 0.620, then compare the reported new overloads with typeshed's stdlib/3/builtins.pyi around line 88. Trace how mypy formats the signature for type.new; done means the diagnostic no longer presents the misleading duplicate cls arguments while preserving the relevant overload information.

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.