From 86070643ac783c87f1e763b9859da8c2b23c95c1 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 20 Jun 2017 13:04:10 -0700 Subject: [PATCH] Improve csv module and float() signature (#1418) * DictReader should not be abstract. Reformat long lines. * Make restval optional for DictWriter.__init__. * The arg to reader() is *Iterable*, not *Iterator*. * Improve signature of float() (use Union instead of overload). --- stdlib/2/__builtin__.pyi | 9 +-------- stdlib/2and3/_csv.pyi | 2 +- stdlib/2and3/csv.pyi | 38 +++++++++++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index b7c39c79f..343235ca4 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -144,14 +144,7 @@ class int(SupportsInt, SupportsFloat, SupportsAbs[int]): def __nonzero__(self) -> bool: ... class float(SupportsFloat, SupportsInt, SupportsAbs[float]): - @overload - def __init__(self) -> None: ... - @overload - def __init__(self, x: SupportsFloat) -> None: ... - @overload - def __init__(self, x: unicode) -> None: ... - @overload - def __init__(self, x: bytearray) -> None: ... + def __init__(self, x: Union[SupportsFloat, str, unicode, bytearray] = ...) -> None: ... def as_integer_ratio(self) -> Tuple[int, int]: ... def hex(self) -> str: ... def is_integer(self) -> bool: ... diff --git a/stdlib/2and3/_csv.pyi b/stdlib/2and3/_csv.pyi index 2b449c209..d5119aea8 100644 --- a/stdlib/2and3/_csv.pyi +++ b/stdlib/2and3/_csv.pyi @@ -37,7 +37,7 @@ class _writer: # TODO: precise type def writer(csvfile: Any, dialect: Any = ..., **fmtparams: Any) -> _writer: ... -def reader(csvfile: Iterator[str], dialect: Any = ..., **fmtparams: Any) -> _reader: ... +def reader(csvfile: Iterable[str], dialect: Any = ..., **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: ... diff --git a/stdlib/2and3/csv.pyi b/stdlib/2and3/csv.pyi index 6eaacb381..6683cccae 100644 --- a/stdlib/2and3/csv.pyi +++ b/stdlib/2and3/csv.pyi @@ -2,12 +2,26 @@ from collections import OrderedDict import sys from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Union -from _csv import _reader, _writer, reader as reader, writer as writer, register_dialect as register_dialect, unregister_dialect as unregister_dialect, get_dialect as get_dialect, list_dialects as list_dialects, field_size_limit as field_size_limit, QUOTE_ALL as QUOTE_ALL, QUOTE_MINIMAL as QUOTE_MINIMAL, QUOTE_NONE as QUOTE_NONE, QUOTE_NONNUMERIC as QUOTE_NONNUMERIC, Error as Error +from _csv import (_reader, + _writer, + reader as reader, + writer as writer, + register_dialect as register_dialect, + unregister_dialect as unregister_dialect, + get_dialect as get_dialect, + list_dialects as list_dialects, + field_size_limit as field_size_limit, + QUOTE_ALL as QUOTE_ALL, + QUOTE_MINIMAL as QUOTE_MINIMAL, + QUOTE_NONE as QUOTE_NONE, + QUOTE_NONNUMERIC as QUOTE_NONNUMERIC, + Error as Error, + ) _Dialect = Union[str, Dialect] _DictRow = Dict[str, Any] -class Dialect: +class Dialect(object): delimiter = ... # type: str quotechar = ... # type: Optional[str] escapechar = ... # type: Optional[str] @@ -45,7 +59,11 @@ if sys.version_info >= (3, 6): dialect = ... # type: _Dialect line_num = ... # type: int fieldnames = ... # type: Sequence[str] - def __init__(self, f: Iterator[str], fieldnames: Sequence[str] = ..., restkey: Optional[str] = ..., restval: Optional[str] = ..., dialect: _Dialect = ..., *args: Any, **kwds: Any) -> None: ... + def __init__(self, f: Iterator[str], fieldnames: Sequence[str] = ..., + restkey: Optional[str] = ..., restval: Optional[str] = ..., dialect: _Dialect = ..., + *args: Any, **kwds: Any) -> None: ... + def __iter__(self) -> Iterator[OrderedDict[str, str]]: ... + def next(self) -> OrderedDict[str, str]: ... else: class DictReader(Iterator[Dict[str, str]]): restkey = ... # type: Optional[str] @@ -54,19 +72,25 @@ else: dialect = ... # type: _Dialect line_num = ... # type: int fieldnames = ... # type: Sequence[str] - def __init__(self, f: Iterator[str], fieldnames: Sequence[str] = ..., restkey: Optional[str] = ..., restval: Optional[str] = ..., dialect: _Dialect = ..., *args: Any, **kwds: Any) -> None: ... + def __init__(self, f: Iterator[str], fieldnames: Sequence[str] = ..., + restkey: Optional[str] = ..., restval: Optional[str] = ..., dialect: _Dialect = ..., + *args: Any, **kwds: Any) -> None: ... + def __iter__(self) -> Iterator[OrderedDict[str, str]]: ... + def next(self) -> OrderedDict[str, str]: ... -class DictWriter: +class DictWriter(object): fieldnames = ... # type: Sequence[str] restval = ... # type: Optional[Any] extrasaction = ... # type: str writer = ... # type: _writer - def __init__(self, f: Any, fieldnames: Sequence[str], restval: Optional[Any], extrasaction: str = ..., dialect: _Dialect = ..., *args: Any, **kwds: Any) -> None: ... + def __init__(self, f: Any, fieldnames: Sequence[str], + restval: Optional[Any] = ..., extrasaction: str = ..., dialect: _Dialect = ..., + *args: Any, **kwds: Any) -> None: ... def writeheader(self) -> None: ... def writerow(self, rowdict: _DictRow) -> None: ... def writerows(self, rowdicts: Iterable[_DictRow]) -> None: ... -class Sniffer: +class Sniffer(object): preferred = ... # type: List[str] def __init__(self) -> None: ... def sniff(self, sample: str, delimiters: Optional[str] = ...) -> Dialect: ...