python / python/mypy

Recognise the ``@object.__new__`` decorator on a class definition

Open
#12,265 0 comments 1 reaction 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

For the following code:

@object.__new__
class A:
    ...

class B:
    ...

reveal_type(A)
reveal_type(B)

mypy returns:

test.py:8: note: Revealed type is "def () -> test.A"
test.py:9: note: Revealed type is "def () -> test.B"

If you print instead of reveal_type it shows that:

<__main__.A object at 0x1081cfb50>
<class '__main__.B'>

Following screenshot shows that ``pyright` does recognise this.

Schermafbeelding 2022-03-01 om 10 52 15

Expected Behavior

Recognise that the @object.__new__ decorator turns A into an instance of A and set that as its type.

Actual Behavior

A is now inferred as Type[A].

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10
  • Operating system and version: MacOS

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 by reproducing the example in test.py with mypy and compare the reveal_type results with the expected runtime behavior. Trace the existing handling of decorators applied to class definitions; done means @object.new makes A infer as an instance of A while B remains a class.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.