Skip to content

bad-class-definition due to Class Method Assignments when using dataclass_transform() #3259

@QEDady

Description

@QEDady

Describe the Bug

Pyrefly incorrectly picks up fields from any class method assignments (e.g., inside @classmethod or __init_subclass__). At runtime, these assignments are ignored by Python's dataclasses.dataclass mechanism.

This leads to a false positive bad-class-definition error when a subclass defines a field without a default value.

Other static type checkers (Pytype and MyPy) are OK with the above and do not emit any errors.

Reproduction Example

import typing
@typing.dataclass_transform()
class ModuleBase:
  ...

class Module(ModuleBase):
  @classmethod
  def foo(cls) -> None:
    cls.field: typing.Any = None

class SmallModule(Module):
  x: int

Pyrefly reports:

ERROR sandbox.py:21:3-4: Dataclass field `x` without a default may not follow dataclass field with a default [[bad-class-definition](https://pyrefly.org/en/docs/error-kinds/#bad-class-definition)]

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxAAQDqMUAxrgLYzUAuunAFuwAqkATjDBRS1AMoYcBADS8IcakuqpqxVAHMYAHXQ0A7nxHVSuAK7VGGaoaEQOMBXB6pTM6rHfo1eCxycpMQQ6FoqvoIcPLjohPqUCdQAEjAeHtaWxLDUYDCoHBYiyrhgispoWHj4iEkAtNQA4jCBIqhQdRwQbEHE7Ix8jADWoeF5MDiow-XUAKpw7CIAbvlQAPocwTAAFACUnNyhcH2MgaF5QiKYvfAzMPh9DmzogYaOPLzsAJIAIgCiuXyhWKvAKdnYcAsxGIuCEgW2MEIWkIagCuGYLGyLWc1C0uE6%2BMwolCjggsQUMRWQgULUYuxmtAcTjUWGoQgskVI0VimSJ1EwEBEp3EETMliE1GwQlwhgWQn03RhcN6o30%2BgAApsQmFCJgClMoKg4HANkIMHAwLCWHt9IxDcbqABZXCYCywABCRpgtV81EI-rV6DtRuUztdsG2YbdME9C12Puo1HVweNbG5mH0iaJZUtuG2drg%2BzqAD5qAA5WLezOJ6xQOCESAMTCIFU6gCC6AkAF5y5XAynlJIWO0oFGI2OYPHqzUIhx9CA5CAyCIxKRCFwWFAKDRBCuRZVZPhMuhIFoigUyXEkpIYOweBwOMQ4IgAPQv5eicSEWFaF8wdAvpg6JwC%2BzAnhAZ5ml0sQvrksJqEsqDQKg2A5GBp7ntBvi4MQWH1voZDcugdRUnAl7UD2uggAAzIQACMABMVH6AA2mk0pCHAAC6%2BgcoqsJOJgdQCkKXQrBR1AAOQQegsIwJJ%2BiyRwdQiAAjhYgoTHUQwwKQdRTIw8DKD2kmGD4CnoCAAC%2Bi5TGJMAAGLQDAFAHtUJDkFZQA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions