Another problem brought up by Joseph Perez on python-dev:
from dataclasses import dataclass
@dataclass
class User:
name: str
friends: list[User]
This will break because the implementation of the dataclass decorator will access the class's __annotations__, but at that point User is not defined yet, so the user will get a NameError.
This is a similar problem to #1, and possible solutions are similar:
- Tell users to hand-stringify their annotation and write
list["User"].
- Hack the language so that instead of throwing a NameError, we do something more useful here.
Another problem brought up by Joseph Perez on python-dev:
This will break because the implementation of the
dataclassdecorator will access the class's__annotations__, but at that pointUseris not defined yet, so the user will get a NameError.This is a similar problem to #1, and possible solutions are similar:
list["User"].