I am just learning Python and I would like to write a module to process some data files. I am using Type Annotations and I would like to write a class (e.g. MyData) that when instantiating takes an optional argument of the type MyData. This is to compare the structure of new data to the previously loaded. However, when I run the code, I got the NameError (see below). PyCharm is highlighting "MyData" in '__init__' function and gives a message "Unresolved reference 'MyData'.
There is definitely something that I am getting wrong. Could you please enlighten me what's going on? I don't even know how to properly formulate a question to ask uncle G.
Thanks in advance for your help.
from typing import Optional
class MyData():
def __init__(self, file_path: str, reference_data: Optional[MyData] = None):
self.file_path = file_path
self.reference_data = reference_data
self.validate_data()
def validate_data(self):
# do some stuff with the data and check whether it has
# similar structure to the reference_data
pass
Traceback (most recent call last):
File "some/path/somecode.py", line 4, in <module>
class MyData():
File "some/path/somecode.py", line 5, in MyData
def __init__(self, file_path: str, reference_data: Optional[MyData] = None):
NameError: name 'MyData' is not defined