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 — Mypyc ignores custom `__new__` without warning · Issue #16012 · python/mypy

Mypyc ignores custom __new__ without warning #16012

New issue
Copy link
New issue
Copy link
Closed
#19739
Closed
Mypyc ignores custom __new__ without warning#16012
#19739
Copy link
Labels
bugmypy got something wrong

Description

@nickdrozd
nickdrozd
opened on Sep 1, 2023
Issue body actions

Mypyc apprently ignores custom __new__ methods without warning. It would be great if it could abide by the custom __new__, but otherwise a warning / error should be raised.

Example code (see #1020 (comment)):

from __future__ import annotations

class Add:
    l: IntLike
    r: IntLike

    def __new__(cls, l: IntLike, r: IntLike) -> IntLike:  # type: ignore[misc]
        print(f'running __new__ with {l} and {r}')

        return (
            l if r == 0 else
            r if l == 0 else
            super().__new__(cls)
        )

    def __init__(self, l: IntLike, r: IntLike):
        self.l = l
        self.r = r

    def __repr__(self) -> str:
        return f'({self.l} + {self.r})'

    def __add__(self, other: IntLike) -> IntLike:
        return Add(self, other)

IntLike = int | Add

Code for running:

print(f'{Add(1, 5)=}')
print(f'{Add(0, 5)=}')
print(f'{Add(1, 0)=}')

Running this interpreted gives:

running __new__ with 1 and 5
Add(1, 5)=(1 + 5)
running __new__ with 0 and 5
Add(0, 5)=5
running __new__ with 1 and 0
Add(1, 0)=1

Running compiled:

Add(1, 5)=(1 + 5)
Add(0, 5)=(0 + 5)
Add(1, 0)=(1 + 0)

The interpreted and compiled behaviors are different, which is bad.

Reacted by Guy Wilson

Activity

  1. nickdrozd
    added
    bugmypy got something wrong
    on Sep 1, 2023
  2. hauntsaninja
    added a commit that references this issue on Sep 2, 2023

    Believe arbitrary __new__ return types

    This issue will close once commit 4616fa2 is merged into the 'master' branch.
    4616fa2
  3. JukkaL
    closed this as completedin #19739on Aug 29, 2025
  4. JukkaL
    added a commit that references this issue on Aug 29, 2025

    [mypyc] Use defined __new__ method in tp_new and constructor (#19739)

    This issue will close once commit c32b0a5 is merged into the 'master' branch.
    c32b0a5
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.