I define a NamedTuple as follow to get data scraped from a webpage (grades from a restaurant rating website):
class Grades(NamedTuple):
Value: Optional[int] = None
Price: Optional[int] = None
Service: Optional[int] = None
Taste: Optional[int] = None
I then assign the values by index with a loop instead of using the keys as I get the grades as a list of integers with the same order (Value, Price, Service, Taste).
scraped_grades = Grades()
for i, subject in enumerate(data):
scraped_grades[i] = data
Yet when running mypy I get the following error:
Unsupported target for indexed assignment
I was under the impression that numerical indexes were a valid way to access or assign data in a NamedTuple
. Thinking that this may simply not be supported by mypy, I tried to assign the values using keys instead, which may be more orthodox:
for i, key in enumerate(scraped_grades ._fields):
scraped_grades[key] = data[i]
But this gives me the exact same error.
Does mypy not support NamedTuples at all? I know I could also just #type: ignore
for this specific line, but I'd prefer a proper solution if one exists (or at least an explanation of why this fails).