-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
TYP: Typing hints in pandas/io/formats/{css,csvs}.py #30398
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,13 +5,14 @@ | |||||
import csv as csvlib | ||||||
from io import StringIO | ||||||
import os | ||||||
from typing import List | ||||||
from typing import Hashable, List, Mapping, Optional, Sequence, Union | ||||||
import warnings | ||||||
from zipfile import ZipFile | ||||||
|
||||||
import numpy as np | ||||||
|
||||||
from pandas._libs import writers as libwriters | ||||||
from pandas._typing import FilePathOrBuffer | ||||||
|
||||||
from pandas.core.dtypes.generic import ( | ||||||
ABCDatetimeIndex, | ||||||
|
@@ -33,27 +34,26 @@ class CSVFormatter: | |||||
def __init__( | ||||||
self, | ||||||
obj, | ||||||
path_or_buf=None, | ||||||
sep=",", | ||||||
na_rep="", | ||||||
float_format=None, | ||||||
path_or_buf: Optional[FilePathOrBuffer[str]] = None, | ||||||
sep: str = ",", | ||||||
na_rep: str = "", | ||||||
float_format: Optional[str] = None, | ||||||
cols=None, | ||||||
header=True, | ||||||
index=True, | ||||||
index_label=None, | ||||||
mode="w", | ||||||
encoding=None, | ||||||
compression="infer", | ||||||
quoting=None, | ||||||
header: Union[bool, Sequence[Hashable]] = True, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question for @WillAyd - do we want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personal preference for only when complaining but it isn't strong if you feel otherwise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fine with that. (although may need to make exception for public apis) |
||||||
index: bool = True, | ||||||
index_label: Optional[Union[bool, Hashable, Sequence[Hashable]]] = None, | ||||||
mode: str = "w", | ||||||
encoding: Optional[str] = None, | ||||||
compression: Union[str, Mapping[str, str], None] = "infer", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
quoting: Optional[int] = None, | ||||||
line_terminator="\n", | ||||||
chunksize=None, | ||||||
chunksize: Optional[int] = None, | ||||||
quotechar='"', | ||||||
date_format=None, | ||||||
doublequote=True, | ||||||
escapechar=None, | ||||||
date_format: Optional[str] = None, | ||||||
doublequote: bool = True, | ||||||
escapechar: Optional[str] = None, | ||||||
decimal=".", | ||||||
): | ||||||
|
||||||
self.obj = obj | ||||||
|
||||||
if path_or_buf is None: | ||||||
|
@@ -154,14 +154,17 @@ def __init__( | |||||
if not index: | ||||||
self.nlevels = 0 | ||||||
|
||||||
def save(self): | ||||||
def save(self) -> None: | ||||||
""" | ||||||
Create the writer & save | ||||||
Create the writer & save. | ||||||
""" | ||||||
# GH21227 internal compression is not used when file-like passed. | ||||||
if self.compression and hasattr(self.path_or_buf, "write"): | ||||||
msg = "compression has no effect when passing file-like object as input." | ||||||
warnings.warn(msg, RuntimeWarning, stacklevel=2) | ||||||
warnings.warn( | ||||||
"compression has no effect when passing file-like object as input.", | ||||||
RuntimeWarning, | ||||||
stacklevel=2, | ||||||
) | ||||||
|
||||||
# when zip compression is called. | ||||||
is_zip = isinstance(self.path_or_buf, ZipFile) or ( | ||||||
|
@@ -223,7 +226,6 @@ def save(self): | |||||
_fh.close() | ||||||
|
||||||
def _save_header(self): | ||||||
|
||||||
writer = self.writer | ||||||
obj = self.obj | ||||||
index_label = self.index_label | ||||||
|
@@ -303,8 +305,7 @@ def _save_header(self): | |||||
encoded_labels.extend([""] * len(columns)) | ||||||
writer.writerow(encoded_labels) | ||||||
|
||||||
def _save(self): | ||||||
|
||||||
def _save(self) -> None: | ||||||
self._save_header() | ||||||
|
||||||
nrows = len(self.data_index) | ||||||
|
@@ -321,8 +322,7 @@ def _save(self): | |||||
|
||||||
self._save_chunk(start_i, end_i) | ||||||
|
||||||
def _save_chunk(self, start_i: int, end_i: int): | ||||||
|
||||||
def _save_chunk(self, start_i: int, end_i: int) -> None: | ||||||
data_index = self.data_index | ||||||
|
||||||
# create the data for a chunk | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestions?
mypy's log for 3 diffrent annotations:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the annotations for
columns
might not be consistent in the IO methods. Can you open up a follow up issue for this and just leave unannotated for now? I think might be more difficult so OK to branch off given there's already a good deal in this PR