I am trying to create a class called Theme
whose __init__
function allows another object, which may be of type Theme
, to be passed in. But when I try and type hint that an object of that type is allowed, Python throws an error because Theme
is not yet defined. Here is the code, which works when I take out the type hint:
from typing import Union
class Theme:
def __init__(self, start: Union[dict, Theme, None] = None):
if start is None:
start = {}
elif isinstance(start, Theme):
start = start.dict
self.dict = start
The error I am getting is:
Traceback (most recent call last):
File "C:/Users/.../Test.py", line 4, in <module>
class Theme:
File "C:/Users/.../Test.py", line 6, in Theme
def __init__(self, start: Union[dict, Theme, None] = None):
NameError: name 'Theme' is not defined
Is there any way I can achieve this or is it just not possible and I shouldn't bother?