Skip to content

respect return type of __new__ #281

@nickdrozd

Description

@nickdrozd

Summary

This code passes Ty without any warnings. But when run it will fail half the time.

from __future__ import annotations

from random import randint
from typing import TYPE_CHECKING

class A:
    def __new__(cls) -> int | A:
        if randint(0, 1):
            return object.__new__(cls)
        else:
            return randint(0, 100)

def f(x: A) -> None:
    assert isinstance(x, A)

a = A()

f(a)

f expects value of type A. But A.__new__ might return int. So constructor of A should not be assumed to always return A. Ty should flag call f(a).

Version

ty 0.0.0-alpha.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcallsIssues relating to call-signature checking and diagnostics

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions