microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.67k stars 770 forks source link

dataclass_transform: Fields without default values cannot appear after fields with default values #5999

Closed powellnorma closed 2 weeks ago

powellnorma commented 2 weeks ago

Environment data

Code Snippet

from dataclasses import dataclass, field
from typing import dataclass_transform

@dataclass_transform()
def my_dataclass(c):
    return dataclass(c)

@my_dataclass
class MyClass:
    a: int
    b: int = field(compare=False)
    c: int

Expected behavior

It should not say Fields without default values cannot appear after fields with default values for the field c. When using @dataclass instead of @my_dataclass, it works as expected.

erictraut commented 2 weeks ago

Pyright is working correctly here, so this isn't a bug.

When you use dataclass_transform, you must specify which functions and classes should be treated as fields specifiers. You have not specified this argument, so dataclasses.field is not considered a field specifier.

If you change your code to the following, it works:

@dataclass_transform(field_specifiers=(field,))
def my_dataclass(c):
    return dataclass(c)