Skip to content

alpha.24 regression: Non-dataclass inheriting from a dataclass breaks generic inference #1427

@MeGaGiGaGon

Description

@MeGaGiGaGon

Summary

This is a MRE of some code that stopped working when I tried ty 0.0.1-alpha.24 playground. If you add @dataclass to ChildOfParentDataclass the error goes away.

Edit: On playing around more, this looks to be an issue with/regression in the generics inference. Making the generic explicit with return ChildOfParentDataclass[T](x) also makes the error go away.

# Gives incorrect error when using a dataclass parent:

from dataclasses import dataclass

@dataclass
class ParentDataclass[T]:
    value: T

# @dataclass  # Uncommenting this makes the error go away
class ChildOfParentDataclass[T](ParentDataclass[T]): ...

def uses_dataclass[T](x: T) -> ChildOfParentDataclass[T]:
    # Argument is incorrect: Expected `T@ChildOfParentDataclass`, found `T@uses_dataclass`
    return ChildOfParentDataclass(x)

# Works fine if the parent is a normal class

class Parent[T]:
    def __init__(self, value: T):
        self.value = value

class ChildOfParent[T](Parent[T]): ...

def uses_init[T](x: T) -> ChildOfParent[T]:
    return ChildOfParent(x)

Comparing alpha.23 to alpha.24:

PS ~>uvx ty@0.0.1-alpha.23 check issue.py
Checking ------------------------------------------------------------ 1/1 files
All checks passed!
PS ~>uvx ty@0.0.1-alpha.24 check issue.py
Checking ------------------------------------------------------------ 1/1 files
error[invalid-argument-type]: Argument is incorrect
  --> issue.py:14:35
   |
12 | def uses_dataclass[T](x: T) -> ChildOfParentDataclass[T]:
13 |     # Argument is incorrect: Expected T@ChildOfParentDataclass, found T@uses_dataclass
14 |     return ChildOfParentDataclass(x)
   |                                   ^ Expected `T@ChildOfParentDataclass`, found `T@uses_dataclass`
15 |
16 | # Works fine if the parent is a normal class
   |
info: rule `invalid-argument-type` is enabled by default

Found 1 diagnostic

Version

ty 0.0.1-alpha.24 (1fee7da 2025-10-23)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdataclassesIssues relating to dataclasses and dataclass_transformgenericsBugs 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