Skip to content

Commit

Permalink
style: Fix unnecessary-dunder-call (PLC2801) (OSGeo#4170)
Browse files Browse the repository at this point in the history
* style: Fix unnecessary-dunder-call (PLC2801)

Ruff rule: https://docs.astral.sh/ruff/rules/unnecessary-dunder-call/

12 instances fixed. Solves some reported issues by Pylint 3.2.6 through unnecessary-dunder-call / C2801

* style: Fix unnecessary-dunder-call (PLC2801) for __init__
4 instances fixed

* style: Fix unnecessary-dunder-call (PLC2801) using getattr
6 instances fixed

* Ignore PLC2801 in test suite file for preserving meaning of test during the change

* style: Fix unnecessary-dunder-call (PLC2801) for get and set item
3 instances fixed

* style: Fix unnecessary-dunder-call (PLC2801) for repr
1 instance fixed

* Ignore PLC2801 for __del__, as there seems to be some differences between both

* Update pyproject.toml to remove PLC2801 exclusion

* gui.wxpython.mapdisp.main: Remove next(self) from LayerList

* grass.pygrass.vector: Remove next(self) from vector class (old Python 2 iterator)

* Revert "grass.pygrass.vector: Remove next(self) from vector class (old Python 2 iterator)"

This reverts commit f79172c.

* Revert "gui.wxpython.mapdisp.main: Remove next(self) from LayerList"

This reverts commit 9963dd1.
  • Loading branch information
echoix authored Aug 19, 2024
1 parent 13e9a1b commit 2cf98da
Show file tree
Hide file tree
Showing 19 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gui/wxpython/gcp/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ def InitMapDisplay(self):
# initialize column sorter
self.itemDataMap = self.mapcoordlist
ncols = self.list.GetColumnCount()
ColumnSorterMixin.__init__(self, ncols)
ColumnSorterMixin(self, ncols)
# init to ascending sort on first click
self._colSortFlag = [1] * ncols

Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/gmodeler/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def RemoveShapes(self, shapes):
remList, upList = self.parent.GetModel().RemoveItem(shape)
shape.Select(False)
diagram.RemoveShape(shape)
shape.__del__()
shape.__del__() # noqa: PLC2801, C2801
for item in remList:
diagram.RemoveShape(item)
item.__del__()
item.__del__() # noqa: PLC2801, C2801

for item in upList:
item.Update()
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/gmodeler/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@ def _defineShape(self, width, height, x, y):
:param width, height: dimension of the shape
:param x, y: position of the shape
"""
ogl.EllipseShape.__init__(self, width, height)
ogl.EllipseShape(self, width, height)
if self.parent.GetCanvas():
self.SetCanvas(self.parent.GetCanvas())

Expand All @@ -1592,7 +1592,7 @@ def _defineShape(self, width, height, x, y):
:param width, height: dimension of the shape
:param x, y: position of the shape
"""
ogl.CompositeShape.__init__(self)
ogl.CompositeShape(self)
if self.parent.GetCanvas():
self.SetCanvas(self.parent.GetCanvas())

Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gmodeler/panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def GetOptData(self, dcmd, layer, params, propwin):
remList, upList = self.model.RemoveItem(data, layer)
for item in remList:
self.canvas.diagram.RemoveShape(item)
item.__del__()
item.__del__() # noqa: PLC2801, C2801

for item in upList:
item.Update()
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/image2target/ii2t_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ def InitMapDisplay(self):
# initialize column sorter
self.itemDataMap = self.mapcoordlist
ncols = self.list.GetColumnCount()
ColumnSorterMixin.__init__(self, ncols)
ColumnSorterMixin(self, ncols)
# init to ascending sort on first click
self._colSortFlag = [1] * ncols

Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/mapdisp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def __next__(self):
return result

def next(self):
return self.__next__()
return next(self)

def GetSelectedLayers(self, checkedOnly=True):
# hidden and selected vs checked and selected
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/photo2image/ip2i_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def InitMapDisplay(self):
# initialize column sorter
self.itemDataMap = self.mapcoordlist
ncols = self.list.GetColumnCount()
ColumnSorterMixin.__init__(self, ncols)
ColumnSorterMixin(self, ncols)
# init to ascending sort on first click
self._colSortFlag = [1] * ncols

Expand Down
5 changes: 4 additions & 1 deletion gui/wxpython/tplot/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ def onClose(self, evt):
if self._giface.GetMapDisplay():
self.coorval.OnClose()
self.cats.OnClose()
self.__del__()

# __del__() and del keyword seem to have differences,
# how can self.Destroy(), called after del, work otherwise
self.__del__() # noqa: PLC2801, C2801
self.Destroy()

def _layout(self):
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ ignore = [
"PLC0415", # import-outside-top-level
"PLC1901", # compare-to-empty-string
"PLC2701", # import-private-name
"PLC2801", # unnecessary-dunder-call
"PLE0704", # misplaced-bare-raise
"PLR0904", # too-many-public-methods
"PLR0911", # too-many-return-statements
Expand Down
2 changes: 1 addition & 1 deletion python/grass/gunittest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def safe_repr(obj, short=False):
try:
result = repr(obj)
except Exception:
result = object.__repr__(obj)
result = object.__repr__(obj) # noqa: PLC2801
if not short or len(result) < _MAX_LENGTH:
return result
return result[:_MAX_LENGTH] + " [truncated]..."
2 changes: 1 addition & 1 deletion python/grass/pygrass/gis/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def keys(self):

def items(self):
"""Return a list of tuple with key and value."""
return [(k, self.__getattribute__(k)) for k in self.keys()]
return [(k, getattr(self, k)) for k in self.keys()]

# ----------METHODS----------
def zoom(self, raster_name):
Expand Down
4 changes: 2 additions & 2 deletions python/grass/pygrass/modules/interface/typedict.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ def __reduce__(self):
def used(self):
key_dict = {}
for key in self:
if self.__getattr__(key):
key_dict[key] = self.__getattr__(key)
if getattr(self, key):
key_dict[key] = getattr(self, key)
return key_dict
4 changes: 2 additions & 2 deletions python/grass/pygrass/raster/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ def keys(self):
]

def items(self):
return [(k, self.__getattribute__(k)) for k in self.keys()]
return [(k, getattr(self, k)) for k in self.keys()]

def __iter__(self):
return ((k, self.__getattribute__(k)) for k in self.keys())
return ((k, getattr(self, k)) for k in self.keys())

def _repr_html_(self):
return dict2html(dict(self.items()), keys=self.keys(), border="1", kdec="b")
Expand Down
12 changes: 6 additions & 6 deletions python/grass/pygrass/raster/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ def __del__(self):
libraster.Rast_free_cats(ctypes.byref(self.c_cats))

def get_cat(self, index):
return self.__getitem__(index)
return self[index]

def set_cat(self, index, value):
if index is None:
self.append(value)
elif index < self.__len__():
self.__setitem__(index, value)
elif index < (len(self)):
self[index] = value
else:
raise TypeError("Index outside range.")

Expand All @@ -221,7 +221,7 @@ def _write_cats(self):
# reset only the C struct
libraster.Rast_init_cats("", ctypes.byref(self.c_cats))
# write to the c struct
for cat in self.__iter__():
for cat in iter(self):
label, min_cat, max_cat = cat
if max_cat is None:
max_cat = min_cat
Expand Down Expand Up @@ -273,7 +273,7 @@ def copy(self, category):
self._read_cats()

def ncats(self):
return self.__len__()
return len(self)

def set_cats_fmt(self, fmt, m1, a1, m2, a2):
"""Not implemented yet.
Expand Down Expand Up @@ -327,7 +327,7 @@ def write_rules(self, filename, sep=":"):
:param str sep: the separator used to divide values and category
"""
cats = []
for cat in self.__iter__():
for cat in iter(self):
if cat[-1] is None:
cat = cat[:-1]
cats.append(sep.join([str(i) for i in cat]))
Expand Down
2 changes: 1 addition & 1 deletion python/grass/pygrass/vector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __next__(self):

@must_be_open
def next(self):
return self.__next__()
return next(self)

@must_be_open
def rewind(self):
Expand Down
4 changes: 2 additions & 2 deletions python/grass/pygrass/vector/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def contains(self, point):
)

def items(self):
return [(k, self.__getattribute__(k)) for k in self.keys()]
return [(k, getattr(self, k)) for k in self.keys()]

def nsewtb(self, tb=True):
"""Return a list of values from bounding box
Expand Down Expand Up @@ -215,7 +215,7 @@ def append(self, box):
3
"""
indx = self.__len__()
indx = len(self)
libvect.Vect_boxlist_append(self.c_boxlist, indx, box.c_bbox)

# def extend(self, boxlist):
Expand Down
13 changes: 5 additions & 8 deletions python/grass/pygrass/vector/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ def pop(self, indx):
indx += self.c_points.contents.n_points
if indx >= self.c_points.contents.n_points:
raise IndexError("Index out of range")
pnt = self.__getitem__(indx)
pnt = self[indx]
libvect.Vect_line_delete_point(self.c_points, indx)
return pnt

Expand Down Expand Up @@ -1028,7 +1028,7 @@ def remove(self, pnt):
..
"""
for indx, point in enumerate(self.__iter__()):
for indx, point in enumerate(iter(self)):
if pnt == point:
libvect.Vect_line_delete_point(self.c_points, indx)
return
Expand Down Expand Up @@ -1086,7 +1086,7 @@ def to_list(self):
..
"""
return [pnt.coords() for pnt in self.__iter__()]
return [pnt.coords() for pnt in iter(self)]

def to_array(self):
"""Return an array of coordinates. ::
Expand All @@ -1112,10 +1112,7 @@ def to_wkt_p(self):
..
"""
return "LINESTRING(%s)" % ", ".join(
[
" ".join(["%f" % coord for coord in pnt.coords()])
for pnt in self.__iter__()
]
[" ".join(["%f" % coord for coord in pnt.coords()]) for pnt in iter(self)]
)

def from_wkt(self, wkt):
Expand Down Expand Up @@ -1592,7 +1589,7 @@ def isles_ids(self):
"""Return the id of isles"""
return [
libvect.Vect_get_area_isle(self.c_mapinfo, self.area_id, i)
for i in range(self.__len__())
for i in range(len(self))
]

@mapinfo_must_be_set
Expand Down
2 changes: 1 addition & 1 deletion python/grass/pygrass/vector/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def update_odict(self):
[
"?",
]
* self.__len__()
* (len(self))
)
kv = ",".join(["%s=?" % k for k in self.odict.keys() if k != self.key])
where = "%s=?" % self.key
Expand Down
6 changes: 3 additions & 3 deletions python/grass/pygrass/vector/testsuite/test_geometry_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def test_setitem(self):
newvalue = 100.0
newpairs = ("setitem_point_2", 1000.0)

self.attrs.__setitem__("name", newname)
self.attrs.__setitem__("name", newname) # noqa: PLC2801
self.assertEqual(self.attrs["name"], newname)
self.attrs.__setitem__("value", newvalue)
self.attrs.__setitem__("value", newvalue) # noqa: PLC2801
self.assertEqual(self.attrs["value"], newvalue)
self.attrs.__setitem__(("name", "value"), newpairs)
self.attrs.__setitem__(("name", "value"), newpairs) # noqa: PLC2801
self.assertEqual(self.attrs["name", "value"], newpairs)


Expand Down

0 comments on commit 2cf98da

Please sign in to comment.