Skip to content

Commit 5f55717

Browse files
authored
_csv.pyi: minor cleanup (#7789)
1 parent 0b5213a commit 5f55717

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

stdlib/_csv.pyi

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from _typeshed import SupportsWrite
12
from collections.abc import Iterable, Iterator
2-
from typing import Any, Protocol, Union
3+
from typing import Any, Union
34
from typing_extensions import Literal, TypeAlias
45

56
__version__: str
@@ -9,6 +10,10 @@ QUOTE_MINIMAL: Literal[0]
910
QUOTE_NONE: Literal[3]
1011
QUOTE_NONNUMERIC: Literal[2]
1112

13+
# Ideally this would be `QUOTE_ALL | QUOTE_MINIMAL | QUOTE_NONE | QUOTE_NONNUMERIC`
14+
# However, using literals in situations like these can cause false-positives (see #7258)
15+
_QuotingType: TypeAlias = int
16+
1217
class Error(Exception): ...
1318

1419
class Dialect:
@@ -18,26 +23,25 @@ class Dialect:
1823
doublequote: bool
1924
skipinitialspace: bool
2025
lineterminator: str
21-
quoting: int
22-
strict: int
26+
quoting: _QuotingType
27+
strict: bool
2328
def __init__(self) -> None: ...
2429

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

2732
class _reader(Iterator[list[str]]):
28-
dialect: Dialect
33+
@property
34+
def dialect(self) -> Dialect: ...
2935
line_num: int
3036
def __next__(self) -> list[str]: ...
3137

3238
class _writer:
33-
dialect: Dialect
39+
@property
40+
def dialect(self) -> Dialect: ...
3441
def writerow(self, row: Iterable[Any]) -> Any: ...
3542
def writerows(self, rows: Iterable[Iterable[Any]]) -> None: ...
3643

37-
class _Writer(Protocol):
38-
def write(self, __s: str) -> object: ...
39-
40-
def writer(csvfile: _Writer, dialect: _DialectLike = ..., **fmtparams: Any) -> _writer: ...
44+
def writer(csvfile: SupportsWrite[str], dialect: _DialectLike = ..., **fmtparams: Any) -> _writer: ...
4145
def reader(csvfile: Iterable[str], dialect: _DialectLike = ..., **fmtparams: Any) -> _reader: ...
4246
def register_dialect(name: str, dialect: Any = ..., **fmtparams: Any) -> None: ...
4347
def unregister_dialect(name: str) -> None: ...

stdlib/csv.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from _csv import (
88
Error as Error,
99
__version__ as __version__,
1010
_DialectLike,
11+
_QuotingType,
1112
_reader,
1213
_writer,
1314
field_size_limit as field_size_limit,
@@ -59,7 +60,7 @@ class excel(Dialect):
5960
doublequote: bool
6061
skipinitialspace: bool
6162
lineterminator: str
62-
quoting: int
63+
quoting: _QuotingType
6364

6465
class excel_tab(excel):
6566
delimiter: str
@@ -70,7 +71,7 @@ class unix_dialect(Dialect):
7071
doublequote: bool
7172
skipinitialspace: bool
7273
lineterminator: str
73-
quoting: int
74+
quoting: _QuotingType
7475

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

0 commit comments

Comments
 (0)