Skip to content

Initialization of generic classes with type parameter default values is broken #1156

@sharkdp

Description

@sharkdp

Summary

I identified another problem that "blocks" the merge of astral-sh/ruff#18007 (in the sense that there are hundreds of false positives caused by this). It can be reproduced on main by explicitly annotating self: Self, and occurs when trying to initialize a generic class with a type parameter default value:

from typing import Self

class MyClass[T = int]:
    def __init__(self: Self, x: T) -> None:
        self.x = x

# error: Argument to bound method `__init__` is incorrect: Argument type `MyClass[T@MyClass]` does not satisfy upper bound of type variable `Self`
MyClass(1)

https://play.ty.dev/adc72fc1-667d-4956-aa06-ed72e1403e1f

Version

Current main (25853e237)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggenericsBugs or features relating to ty's generics implementation

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions