Skip to content

Instantly share code, notes, and snippets.

@mitsuse
Created August 24, 2020 03:11
Show Gist options
  • Save mitsuse/c3ba46d6aaebe388d2c9eb92cf9a07d1 to your computer and use it in GitHub Desktop.
Save mitsuse/c3ba46d6aaebe388d2c9eb92cf9a07d1 to your computer and use it in GitHub Desktop.
`typing_extensions.Final` cannot be used for dataclasses.
from __future__ import annotations
from typing_extensions import Final
from dataclasses import dataclass
@dataclass
class A:
x: Final[int] = 100
# x: Final[int] # error: Final name must be initialized with a value
class B:
x: Final[int]
def __init__(self, x: int) -> None:
self.x = x
a = A(200)
# error: Cannot assign to final attribute "x"
# a.x = 20
print(a) # A(x=200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment