Skip to content

CLN: Update files (as per #36450) to Python 3.7+ syntax #36457

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 1 commit into from
Sep 18, 2020
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
2 changes: 1 addition & 1 deletion pandas/_config/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def detect_console_encoding() -> str:
encoding = None
try:
encoding = sys.stdout.encoding or sys.stdin.encoding
except (AttributeError, IOError):
except (AttributeError, OSError):
pass

# try again for something better
Expand Down
3 changes: 1 addition & 2 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1960,8 +1960,7 @@ def index_subclass_makers_generator():
makeCategoricalIndex,
makeMultiIndex,
]
for make_index_func in make_index_funcs:
yield make_index_func
yield from make_index_funcs


def all_timeseries_index_generator(k=10):
Expand Down
10 changes: 5 additions & 5 deletions pandas/_vendored/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def __repr__(self):

def __getitem__(self, parameters):
item = typing._type_check(
parameters, "{} accepts only single type".format(self._name)
parameters, f"{self._name} accepts only single type"
)
return _GenericAlias(self, (item,))

Expand Down Expand Up @@ -1671,7 +1671,7 @@ def __class_getitem__(cls, params):
params = (params,)
if not params and cls is not Tuple:
raise TypeError(
"Parameter list to {}[...] cannot be empty".format(cls.__qualname__)
f"Parameter list to {cls.__qualname__}[...] cannot be empty"
)
msg = "Parameters to generic types must be types."
params = tuple(_type_check(p, msg) for p in params)
Expand Down Expand Up @@ -2113,7 +2113,7 @@ def __class_getitem__(cls, params):
return _AnnotatedAlias(origin, metadata)

def __init_subclass__(cls, *args, **kwargs):
raise TypeError("Cannot subclass {}.Annotated".format(cls.__module__))
raise TypeError(f"Cannot subclass {cls.__module__}.Annotated")

def _strip_annotations(t):
"""Strips the annotations from a given type.
Expand Down Expand Up @@ -2195,7 +2195,7 @@ def _tree_repr(self, tree):
else:
tp_repr = origin[0]._tree_repr(origin)
metadata_reprs = ", ".join(repr(arg) for arg in metadata)
return "%s[%s, %s]" % (cls, tp_repr, metadata_reprs)
return f"{cls}[{tp_repr}, {metadata_reprs}]"

def _subs_tree(self, tvars=None, args=None): # noqa
if self is Annotated:
Expand Down Expand Up @@ -2382,7 +2382,7 @@ def TypeAlias(self, parameters):

It's invalid when used anywhere except as in the example above.
"""
raise TypeError("{} is not subscriptable".format(self))
raise TypeError(f"{self} is not subscriptable")


elif sys.version_info[:2] >= (3, 7):
Expand Down
6 changes: 3 additions & 3 deletions pandas/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False):
stderr=(subprocess.PIPE if hide_stderr else None),
)
break
except EnvironmentError:
except OSError:
e = sys.exc_info()[1]
if e.errno == errno.ENOENT:
continue
Expand Down Expand Up @@ -121,7 +121,7 @@ def git_get_keywords(versionfile_abs):
# _version.py.
keywords = {}
try:
f = open(versionfile_abs, "r")
f = open(versionfile_abs)
for line in f.readlines():
if line.strip().startswith("git_refnames ="):
mo = re.search(r'=\s*"(.*)"', line)
Expand All @@ -132,7 +132,7 @@ def git_get_keywords(versionfile_abs):
if mo:
keywords["full"] = mo.group(1)
f.close()
except EnvironmentError:
except OSError:
pass
return keywords

Expand Down
4 changes: 2 additions & 2 deletions pandas/io/clipboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def copy_dev_clipboard(text):
fo.write(text)

def paste_dev_clipboard() -> str:
with open("/dev/clipboard", "rt") as fo:
with open("/dev/clipboard") as fo:
content = fo.read()
return content

Expand Down Expand Up @@ -521,7 +521,7 @@ def determine_clipboard():
return init_windows_clipboard()

if platform.system() == "Linux":
with open("/proc/version", "r") as f:
with open("/proc/version") as f:
if "Microsoft" in f.read():
return init_wsl_clipboard()

Expand Down
6 changes: 2 additions & 4 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,7 @@ def _format_regular_rows(self):
else:
coloffset = 0

for cell in self._generate_body(coloffset):
yield cell
yield from self._generate_body(coloffset)

