Skip to content

csv.DictReader and DictWriter not subscriptable at runtime #92391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cdce8p opened this issue May 6, 2022 · 5 comments · Fixed by #92393
Closed

csv.DictReader and DictWriter not subscriptable at runtime #92391

cdce8p opened this issue May 6, 2022 · 5 comments · Fixed by #92393
Labels
stdlib Python modules in the Lib dir topic-typing type-feature A feature request or enhancement

Comments

@cdce8p
Copy link
Contributor

cdce8p commented May 6, 2022

Feature or enhancement

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__.

from csv import DictWriter
from typing import IO

def f(fp: IO[str]) -> DictWriter[str]:
    return DictWriter(fp, ["id", "name"])

Previous discussion

@serhiy-storchaka
Copy link
Member

Why are they generic in typeshed?

@AlexWaygood
Copy link
Member

Why are they generic in typeshed?

DictReader has been generic for a while. DictWriter was 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):

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 DictWriter does 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).

@serhiy-storchaka
Copy link
Member

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.

@AlexWaygood
Copy link
Member

The type of the result depends not only on the type of field names... but on types and values of other parameters and on the content of the file.

Can you give an example? If the stubs are incorrect, I would be interested in improving them.

@serhiy-storchaka
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants