Skip to content
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

csv: re-export Dialect from _csv #3613

Merged
merged 1 commit into from
Jan 23, 2020

Conversation

hauntsaninja
Copy link
Collaborator

Resolves #3611

Tested with mypy on:

f = open("asdf.csv", "w")

csv.writer(f)
csv.writer(f, dialect=csv.excel)
csv.writer(f, dialect=csv.excel())

csv.DictReader(f)
csv.DictReader(f, dialect=csv.excel)
csv.DictReader(f, dialect=csv.excel())

class CustomDialect(csv.Dialect):
    delimiter = "%"

csv.writer(f, dialect=CustomDialect)
csv.writer(f, dialect=CustomDialect())

Resolves python#3611

Tested with mypy on:
```
f = open("asdf.csv", "w")

csv.writer(f)
csv.writer(f, dialect=csv.excel)
csv.writer(f, dialect=csv.excel())

csv.DictReader(f)
csv.DictReader(f, dialect=csv.excel)
csv.DictReader(f, dialect=csv.excel())

class CustomDialect(csv.Dialect):
    delimiter = "%"

csv.writer(f, dialect=CustomDialect)
csv.writer(f, dialect=CustomDialect())
```
_DictRow = Mapping[str, Any]

class Dialect(object):
Copy link
Member

Choose a reason for hiding this comment

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

I thought this was actually an unrelated class at runtime? If so, we should probably keep it here, but accept both the _csv and the csv Dialects below.

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Jan 18, 2020

Choose a reason for hiding this comment

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

They are separate classes at runtime, but are perhaps morally one class, since all the CPython code duck types around it.
The issue is _csv.writer also needs to accept csv.Dialect so that common code like csv.writer(f, dialect=csv.excel) is accepted, as in #3611.
I guess we could get around the cyclic import with if TYPE_CHECKING? Does that work in pyi / is that better?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Cyclic imports are supported in stubs. if TYPE_CHECKING is not necessary as it's implied.

Copy link
Member

Choose a reason for hiding this comment

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

We discussed this some more; it seems best to just treat both as one class because that's how they're used in practice.

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.

stub for csv.writer can't accept subclass of csv.Dialect
3 participants