You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
csv.DictReader and DictWriter not subscriptable at runtime #92391
Add PEP 585 style __class_getitem__ to csv.DictReader and csv.DictWriter.
Pitch
With PEP 585 most generic stdlib types are now subscriptable at runtime. Both DictReader and DictWriter are generic in typeshed but don't yet implement __class_getitem__.
The classes are slightly complicated to type, so I am not sure the stubs are 100% correct at the moment (I have a PR open at the moment to improve them). But I think it is correct that they are both generic. Copying what I wrote in python/typeshed#7787 (comment):
DictReader seems pretty clearly like it should be generic -- the type that's returned from DictReader.__next__ depends very clearly on the type that you stick into DictReader.__init__.
In fact, DictReader seems like it could possibly be typed more correctly if it took another parameter: see python/typeshed#5994 (comment). (But that's irrelevant to the runtime; adding __class_getitem__ will allow it to take any number of parameters at runtime.)
I was confused about the implicit conversion to string in DictWriter, which made me wonder if DictWriter should be generic. But DictWriterdoes some runtime typechecking in the _dict_to_list method before it does the implicit conversion to string. So I think it makes sense for DictWriter to be generic as well (as it is in the stub currently).
I am not sure that they were designed this way. Why not return just dict[Any, Any] in DictReader.__next__? The type of the result depends not only on the type of field names (which can have different types), but on types and values of other parameters and on the content of the file.
The type of keys: the type of any item in fieldnames (str | float if fieldnames is None), or the type of restkey.
The type of values: str | float, list[str|float], or the type of restval.
Activity
serhiy-storchaka commentedon May 7, 2022
Why are they generic in typeshed?
AlexWaygood commentedon May 7, 2022
DictReaderhas been generic for a while.DictWriterwas made generic in python/typeshed#5366.The classes are slightly complicated to type, so I am not sure the stubs are 100% correct at the moment (I have a PR open at the moment to improve them). But I think it is correct that they are both generic. Copying what I wrote in python/typeshed#7787 (comment):
serhiy-storchaka commentedon May 7, 2022
I am not sure that they were designed this way. Why not return just
dict[Any, Any]inDictReader.__next__? The type of the result depends not only on the type of field names (which can have different types), but on types and values of other parameters and on the content of the file.AlexWaygood commentedon May 7, 2022
Can you give an example? If the stubs are incorrect, I would be interested in improving them.
serhiy-storchaka commentedon May 7, 2022
The type of keys: the type of any item in
fieldnames(str | floatiffieldnamesis None), or the type ofrestkey.The type of values:
str | float,list[str|float], or the type ofrestval.