microsoft / microsoft/pyright

dataclass_transform as decorator for class decorator blocks type checking

Open
#10,626 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Describe the bug**
Bug occurs when `dataclass_transform` is used to decorate a function which will be used a class decorator.

Normally, one can use the class decorator to statically check that the decorated class follows a particular protocol. But when the class decorator is itself decorated by `dataclass_transform` specifically, those checks do not occur.

A dummy `identity_decorator` is used below to demonstrate that class decorator can normally be decorated itself.

**Code or Screenshots**
```python
from typing import Protocol, dataclass_transform

class FooProto(Protocol):
foo: str

def identity_decorator[T](a: T) -> T:
return a

@identity_decorator
def dataclass_like[T: FooProto](cls: type[T]) -> type[T]: ...

@dataclass_transform()
def dataclass_like_trans[T: FooProto](cls: type[T]) -> type[T]: ...

class Foo:
foo: str

class Bar:
bar: int

dataclass_like(Foo) # (expected) No error.
dataclass_like(Bar) # (expected) Error: Type "Bar" is not assignable to type "FooProto".

dataclass_like_trans(Foo) # (expected) No error.
dataclass_like_trans(Bar) # (expected) Error: Type "Bar" is not assignable to type "FooProto".

@dataclass_like
class Foo2:
foo: str

@dataclass_like # (expected) Error: Type "Bar2" is not assignable to type "FooProto".
class Bar2:
bar: int

@dataclass_like_trans
class Foo3:
foo: str

@dataclass_like_trans # (unexpected): No error.
class Bar3:
bar: int
```

**VS Code extension or command-line**
Using pyright via the VSCode PyLance extension.

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied reproducer with Pyright through the VS Code Pylance extension or the command line, focusing on how dataclass_transform changes class-decorator checking. Trace the handling of the dataclass_like_trans decorator and ensure Bar3 produces the same protocol error as Bar2; verify both direct calls and decorator usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.