I have a dataclass called ReportParts. I wrote an add() function that mashes another ReportParts into the the first ReportParts. It works perfectly.
@dataclass
class ReportParts():
headers: []
columns: [[]]
def add(self, newParts):
self.headers.extend(newParts.headers)
self.columns.extend(newParts.columns)
petNames = reportParts(['cat','dog'], [['Gato','Sabrina','Meow'],['Spot','Cerberus','Argos']])
bunnyNames = reportParts(['bunny'], [['Hopper','Flopsy','Snowball']])
petNames.add(bunnyNames)
#Pretty Print
for animal, names in zip(petNames.headers, petNames.columns):
print (animal + ': ' + str(names))
>>>cat: ['Gato', 'Sabrina', 'Meow']
>>>dog: ['Spot', 'Cerberus', 'Argos']
>>>bunny: ['Hopper', 'Flopsy', 'Snowball']
Why is it that if I specify that the parameter for add() is another ReportParts, Python says it doesn't know what a ReportParts is?
@dataclass
class ReportParts():
headers: []
columns: [[]]
def add(self, newParts: ReportParts): #<--- newParts is now specified as a ReportParts
self.headers.extend(newParts.headers)
self.columns.extend(newParts.columns)
>>>Exception has occurred: NameError
>>>name 'reportParts' is not defined
>>> File "/Users/test/GitHub/WildSort/Sandbox.py", line 11, in reportParts
>>> def add(self, newParts:reportParts):
^^^^^^^^^^^
Also, more subjective I know, but is there a good way around this? Should I be checking for type(newParts) != ReportParts
and raising a TypeError? Should I just leave it be?