Skip to content

Return type of class decorator is ignored #16241

Closed as not planned
Closed as not planned
@NeilGirdhar

Description

@NeilGirdhar

MyPy seems to ignore the return type of class decorators. @AlexWaygood 's suggested I create a minimal repro of this problem:

from typing import Any

class T:
    y: int

def some_decorator(cls: type[Any]) -> type[T]:
    cls.y = 1
    return cls

@some_decorator
class X:
    a: int

def f(x: X) -> None:
    print(x.y)  # No attribute!

This makes it impossible to define a dataclass transform that produces ordinary dataclasses:

import dataclasses
from dataclasses import replace
from typing import Any, ClassVar, Protocol, dataclass_transform


class DataclassInstance(Protocol):
    __dataclass_fields__: ClassVar[dict[str, dataclasses.Field[Any]]]


@dataclass_transform()
def dataclass(cls: type[Any]) -> type[DataclassInstance]:
    return dataclasses.dataclass(cls)

@dataclass
class X:
    a: int

def f(x: X) -> X:
    return replace(x, a=1)  # Error: dataclass expected

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