Oct-07-2022, 02:57 PM
If you are actively using a type checker the following code would flag an error
Python version 3.10 added slots as a parameter to dataclass, The following can now be done
from dataclasses import dataclass from typing import Optional @dataclass class Node: value: float next: Optional["Node"] = None prev: Optional["Node"] = None node = Node(0.1) node.string = "Hello"For instance in vscode with the type checker mode set to at least basic, pylance would put a red line under string and hovering over it would show the following:
Error:string: Literal['Hello']
Cannot assign member "string" for type "Node"
Member "string" is unknown
Python version 3.10 added slots as a parameter to dataclass, The following can now be done
from dataclasses import dataclass from typing import Optional @dataclass(slots=True) class Node: value: float next: Optional["Node"] = None prev: Optional["Node"] = None node = Node(0.1) node.string = "Hello"this will give the same
AttributeError: 'Node' object has no attribute 'string'
error