Skip to content

Fix _csv.Dialect.__init__ #12320

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

Merged
merged 7 commits into from
Oct 3, 2024
Merged

Fix _csv.Dialect.__init__ #12320

merged 7 commits into from
Oct 3, 2024

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jul 11, 2024

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 and others added 2 commits July 11, 2024 12:59
All props can be passed to `__init__` as pos-or-keyword:

```python
>>> _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>
```

This comment has been minimized.

@sobolevn
Copy link
Member Author

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

@srittau
Copy link
Collaborator

srittau commented Jul 11, 2024

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.

@JelleZijlstra
Copy link
Member

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

@sobolevn
Copy link
Member Author

sobolevn commented Oct 3, 2024

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 :)

This comment has been minimized.

@sobolevn
Copy link
Member Author

sobolevn commented Oct 3, 2024

@JelleZijlstra thanks for the reminder!

def __init__(self) -> None: ...

_DialectLike: TypeAlias = str | Dialect | type[Dialect]
def __init__(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be __new__?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is __new__ for _csv.Dialect, but __init__ for csv.Dialect. I feel safer to use __init__ in both cases.

Copy link
Contributor

github-actions bot commented Oct 3, 2024

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

@srittau srittau merged commit d16fe74 into main Oct 3, 2024
63 checks passed
@srittau srittau deleted the sobolevn-patch-1 branch October 3, 2024 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants