Skip to content

Commit 22de537

Browse files
authored
STYLE: fixing linting issues (redefined-outer-name) (#49762)
* fix: fixing linting issues Fixing pylint issues in xml.py, style.py, and _json.py * fix: fixing issue with some failing test cases Fixing the PR checks failing test cases for my PR * fix: fixing styling issues and PR comments * fix: removing some code not necessary for PR * fx: updating my code to be better formatted
1 parent a6d3e13 commit 22de537

File tree

3 files changed

+62
-37
lines changed

3 files changed

+62
-37
lines changed

pandas/io/formats/style.py

+53-28
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,24 @@ def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
9191
####
9292
# Shared Doc Strings
9393

94-
subset = """subset : label, array-like, IndexSlice, optional
94+
subset_args = """subset : label, array-like, IndexSlice, optional
9595
A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
9696
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
9797
prioritised, to limit ``data`` to *before* applying the function."""
9898

99-
props = """props : str, default None
99+
properties_args = """props : str, default None
100100
CSS properties to use for highlighting. If ``props`` is given, ``color``
101101
is not used."""
102102

103-
color = """color : str, default '{default}'
103+
coloring_args = """color : str, default '{default}'
104104
Background color to use for highlighting."""
105105

106-
buf = """buf : str, path object, file-like object, optional
106+
buffering_args = """buf : str, path object, file-like object, optional
107107
String, path object (implementing ``os.PathLike[str]``), or file-like
108108
object implementing a string ``write()`` function. If ``None``, the result is
109109
returned as a string."""
110110

111-
encoding = """encoding : str, optional
111+
encoding_args = """encoding : str, optional
112112
Character encoding setting for file output (and meta tags if available).
113113
Defaults to ``pandas.options.styler.render.encoding`` value of "utf-8"."""
114114

@@ -1186,7 +1186,7 @@ def to_html(
11861186
) -> str:
11871187
...
11881188

1189-
@Substitution(buf=buf, encoding=encoding)
1189+
@Substitution(buf=buffering_args, encoding=encoding_args)
11901190
def to_html(
11911191
self,
11921192
buf: FilePath | WriteBuffer[str] | None = None,
@@ -1345,7 +1345,7 @@ def to_string(
13451345
) -> str:
13461346
...
13471347

1348-
@Substitution(buf=buf, encoding=encoding)
1348+
@Substitution(buf=buffering_args, encoding=encoding_args)
13491349
def to_string(
13501350
self,
13511351
buf: FilePath | WriteBuffer[str] | None = None,
@@ -1690,7 +1690,7 @@ def _apply(
16901690
self._update_ctx(result)
16911691
return self
16921692

1693-
@Substitution(subset=subset)
1693+
@Substitution(subset=subset_args)
16941694
def apply(
16951695
self,
16961696
func: Callable,
@@ -1929,7 +1929,7 @@ def _applymap(
19291929
self._update_ctx(result)
19301930
return self
19311931

1932-
@Substitution(subset=subset)
1932+
@Substitution(subset=subset_args)
19331933
def applymap(
19341934
self, func: Callable, subset: Subset | None = None, **kwargs
19351935
) -> Styler:
@@ -2685,7 +2685,7 @@ def _get_numeric_subset_default(self):
26852685
visibility across varying background colors. All text is dark if 0, and\n
26862686
light if 1, defaults to 0.408.""",
26872687
)
2688-
@Substitution(subset=subset)
2688+
@Substitution(subset=subset_args)
26892689
def background_gradient(
26902690
self,
26912691
cmap: str | Colormap = "PuBu",
@@ -2868,7 +2868,7 @@ def text_gradient(
28682868
text_only=True,
28692869
)
28702870

2871-
@Substitution(subset=subset)
2871+
@Substitution(subset=subset_args)
28722872
def set_properties(self, subset: Subset | None = None, **kwargs) -> Styler:
28732873
"""
28742874
Set defined CSS-properties to each ``<td>`` HTML element for the given subset.
@@ -2900,7 +2900,7 @@ def set_properties(self, subset: Subset | None = None, **kwargs) -> Styler:
29002900
values = "".join([f"{p}: {v};" for p, v in kwargs.items()])
29012901
return self.applymap(lambda x: values, subset=subset)
29022902

2903-
@Substitution(subset=subset)
2903+
@Substitution(subset=subset_args)
29042904
def bar( # pylint: disable=disallowed-name
29052905
self,
29062906
subset: Subset | None = None,
@@ -3023,7 +3023,11 @@ def bar( # pylint: disable=disallowed-name
30233023

30243024
return self
30253025

3026-
@Substitution(subset=subset, props=props, color=color.format(default="red"))
3026+
@Substitution(
3027+
subset=subset_args,
3028+
props=properties_args,
3029+
color=coloring_args.format(default="red"),
3030+
)
30273031
def highlight_null(
30283032
self,
30293033
color: str = "red",
@@ -3066,7 +3070,11 @@ def f(data: DataFrame, props: str) -> np.ndarray:
30663070
props = f"background-color: {color};"
30673071
return self.apply(f, axis=None, subset=subset, props=props)
30683072

3069-
@Substitution(subset=subset, color=color.format(default="yellow"), props=props)
3073+
@Substitution(
3074+
subset=subset_args,
3075+
color=coloring_args.format(default="yellow"),
3076+
props=properties_args,
3077+
)
30703078
def highlight_max(
30713079
self,
30723080
subset: Subset | None = None,
@@ -3110,7 +3118,11 @@ def highlight_max(
31103118
props=props,
31113119
)
31123120

3113-
@Substitution(subset=subset, color=color.format(default="yellow"), props=props)
3121+
@Substitution(
3122+
subset=subset_args,
3123+
color=coloring_args.format(default="yellow"),
3124+
props=properties_args,
3125+
)
31143126
def highlight_min(
31153127
self,
31163128
subset: Subset | None = None,
@@ -3154,7 +3166,11 @@ def highlight_min(
31543166
props=props,
31553167
)
31563168

3157-
@Substitution(subset=subset, color=color.format(default="yellow"), props=props)
3169+
@Substitution(
3170+
subset=subset_args,
3171+
color=coloring_args.format(default="yellow"),
3172+
props=properties_args,
3173+
)
31583174
def highlight_between(
31593175
self,
31603176
subset: Subset | None = None,
@@ -3258,7 +3274,11 @@ def highlight_between(
32583274
inclusive=inclusive,
32593275
)
32603276

3261-
@Substitution(subset=subset, color=color.format(default="yellow"), props=props)
3277+
@Substitution(
3278+
subset=subset_args,
3279+
color=coloring_args.format(default="yellow"),
3280+
props=properties_args,
3281+
)
32623282
def highlight_quantile(
32633283
self,
32643284
subset: Subset | None = None,
@@ -3597,17 +3617,19 @@ def _background_gradient(
35973617
else: # else validate gmap against the underlying data
35983618
gmap = _validate_apply_axis_arg(gmap, "gmap", float, data)
35993619

3600-
with _mpl(Styler.background_gradient) as (plt, mpl):
3620+
with _mpl(Styler.background_gradient) as (_, _matplotlib):
36013621
smin = np.nanmin(gmap) if vmin is None else vmin
36023622
smax = np.nanmax(gmap) if vmax is None else vmax
36033623
rng = smax - smin
36043624
# extend lower / upper bounds, compresses color range
3605-
norm = mpl.colors.Normalize(smin - (rng * low), smax + (rng * high))
3625+
norm = _matplotlib.colors.Normalize(smin - (rng * low), smax + (rng * high))
36063626

36073627
if cmap is None:
3608-
rgbas = mpl.colormaps[mpl.rcParams["image.cmap"]](norm(gmap))
3628+
rgbas = _matplotlib.colormaps[_matplotlib.rcParams["image.cmap"]](
3629+
norm(gmap)
3630+
)
36093631
else:
3610-
rgbas = mpl.colormaps.get_cmap(cmap)(norm(gmap))
3632+
rgbas = _matplotlib.colormaps.get_cmap(cmap)(norm(gmap))
36113633

36123634
def relative_luminance(rgba) -> float:
36133635
"""
@@ -3636,10 +3658,11 @@ def css(rgba, text_only) -> str:
36363658
dark = relative_luminance(rgba) < text_color_threshold
36373659
text_color = "#f1f1f1" if dark else "#000000"
36383660
return (
3639-
f"background-color: {mpl.colors.rgb2hex(rgba)};color: {text_color};"
3661+
f"background-color: {_matplotlib.colors.rgb2hex(rgba)};"
3662+
+ f"color: {text_color};"
36403663
)
36413664
else:
3642-
return f"color: {mpl.colors.rgb2hex(rgba)};"
3665+
return f"color: {_matplotlib.colors.rgb2hex(rgba)};"
36433666

36443667
if data.ndim == 1:
36453668
return [css(rgba, text_only) for rgba in rgbas]
@@ -3885,18 +3908,20 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple
38853908
rgbas = None
38863909
if cmap is not None:
38873910
# use the matplotlib colormap input
3888-
with _mpl(Styler.bar) as (plt, mpl):
3911+
with _mpl(Styler.bar) as (_, _matplotlib):
38893912
cmap = (
3890-
mpl.colormaps[cmap]
3913+
_matplotlib.colormaps[cmap]
38913914
if isinstance(cmap, str)
38923915
else cmap # assumed to be a Colormap instance as documented
38933916
)
3894-
norm = mpl.colors.Normalize(left, right)
3917+
norm = _matplotlib.colors.Normalize(left, right)
38953918
rgbas = cmap(norm(values))
38963919
if data.ndim == 1:
3897-
rgbas = [mpl.colors.rgb2hex(rgba) for rgba in rgbas]
3920+
rgbas = [_matplotlib.colors.rgb2hex(rgba) for rgba in rgbas]
38983921
else:
3899-
rgbas = [[mpl.colors.rgb2hex(rgba) for rgba in row] for row in rgbas]
3922+
rgbas = [
3923+
[_matplotlib.colors.rgb2hex(rgba) for rgba in row] for row in rgbas
3924+
]
39003925

39013926
assert isinstance(align, str) # mypy: should now be in [left, right, mid, zero]
39023927
if data.ndim == 1:

pandas/io/json/_json.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
import numpy as np
2323

24-
from pandas._libs import json
24+
from pandas._libs.json import (
25+
dumps,
26+
loads,
27+
)
2528
from pandas._libs.tslibs import iNaT
2629
from pandas._typing import (
2730
CompressionOptions,
@@ -73,9 +76,6 @@
7376

7477
FrameSeriesStrT = TypeVar("FrameSeriesStrT", bound=Literal["frame", "series"])
7578

76-
loads = json.loads
77-
dumps = json.dumps
78-
7979

8080
# interface to/from
8181
@overload

pandas/io/xml.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,9 @@ def _parse_doc(
533533

534534
with preprocess_data(handle_data) as xml_data:
535535
curr_parser = XMLParser(encoding=self.encoding)
536-
doc = parse(xml_data, parser=curr_parser)
536+
document = parse(xml_data, parser=curr_parser)
537537

538-
return doc.getroot()
538+
return document.getroot()
539539

540540

541541
class _LxmlFrameParser(_XMLFrameParser):
@@ -648,13 +648,13 @@ def _parse_doc(
648648
"Can not pass encoding None when input is StringIO."
649649
)
650650

651-
doc = fromstring(
651+
document = fromstring(
652652
xml_data.getvalue().encode(self.encoding), parser=curr_parser
653653
)
654654
else:
655-
doc = parse(xml_data, parser=curr_parser)
655+
document = parse(xml_data, parser=curr_parser)
656656

657-
return doc
657+
return document
658658

659659
def _transform_doc(self) -> _XSLTResultTree:
660660
"""

0 commit comments

Comments
 (0)