GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `__new__` return types are not respected (reported as error and overwritten by the current class) unless the return type is a subclass of the current class · Issue #19624 · python/mypy

__new__ return types are not respected (reported as error and overwritten by the current class) unless the return type is a subclass of the current class #19624

New issue
Copy link
New issue
Copy link
Closed as duplicate
Closed as duplicate
__new__ return types are not respected (reported as error and overwritten by the current class) unless the return type is a subclass of the current class#19624
Copy link
Labels
bugmypy got something wrong

Description

@studyingegret
studyingegret
opened on Aug 9, 2025
Issue body actions

new.py:

from typing import cast

class A:
    def __new__(cls) -> int:
        return 1

class B:
    def __new__(cls) -> "C":
        return cast(C, None)

class C(B):
    def __new__(cls) -> B:
        return cast(B, None)

reveal_type(A()) # Expect int (builtin.int)
reveal_type(B()) # Expect C (new.C)
reveal_type(C()) # Expect B (new.B)

Run

mypy new.py --config-file=

(The --config-file= ignores configuration files)

Expected:

new.py:15: note: Revealed type is "builtin.int"
new.py:16: note: Revealed type is "new.C"
new.py:17: note: Revealed type is "new.B"

Actual:

new.py:4: error: Incompatible return type for "__new__" (returns "int", but must return a subtype of "A")  [misc]
new.py:12: error: Incompatible return type for "__new__" (returns "B", but must return a subtype of "C")  [misc]
new.py:15: note: Revealed type is "new.A"
new.py:16: note: Revealed type is "new.C"
new.py:17: note: Revealed type is "new.C"
Found 2 errors in 1 file (checked 1 source file)

Environment

  • mypy 1.18.0+dev (compiled: no) (186515f)
    • python -m pip install -e . -C editable_mode=strict
  • Python 3.13.2

Related issues

Same: #1020 #15182 #8330
Related: #14502 #12045 #9045

(Note: Some of these issues are noted as no longer reproducable in the comments. I didn't check their reproducability, but I am able to reproduce this one, which might mean a regression.)

Activity

  1. studyingegret
    added
    bugmypy got something wrong
    on Aug 9, 2025
  2. sterliakov commented on Aug 9, 2025

    @sterliakov
    sterliakov
    on Aug 9, 2025
    Collaborator
    More actions

    I think #15182 and #8330 are both almost exact duplicates, so I'll close this ticket accordingly to keep the tracker healthy.

  3. sterliakov
    closed this as a duplicateof Honor return type of __new__ even if not a subclass #15182 on Aug 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.