Skip to content

Allow Final attributes in __post_init__ #3247

@niksabaldun

Description

@niksabaldun

Summary

from dataclasses import dataclass
from typing import Final

@dataclass
class Test:
    def __post_init__(self):
        self.test_int: Final[int] = 0

Ty complains: "Cannot assign to final attribute test_int on type Self@__post_init__: Final attributes can only be assigned in the class body or __init__".

As __init__ is typically not used directly in dataclasses, and body is meant to hold dynamic data, I believe that __post_init__ is a legitimate place for Final attributes.

Version

0.0.28

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions