typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Fix `_csv.Dialect.__init__`

Open sobolevn opened this issue 1 year ago • 3 comments

All props can be passed to __init__ as pos-or-keyword:

>>> _csv.Dialect(1)
<_csv.Dialect object at 0x106604d70>
>>> _csv.Dialect(dialect=1)
<_csv.Dialect object at 0x106605d90>

>>> _csv.Dialect(delimiter='.')
<_csv.Dialect object at 0x106605790>

>>> _csv.Dialect(quotechar='.')
<_csv.Dialect object at 0x106605970>
>>> _csv.Dialect(quotechar=None)
<_csv.Dialect object at 0x106605850>

Source: https://github.com/python/cpython/blame/e6264b44dc7221c713b14dfa0f5929b33d362829/Modules/_csv.c#L389

sobolevn avatar Jul 11 '24 09:07 sobolevn

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jul 11 '24 10:07 github-actions[bot]

Ok, I see: # actually csv.Dialect is a different class to _csv.Dialect at runtime, but for typing purposes, they're identical

sobolevn avatar Jul 11 '24 10:07 sobolevn

See #3613 for why this was added. csv.Dialect looks to be a weird hack at runtime, probably back from a time when C classes couldn't be sub-classed by Python code.

srittau avatar Jul 11 '24 10:07 srittau

Do you still want to change anything here? CI is failing.

JelleZijlstra avatar Oct 03 '24 04:10 JelleZijlstra

We want:

  • "Base class" _csv.Dialect must have __init__ with parameters
  • "Child class" csv.Dialect must have __init__(self)

I can make csv.Dialect a subclass of _csv.Dialect and make unsafe __init__ redefinition with type ignores. Will try that :)

sobolevn avatar Oct 03 '24 06:10 sobolevn

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Oct 03 '24 06:10 github-actions[bot]

@JelleZijlstra thanks for the reminder!

sobolevn avatar Oct 03 '24 06:10 sobolevn

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Oct 03 '24 09:10 github-actions[bot]