Skip to content

Mypy does not infer that a dataclass field is optional when None is assigned #13934

Closed as not planned
@Kentzo

Description

@Kentzo

Bug Report

It's documented that a dataclass field can be declared as optional without Optional or | None by simply assigning None as a default value.

However, mypy fails to infer this.

To Reproduce

import dataclasses as dc


@dc.dataclass
class C:
    j: int = None

Expected Behavior

No errors

Actual Behavior

test.py:6: error: Incompatible types in assignment (expression has type "None", variable has type "int")

Your Environment

  • Mypy version used: 0.982
  • Python version used: 3.10.7

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