1

I have a program that lets the user choose a CSV dialect. I wrote a bit of code to list all registered dialects and display their docstrings, but it shows the docstring for the parent class (Dialect), not the subclass (excel, excel_tab, etc). Here's some code:

>>> from csv import list_dialects, get_dialect
>>> from inspect import getdoc
>>> for name in list_dialects():
...     print(name, repr(getdoc(get_dialect(name))))
...
excel 'CSV dialect\n\nThe Dialect type records CSV parsing and generation options.'
excel-tab 'CSV dialect\n\nThe Dialect type records CSV parsing and generation options.'
unix 'CSV dialect\n\nThe Dialect type records CSV parsing and generation options.'

Based on the source code, this is what I was hoping to get:

excel 'Describe the usual properties of Excel-generated CSV files.'
excel-tab 'Describe the usual properties of Excel-generated TAB-delimited files.'
unix 'Describe the usual properties of Unix-generated CSV files.'
samwyse
  • 2,760
  • 1
  • 27
  • 38
  • It might be easier to just iterate through the `__subclasses__` of `Dialect`, see e.g. https://stackoverflow.com/q/3862310/3001761. If you look at what `get_dialect` returns, they _are_ all instances of `_csv.Dialect`. – jonrsharpe Mar 25 '22 at 14:51
  • I'll note that `get_dialect` seems to return a value of type `Dialect`, rather than a value of any of the specific sub-classes. I'm not sure why, though `get_dialect` is implemented in C, not Python (ee https://github.com/python/cpython/blob/3.10/Modules/_csv.c.), so there seems to be some detail of the dialect registry I'm not seeing. – chepner Mar 25 '22 at 14:58
  • Point is, you have an unintentional XY problem. `getdoc` *would* return the doc string of the subclass, if you actually *had* an instance of the subclass. – chepner Mar 25 '22 at 15:01
  • I changed the question since this obviously isn't an issue with subclasses. If either of you want to change your comment to an answer, I'll accept one of them. – samwyse Mar 25 '22 at 15:06
  • And it turns out that excel_tab is a subclass of excel, not Dialect, so I can't just iterate, I need to recurse through all the subclasses. – samwyse Mar 25 '22 at 15:12
  • @samwyse if you keep reading down that post you can see there are snippets covering that too. – jonrsharpe Mar 25 '22 at 15:36

1 Answers1

1

csv.get_dialect is pretty weird. It doesn't actually return an instance of the classes you saw in the source code. In fact, it doesn't even return an instance of csv.Dialect!

For some reason, csv.get_dialect returns an instance of _csv.Dialect. Note the underscore - that's a different module and a different Dialect class that is not csv.Dialect and has no inheritance relationship with csv.Dialect.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Oof, I didn't even notice the point about `csv.Dialect` and `_csv.Dialecct`; I just assumed the former was (or was a wrapper) around the latter. – chepner Mar 25 '22 at 15:19
  • Looks like `register_dialect` uses `csv.Dialect` to create an instance of `_csv.Dialect`. – chepner Mar 25 '22 at 15:23