Skip to content

DictWriter signed as unsubscriptable #6519

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
clo-vis opened this issue May 6, 2022 · 1 comment
Closed

DictWriter signed as unsubscriptable #6519

clo-vis opened this issue May 6, 2022 · 1 comment
Labels
C: unsubscriptable-object Issues related to 'unsubscriptable-object' check Invalid Not a bug, already exists or already fixed typing

Comments

@clo-vis
Copy link

clo-vis commented May 6, 2022

Bug description

from csv import DictWriter
from typing import IO


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

Command used

pylint playground.py

Pylint output

************* Module playground
...
playground.py:5:35: E1136: Value 'DictWriter' is unsubscriptable (unsubscriptable-object)

Expected behavior

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

On a side note, mypy doesn't complain about the code, but it does complain for

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

Missing type parameters for generic type "DictWriter" [type-arg]

Pylint version

pylint 2.13.8
astroid 2.11.4
Python 3.9.12 (tags/v3.9.12:b28265d, Mar 23 2022, 23:52:46) [MSC v.1929 64 bit (AMD64)]

OS / Environment

Microsoft Windows [Version 10.0.19042.1645]

@clo-vis clo-vis added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 6, 2022
@cdce8p cdce8p added typing C: unsubscriptable-object Issues related to 'unsubscriptable-object' check and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels May 6, 2022
@cdce8p
Copy link
Member

cdce8p commented May 6, 2022

The error is correct. At runtime, DictWriter is unsubscriptable and will raise a TypeError.
You need to use string annotations or PEP585 from __future__ import annotations. That will also hide the pylint error.

https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime

@cdce8p cdce8p added the Invalid Not a bug, already exists or already fixed label May 6, 2022
@cdce8p cdce8p closed this as completed May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: unsubscriptable-object Issues related to 'unsubscriptable-object' check Invalid Not a bug, already exists or already fixed typing
Projects
None yet
Development

No branches or pull requests

2 participants