def _format_hierarchical_rows(self):
has_aliases = isinstance(self.header, (tuple, list, np.ndarray, ABCIndex))
Expand Down Expand Up @@ -664,8 +663,7 @@ def _format_hierarchical_rows(self):
)
gcolidx += 1

for cell in self._generate_body(gcolidx):
yield cell
yield from self._generate_body(gcolidx)

def _generate_body(self, coloffset: int):
if self.styler is None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ def _build_doc(self):
r = r.getroot()
except AttributeError:
pass
except (UnicodeDecodeError, IOError) as e:
except (UnicodeDecodeError, OSError) as e:
# if the input is a blob of html goop
if not is_url(self.io):
r = fromstring(self.io, parser=parser)
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ def close(self):
if self.should_close:
try:
self.open_stream.close()
except (IOError, AttributeError):
except (OSError, AttributeError):
pass
for file_handle in self.file_handles:
file_handle.close()
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def read_hdf(

if isinstance(path_or_buf, HDFStore):
if not path_or_buf.is_open:
raise IOError("The HDFStore must be open for reading.")
raise OSError("The HDFStore must be open for reading.")

store = path_or_buf
auto_close = False
Expand Down Expand Up @@ -693,7 +693,7 @@ def open(self, mode: str = "a", **kwargs):

try:
self._handle = tables.open_file(self._path, self._mode, **kwargs)
except IOError as err: # pragma: no cover
except OSError as err: # pragma: no cover
if "can not be written" in str(err):
print(f"Opening {self._path} in read-only mode")
self._handle = tables.open_file(self._path, "r", **kwargs)
Expand Down Expand Up @@ -724,7 +724,7 @@ def open(self, mode: str = "a", **kwargs):
# trying to read from a non-existent file causes an error which
# is not part of IOError, make it one
if self._mode == "r" and "Unable to open/create file" in str(err):
raise IOError(str(err)) from err
raise OSError(str(err)) from err
raise

def close(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ def close(self) -> None:
""" close the handle if its open """
try:
self.path_or_buf.close()
except IOError:
except OSError:
pass

def _set_encoding(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def _setup_subplots(self):
valid_log = {False, True, "sym", None}
input_log = {self.logx, self.logy, self.loglog}
if input_log - valid_log:
invalid_log = next(iter((input_log - valid_log)))
invalid_log = next(iter(input_log - valid_log))
raise ValueError(
f"Boolean, None and 'sym' are valid options, '{invalid_log}' is given."
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def test_constructor_with_generator(self):
# returned a scalar for a generator

exp = Categorical([0, 1, 2])
cat = Categorical((x for x in [0, 1, 2]))
cat = Categorical(x for x in [0, 1, 2])
tm.assert_categorical_equal(cat, exp)
cat = Categorical(range(3))
tm.assert_categorical_equal(cat, exp)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/integer/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_from_dtype_from_float(data):

# from int / array
expected = pd.Series(data).dropna().reset_index(drop=True)
dropped = np.array(data.dropna()).astype(np.dtype((dtype.type)))
dropped = np.array(data.dropna()).astype(np.dtype(dtype.type))
result = pd.Series(dropped, dtype=str(dtype))
tm.assert_series_equal(result, expected)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def _na_value(self):

def _formatter(self, boxed=False):
if boxed:
return "Decimal: {0}".format
return "Decimal: {}".format
return repr

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_combine_add(self, data_repeated):
s2 = pd.Series(orig_data2)
result = s1.combine(s2, lambda x1, x2: x1 + x2)
expected = pd.Series(
([a + b for (a, b) in zip(list(orig_data1), list(orig_data2))])
[a + b for (a, b) in zip(list(orig_data1), list(orig_data2))]
)
self.assert_series_equal(result, expected)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_series_with_name_not_matching_column(self):
lambda: DataFrame({}),
lambda: DataFrame(()),
lambda: DataFrame([]),
lambda: DataFrame((_ for _ in [])),
lambda: DataFrame(_ for _ in []),
lambda: DataFrame(range(0)),
lambda: DataFrame(data=None),
lambda: DataFrame(data={}),
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ def test_len():

# issue 11016
df = pd.DataFrame(dict(a=[np.nan] * 3, b=[1, 2, 3]))
assert len(df.groupby(("a"))) == 0
assert len(df.groupby(("b"))) == 3
assert len(df.groupby("a")) == 0
assert len(df.groupby("b")) == 3
assert len(df.groupby(["a", "b"])) == 3


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ def test_get_group(self):
with pytest.raises(ValueError, match=msg):
g.get_group("foo")
with pytest.raises(ValueError, match=msg):
g.get_group(("foo"))
g.get_group("foo")
msg = "must supply a same-length tuple to get_group with multiple grouping keys"
with pytest.raises(ValueError, match=msg):
g.get_group(("foo", "bar", "baz"))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ def test_get_indexer_strings_raises(self):
def test_get_indexer_numeric_index_boolean_target(self, idx_class):
# GH 16877

numeric_index = idx_class(RangeIndex((4)))
numeric_index = idx_class(RangeIndex(4))
result = numeric_index.get_indexer([True, False, True])
expected = np.array([-1, -1, -1], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ def run_tests(df, rhs, right):
# make frames multi-type & re-run tests
for frame in [df, rhs, right]:
frame["joe"] = frame["joe"].astype("float64")
frame["jolie"] = frame["jolie"].map("@{0}".format)
frame["jolie"] = frame["jolie"].map("@{}".format)

run_tests(df, rhs, right)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def test_to_string_unicode_columns(self, float_frame):
assert isinstance(result, str)

def test_to_string_utf8_columns(self):
n = "\u05d0".encode("utf-8")
n = "\u05d0".encode()

with option_context("display.max_rows", 1):
df = DataFrame([1, 2], columns=[n])
Expand Down
20 changes: 10 additions & 10 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_to_csv_with_single_column(self):
"""
with tm.ensure_clean("test.csv") as path:
df1.to_csv(path, header=None, index=None)
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected1

df2 = DataFrame([1, None])
Expand All @@ -36,7 +36,7 @@ def test_to_csv_with_single_column(self):
"""
with tm.ensure_clean("test.csv") as path:
df2.to_csv(path, header=None, index=None)
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected2

def test_to_csv_defualt_encoding(self):
Expand All @@ -58,7 +58,7 @@ def test_to_csv_quotechar(self):

with tm.ensure_clean("test.csv") as path:
df.to_csv(path, quoting=1) # 1=QUOTE_ALL
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected

expected = """\
Expand All @@ -69,7 +69,7 @@ def test_to_csv_quotechar(self):

with tm.ensure_clean("test.csv") as path:
df.to_csv(path, quoting=1, quotechar="$")
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected

with tm.ensure_clean("test.csv") as path:
Expand All @@ -86,7 +86,7 @@ def test_to_csv_doublequote(self):

with tm.ensure_clean("test.csv") as path:
df.to_csv(path, quoting=1, doublequote=True) # QUOTE_ALL
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected

from _csv import Error
Expand All @@ -105,7 +105,7 @@ def test_to_csv_escapechar(self):

with tm.ensure_clean("test.csv") as path: # QUOTE_ALL
df.to_csv(path, quoting=1, doublequote=False, escapechar="\\")
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected

df = DataFrame({"col": ["a,a", ",bb,"]})
Expand All @@ -117,7 +117,7 @@ def test_to_csv_escapechar(self):

with tm.ensure_clean("test.csv") as path:
df.to_csv(path, quoting=3, escapechar="\\") # QUOTE_NONE
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected

def test_csv_to_string(self):
Expand Down Expand Up @@ -342,7 +342,7 @@ def test_to_csv_string_array_ascii(self):
"""
with tm.ensure_clean("str_test.csv") as path:
df.to_csv(path, encoding="ascii")
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected_ascii

def test_to_csv_string_array_utf8(self):
Expand All @@ -356,7 +356,7 @@ def test_to_csv_string_array_utf8(self):
"""
with tm.ensure_clean("unicode_test.csv") as path:
df.to_csv(path, encoding="utf-8")
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected_utf8

def test_to_csv_string_with_lf(self):
Expand Down Expand Up @@ -467,7 +467,7 @@ def test_to_csv_write_to_open_file(self):
with open(path, "w") as f:
f.write("manual header\n")
df.to_csv(f, header=None, index=None)
with open(path, "r") as f:
with open(path) as f:
assert f.read() == expected

def test_to_csv_write_to_open_file_with_newline_py3(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_to_html_encoding(float_frame, tmp_path):
# GH 28663
path = tmp_path / "test.html"
float_frame.to_html(path, encoding="gbk")
with open(str(path), "r", encoding="gbk") as f:
with open(str(path), encoding="gbk") as f:
assert float_frame.to_html() == f.read()


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_to_latex_filename(self, float_frame):
with tm.ensure_clean("test.tex") as path:
float_frame.to_latex(path)

with open(path, "r") as f:
with open(path) as f:
assert float_frame.to_latex() == f.read()

# test with utf-8 and encoding option (GH 7061)
Expand Down
Loading