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.pyi: minor cleanup #7789

Merged
merged 2 commits into from
May 6, 2022
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
22 changes: 13 additions & 9 deletions stdlib/_csv.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from _typeshed import SupportsWrite
from collections.abc import Iterable, Iterator
from typing import Any, Protocol, Union
from typing import Any, Union
from typing_extensions import Literal, TypeAlias

__version__: str
Expand All @@ -9,6 +10,10 @@ QUOTE_MINIMAL: Literal[0]
QUOTE_NONE: Literal[3]
QUOTE_NONNUMERIC: Literal[2]

# Ideally this would be `QUOTE_ALL | QUOTE_MINIMAL | QUOTE_NONE | QUOTE_NONNUMERIC`
# However, using literals in situations like these can cause false-positives (see #7258)
_QuotingType: TypeAlias = int

class Error(Exception): ...

class Dialect:
Expand All @@ -18,26 +23,25 @@ class Dialect:
doublequote: bool
skipinitialspace: bool
lineterminator: str
quoting: int
strict: int
quoting: _QuotingType
strict: bool
def __init__(self) -> None: ...

_DialectLike: TypeAlias = Union[str, Dialect, type[Dialect]]

class _reader(Iterator[list[str]]):
dialect: Dialect
@property
def dialect(self) -> Dialect: ...
line_num: int
def __next__(self) -> list[str]: ...

class _writer:
dialect: Dialect
@property
def dialect(self) -> Dialect: ...
def writerow(self, row: Iterable[Any]) -> Any: ...
def writerows(self, rows: Iterable[Iterable[Any]]) -> None: ...

class _Writer(Protocol):
def write(self, __s: str) -> object: ...

def writer(csvfile: _Writer, dialect: _DialectLike = ..., **fmtparams: Any) -> _writer: ...
def writer(csvfile: SupportsWrite[str], dialect: _DialectLike = ..., **fmtparams: Any) -> _writer: ...
def reader(csvfile: Iterable[str], dialect: _DialectLike = ..., **fmtparams: Any) -> _reader: ...
def register_dialect(name: str, dialect: Any = ..., **fmtparams: Any) -> None: ...
def unregister_dialect(name: str) -> None: ...
Expand Down
5 changes: 3 additions & 2 deletions stdlib/csv.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from _csv import (
Error as Error,
__version__ as __version__,
_DialectLike,
_QuotingType,
_reader,
_writer,
field_size_limit as field_size_limit,
Expand Down Expand Up @@ -59,7 +60,7 @@ class excel(Dialect):
doublequote: bool
skipinitialspace: bool
lineterminator: str
quoting: int
quoting: _QuotingType

class excel_tab(excel):
delimiter: str
Expand All @@ -70,7 +71,7 @@ class unix_dialect(Dialect):
doublequote: bool
skipinitialspace: bool
lineterminator: str
quoting: int
quoting: _QuotingType

class DictReader(Generic[_T], Iterator[_DictReadMapping[_T, str]]):
fieldnames: Sequence[_T] | None
Expand Down