Skip to content

csv.DictReader and DictWriter not subscriptable at runtime #92391

Description

@cdce8p

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

Activity

  1. serhiy-storchaka commented on May 7, 2022

    @serhiy-storchaka
    Member

    Why are they generic in typeshed?

  2. AlexWaygood commented on May 7, 2022

    @AlexWaygood
    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).

  3. serhiy-storchaka commented on May 7, 2022

    @serhiy-storchaka
    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.

  4. AlexWaygood commented on May 7, 2022

    @AlexWaygood
    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.

  5. serhiy-storchaka commented on May 7, 2022

    @serhiy-storchaka
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions