Skip to content

non-string fieldnames for csv.DictReader and csv.DictWriter #5366

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 4 commits into from
May 10, 2021
Merged
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
49 changes: 29 additions & 20 deletions stdlib/csv.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ from _csv import (
unregister_dialect as unregister_dialect,
writer as writer,
)
from collections import OrderedDict
from typing import Any, Dict, Iterable, Iterator, List, Mapping, Optional, Sequence, Text, Type
from typing import Any, Generic, Iterable, Iterator, List, Mapping, Optional, Sequence, Text, Type, TypeVar, overload

_DictRow = Mapping[str, Any]
if sys.version_info >= (3, 8) or sys.version_info < (3, 6):
from typing import Dict as _DictReadMapping
else:
from collections import OrderedDict as _DictReadMapping

_T = TypeVar("_T")

class excel(Dialect):
delimiter: str
Expand All @@ -42,45 +46,50 @@ if sys.version_info >= (3,):
lineterminator: str
quoting: int

if sys.version_info >= (3, 8):
_DRMapping = Dict[str, str]
elif sys.version_info >= (3, 6):
_DRMapping = OrderedDict[str, str]
else:
_DRMapping = Dict[str, str]

class DictReader(Iterator[_DRMapping]):
class DictReader(Generic[_T], Iterator[_DictReadMapping[_T, str]]):
fieldnames: Optional[Sequence[_T]]
restkey: Optional[str]
restval: Optional[str]
reader: _reader
dialect: _DialectLike
line_num: int
fieldnames: Optional[Sequence[str]]
@overload
def __init__(
self,
f: Iterable[Text],
fieldnames: Sequence[_T],
restkey: Optional[str] = ...,
restval: Optional[str] = ...,
dialect: _DialectLike = ...,
*args: Any,
**kwds: Any,
) -> None: ...
@overload
def __init__(
self: DictReader[str],
f: Iterable[Text],
fieldnames: Optional[Sequence[str]] = ...,
restkey: Optional[str] = ...,
restval: Optional[str] = ...,
dialect: _DialectLike = ...,
*args: Any,
**kwds: Any,
) -> None: ...
def __iter__(self) -> DictReader: ...
def __iter__(self) -> DictReader[_T]: ...
if sys.version_info >= (3,):
def __next__(self) -> _DRMapping: ...
def __next__(self) -> _DictReadMapping[_T, str]: ...
else:
def next(self) -> _DRMapping: ...
def next(self) -> _DictReadMapping[_T, str]: ...

class DictWriter(object):
fieldnames: Sequence[str]
class DictWriter(Generic[_T]):
fieldnames: Sequence[_T]
restval: Optional[Any]
extrasaction: str
writer: _writer
def __init__(
self,
f: Any,
fieldnames: Iterable[str],
fieldnames: Sequence[_T],
restval: Optional[Any] = ...,
extrasaction: str = ...,
dialect: _DialectLike = ...,
Expand All @@ -91,8 +100,8 @@ class DictWriter(object):
def writeheader(self) -> Any: ...
else:
def writeheader(self) -> None: ...
def writerow(self, rowdict: _DictRow) -> Any: ...
def writerows(self, rowdicts: Iterable[_DictRow]) -> None: ...
def writerow(self, rowdict: Mapping[_T, Any]) -> Any: ...
def writerows(self, rowdicts: Iterable[Mapping[_T, Any]]) -> None: ...

class Sniffer(object):
preferred: List[str]
Expand Down