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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions stdlib/2and3/_csv.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from typing import Any, Iterable, Iterator, List, Optional, Protocol, Sequence, Text, Union
from typing import Any, Iterable, Iterator, List, Optional, Protocol, Sequence, Text, Type, Union

QUOTE_ALL: int
QUOTE_MINIMAL: int
Expand All @@ -20,6 +20,8 @@ class Dialect:
strict: int
def __init__(self) -> None: ...

_DialectLike = Union[str, Dialect, Type[Dialect]]

class _reader(Iterator[List[str]]):
dialect: Dialect
line_num: int
Expand All @@ -41,8 +43,8 @@ class _writer:

class _Writer(Protocol):
def write(self, s: str) -> Any: ...
def writer(csvfile: _Writer, dialect: Union[Dialect, str] = ..., **fmtparams: Any) -> _writer: ...
def reader(csvfile: Iterable[Text], dialect: Union[Dialect, str] = ..., **fmtparams: Any) -> _reader: ...
def writer(csvfile: _Writer, dialect: _DialectLike = ..., **fmtparams: Any) -> _writer: ...
def reader(csvfile: Iterable[Text], dialect: _DialectLike = ..., **fmtparams: Any) -> _reader: ...
def register_dialect(name: str, dialect: Any = ..., **fmtparams: Any) -> None: ...
def unregister_dialect(name: str) -> None: ...
def get_dialect(name: str) -> Dialect: ...
Expand Down
19 changes: 5 additions & 14 deletions stdlib/2and3/csv.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ from _csv import (
QUOTE_MINIMAL as QUOTE_MINIMAL,
QUOTE_NONE as QUOTE_NONE,
QUOTE_NONNUMERIC as QUOTE_NONNUMERIC,
Dialect as Dialect,
Error as Error,
_DialectLike,
_reader,
_writer,
field_size_limit as field_size_limit,
Expand All @@ -18,19 +20,8 @@ from _csv import (
from collections import OrderedDict
from typing import Any, Dict, Iterable, Iterator, List, Mapping, Optional, Sequence, Text, Type, Union

_Dialect = Union[str, Dialect, Type[Dialect]]
_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.

delimiter: str
quotechar: Optional[str]
escapechar: Optional[str]
doublequote: bool
skipinitialspace: bool
lineterminator: str
quoting: int
def __init__(self) -> None: ...

class excel(Dialect):
delimiter: str
quotechar: str
Expand Down Expand Up @@ -62,7 +53,7 @@ class DictReader(Iterator[_DRMapping]):
restkey: Optional[str]
restval: Optional[str]
reader: _reader
dialect: _Dialect
dialect: _DialectLike
line_num: int
fieldnames: Sequence[str]
def __init__(
Expand All @@ -71,7 +62,7 @@ class DictReader(Iterator[_DRMapping]):
fieldnames: Optional[Sequence[str]] = ...,
restkey: Optional[str] = ...,
restval: Optional[str] = ...,
dialect: _Dialect = ...,
dialect: _DialectLike = ...,
*args: Any,
**kwds: Any,
) -> None: ...
Expand All @@ -92,7 +83,7 @@ class DictWriter(object):
fieldnames: Iterable[str],
restval: Optional[Any] = ...,
extrasaction: str = ...,
dialect: _Dialect = ...,
dialect: _DialectLike = ...,
*args: Any,
**kwds: Any,
) -> None: ...
Expand Down