Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Forward References

9 views
Skip to first unread message

Jonathan Gossage

unread,
Sep 3, 2023, 4:44:37 PM9/3/23
to
I am attempting to use forward references in my program and I am failing.
This also does not work with the older way of putting the name of a class
as a string. Here is some sample code:

from __future__ import annotations

from dataclasses import dataclass
from typing import TypeAlias


ColorDef: TypeAlias = RGB | int | str



@dataclass(frozen=True, slots=True)
class RGB(object):

Can anyone suggest how I should fix this without reversing the statement
order?

pass


--
Jonathan Gossage

MRAB

unread,
Sep 3, 2023, 7:00:08 PM9/3/23
to
The usual way to deal with forward type references is to use a string
literal, i.e. 'RGB', but that doesn't work with '|', so use typing.Union
instead:

from typing import TypeAlias, Union

ColorDef: TypeAlias = Union['RGB', int, str]

0 new messages