Skip to content

DictWriter is not subscriptable #7787

Closed
@clo-vis

Description

@clo-vis

mypy complains that DictWriter is missing the type parameter in the code:

from csv import DictWriter
from typing import IO
def f_is_for_fun(file: IO[str]) -> DictWriter:
    return DictWriter(file, ["id", "name"])

which is reasonable, because DictWriter is defined as class DictWriter(Generic[_T]) (https://github.com/python/typeshed/blob/master/stdlib/csv.pyi)

But if I change the code to make mypy happy:

from csv import DictWriter
from typing import IO
def f_is_for_fun(file: IO[str]) -> DictWriter[str]:
    return DictWriter(file, ["id", "name"])

then I become an error at runtime:


def f_is_for_fun(file: IO[str]) -> DictWriter[str]:
TypeError: 'type' object is not subscriptable

or, with python 3.11
TypeError: type 'DictWriter' is not subscriptable

And if I add
from __future__ import annotations
then mypy and the python interpreter are happy, but not pylint.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions