Skip to content

Commit 722cee1

Browse files
Leviobmliu08
authored andcommitted
STYLE: Fix Pylint Style Checks For used-before-assignment (pandas-dev#48940)
* STYLE: Fix Pylint Style Checks For used-before-assignment * Refactor: Use 'with open' instead of open/close * CLN: disable used-before-assignment warning * CLN: Remove used-before-assignment from pyproject.toml * CLN: disable used-before-assignment warning in-line
1 parent da2a540 commit 722cee1

File tree

10 files changed

+14
-15
lines changed

10 files changed

+14
-15
lines changed

pandas/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=
7373
assert isinstance(commands, list)
7474
p = None
7575
for c in commands:
76+
dispcmd = str([c] + args)
7677
try:
77-
dispcmd = str([c] + args)
7878
# remember shell=False, so use git.cmd on windows, not just git
7979
p = subprocess.Popen(
8080
[c] + args,

pandas/core/arrays/sparse/array.py

+1
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,7 @@ def astype(self, dtype: AstypeArg | None = None, copy: bool = True):
13091309
values = np.array(self)
13101310
return astype_nansafe(values, dtype=future_dtype)
13111311
else:
1312+
# pylint: disable-next=used-before-assignment
13121313
dtype = cast(ExtensionDtype, dtype)
13131314
cls = dtype.construct_array_type()
13141315
return cls._from_sequence(self, dtype=dtype, copy=copy)

pandas/core/dtypes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def is_scipy_sparse(arr) -> bool:
260260
"""
261261
global _is_scipy_sparse
262262

263-
if _is_scipy_sparse is None:
263+
if _is_scipy_sparse is None: # pylint: disable=used-before-assignment
264264
try:
265265
from scipy.sparse import issparse as _is_scipy_sparse
266266
except ImportError:

pandas/core/groupby/indexing.py

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def _positional_selector(self) -> GroupByPositionalSelector:
112112
4 b 5
113113
"""
114114
if TYPE_CHECKING:
115+
# pylint: disable-next=used-before-assignment
115116
groupby_self = cast(groupby.GroupBy, self)
116117
else:
117118
groupby_self = self

pandas/core/tools/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ def to_datetime(
10601060
exact=exact,
10611061
infer_datetime_format=infer_datetime_format,
10621062
)
1063-
1063+
# pylint: disable-next=used-before-assignment
10641064
result: Timestamp | NaTType | Series | Index
10651065

10661066
if isinstance(arg, Timestamp):

pandas/io/formats/style.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@
7878
has_mpl = True
7979
except ImportError:
8080
has_mpl = False
81-
no_mpl_message = "{0} requires matplotlib."
8281

8382

8483
@contextmanager
8584
def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
8685
if has_mpl:
8786
yield plt, mpl
8887
else:
89-
raise ImportError(no_mpl_message.format(func.__name__))
88+
raise ImportError(f"{func.__name__} requires matplotlib.")
9089

9190

9291
####

pandas/tests/io/excel/test_writers.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -865,11 +865,10 @@ def test_to_excel_output_encoding(self, ext):
865865
def test_to_excel_unicode_filename(self, ext):
866866
with tm.ensure_clean("\u0192u." + ext) as filename:
867867
try:
868-
f = open(filename, "wb")
868+
with open(filename, "wb"):
869+
pass
869870
except UnicodeEncodeError:
870871
pytest.skip("No unicode file names on this system")
871-
finally:
872-
f.close()
873872

874873
df = DataFrame(
875874
[[0.123456, 0.234567, 0.567567], [12.32112, 123123.2, 321321.2]],

pandas/tests/io/pytables/test_store.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,13 @@ def test_copy():
876876
with catch_warnings(record=True):
877877

878878
def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs):
879-
try:
880-
store = HDFStore(f, "r")
879+
if new_f is None:
880+
import tempfile
881881

882-
if new_f is None:
883-
import tempfile
882+
fd, new_f = tempfile.mkstemp()
884883

885-
fd, new_f = tempfile.mkstemp()
884+
try:
885+
store = HDFStore(f, "r")
886886
tstore = store.copy(new_f, keys=keys, propindexes=propindexes, **kwargs)
887887

888888
# check keys

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ disable = [
7575
"unsubscriptable-object",
7676
"unsupported-assignment-operation",
7777
"unsupported-membership-test",
78-
"used-before-assignment",
7978

8079
# pylint type "C": convention, for programming standard violation
8180
"import-outside-toplevel",

versioneer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=
386386
assert isinstance(commands, list)
387387
p = None
388388
for c in commands:
389+
dispcmd = str([c] + args)
389390
try:
390-
dispcmd = str([c] + args)
391391
# remember shell=False, so use git.cmd on windows, not just git
392392
p = subprocess.Popen(
393393
[c] + args,

0 commit comments

Comments
 (0)