typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

2 different `Dialect` types - one in `csv` module and other in `_csv` module

Open Dr-Irv opened this issue 5 months ago • 3 comments

Consider this code:

from csv import get_dialect


def foo(s: str):
    dialect = get_dialect(s)
    reveal_type(dialect)

mypy reports

dialects.py:6: note: Revealed type is "_csv.Dialect"

One would expect the result would be csv.Dialect.

There are 2 classes called Dialect in typeshed: https://github.com/python/typeshed/blob/2480d7e7c74493a024eaf254c5d2c6f452c80ee2/stdlib/_csv.pyi#L28 and https://github.com/python/typeshed/blob/2480d7e7c74493a024eaf254c5d2c6f452c80ee2/stdlib/csv.pyi#L61

get_dialect() is imported in csv.pyi from _csv.pyi, so the declaration in _csv.pyi is returning _csv.Dialect instead of csv.Dialect, so the type checkers see that as 2 separate classes.

Dr-Irv avatar Aug 31 '25 16:08 Dr-Irv

Hmm, at a glance this seem to be accurate to the runtime behavior: there are actually two different Dialect classes, csv.Dialect vs _csv.Dialect, and csv.get_dialect() doess seem to return the latter:

>>> csv.get_dialect("excel")
<_csv.Dialect object at 0x7e438f29b800>

>>> isinstance(csv.get_dialect("excel"), csv.Dialect)
False
>>> isinstance(csv.get_dialect("excel"), csv._Dialect)
True

brianschubert avatar Aug 31 '25 17:08 brianschubert

The docs say that get_dialect() returns a csv.Dialect so that's where the confusion is coming from.

Dr-Irv avatar Aug 31 '25 18:08 Dr-Irv

This is a bit unfortunate, mostly because the implementation in Python is a bit hacky. We could in theory just declare csv.Dialect to be an alias of _csv.Dialect (or a sub-class), since their behavior is so similar, but that could leads to other problems. I would suggest to just add a note to csv.Dialect that this is often the wrong class and only used for sub-classing, and usually using _csv.Dialect is the correct option.

srittau avatar Sep 02 '25 10:09 srittau