Skip to content
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

Enable ruff's pylint rules (PL) and remove pylint #2815

Merged
merged 11 commits into from
Nov 21, 2023
5 changes: 1 addition & 4 deletions .github/workflows/style_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ jobs:

- name: Install packages
run: |
python -m pip install blackdoc docformatter pylint ruff
python -m pip install blackdoc docformatter ruff
python -m pip list
sudo apt-get install dos2unix

- name: Formatting check (blackdoc, docformatter, ruff)
run: make check

- name: Linting (pylint)
run: make lint

- name: Ensure files use UNIX line breaks and have 644 permission
run: |
find . -type f -not -path '*/\.git/*' -exec grep -Iq . {} \; -exec dos2unix --quiet {} \;
Expand Down
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ help:
@echo " format run blackdoc, docformatter and ruff to automatically format the code"
@echo " check run code style and quality checks (blackdoc, docformatter, ruff)"
@echo " codespell run codespell to check common misspellings"
@echo " lint run pylint for a deeper (and slower) quality check"
@echo " clean clean up build and generated files"
@echo " distclean clean up build and generated files, including project metadata files"
@echo ""
Expand Down Expand Up @@ -74,9 +73,6 @@ check:
codespell:
@codespell

lint:
pylint $(LINT_FILES)

clean:
find . -name "*.pyc" -exec rm -v {} +
find . -name "*~" -exec rm -v {} +
Expand Down
9 changes: 2 additions & 7 deletions doc/conf.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
"""
Sphinx documentation configuration file.
"""
# pylint: disable=invalid-name

import datetime
from importlib.metadata import metadata

# ruff: isort: off
from sphinx_gallery.sorting import ( # pylint: disable=no-name-in-module
ExplicitOrder,
ExampleTitleSortKey,
)
from sphinx_gallery.sorting import ExplicitOrder, ExampleTitleSortKey
import pygmt
from pygmt import __commit__, __version__
from pygmt.sphinx_gallery import PyGMTScraper
Expand Down Expand Up @@ -139,7 +134,7 @@
# General information about the project
year = datetime.date.today().year
project = "PyGMT"
copyright = f"2017-{year}, The PyGMT Developers" # pylint: disable=redefined-builtin
copyright = f"2017-{year}, The PyGMT Developers"
if len(__version__.split("+")) > 1 or __version__ == "unknown":
version = "dev"
# Set base_url for stable version
Expand Down
9 changes: 4 additions & 5 deletions doc/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,14 @@ editors or IDEs. We consistently use `# %%` as code block separators (please
refer to [issue #2660](https://github.com/GenericMappingTools/pygmt/issues/2660)
for the discussions) and require at least one separator in all example files.

We also use [ruff](https://docs.astral.sh/ruff) and
[pylint](https://pylint.pycqa.org/) to check the quality of the code and quickly catch
common errors.
We also use [ruff](https://docs.astral.sh/ruff) to check the quality of the code
and quickly catch common errors.

The [`Makefile`](https://github.com/GenericMappingTools/pygmt/blob/main/Makefile)
contains rules for running both checks:
contains rules for running the linter checks:

```bash
make check # Runs blackdoc, docformatter, ruff (in check mode)
make lint # Runs pylint, which is a bit slower
```

### Testing your Code
Expand Down
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies:
- blackdoc
- codespell
- docformatter>=1.7.2
- pylint
- ruff
# Dev dependencies (unit testing)
- matplotlib
Expand Down
4 changes: 2 additions & 2 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def print_clib_info(file=sys.stdout):
Includes the GMT version, default values for parameters, the path to the
``libgmt`` shared library, and GMT directories.
"""
from pygmt.clib import Session # pylint: disable=import-outside-toplevel
from pygmt.clib import Session

lines = ["GMT library information:"]
with Session() as ses:
Expand All @@ -104,7 +104,7 @@ def show_versions(file=sys.stdout):
- Core dependency versions (NumPy, Pandas, Xarray, etc)
- GMT library information
"""
# pylint: disable=import-outside-toplevel

import importlib
import platform
import subprocess
Expand Down
1 change: 0 additions & 1 deletion pygmt/clib/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ def check_libgmt(libgmt):
functions = ["Create_Session", "Get_Enum", "Call_Module", "Destroy_Session"]
for func in functions:
if not hasattr(libgmt, "GMT_" + func):
# pylint: disable=protected-access
msg = (
f"Error loading '{libgmt._name}'. Couldn't access function GMT_{func}. "
"Ensure that you have installed an up-to-date GMT version 6 library. "
Expand Down
12 changes: 5 additions & 7 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def info(self):
Dictionary with the GMT version and default paths and parameters.
"""
if not hasattr(self, "_info"):
self._info = { # pylint: disable=attribute-defined-outside-init
self._info = {
"version": self.get_default("API_VERSION"),
"padding": self.get_default("API_PAD"),
# API_BINDIR points to the directory of the Python interpreter
Expand Down Expand Up @@ -312,7 +312,6 @@ def get_libgmt_func(self, name, argtypes=None, restype=None):
<class 'ctypes.CDLL.__init__.<locals>._FuncPtr'>
"""
if not hasattr(self, "_libgmt"):
# pylint: disable=attribute-defined-outside-init
self._libgmt = load_libgmt()
function = getattr(self._libgmt, name)
if argtypes is not None:
Expand Down Expand Up @@ -349,7 +348,7 @@ def create(self, name):
"""
try:
# Won't raise an exception if there is a currently open session
self.session_pointer # pylint: disable=pointless-statement
self.session_pointer
# In this case, fail to create a new session until the old one is
# destroyed
raise GMTCLibError(
Expand All @@ -369,10 +368,10 @@ def create(self, name):

# Capture the output printed by GMT into this list. Will use it later
# to generate error messages for the exceptions raised by API calls.
self._error_log = [] # pylint: disable=attribute-defined-outside-init
self._error_log = []

@ctp.CFUNCTYPE(ctp.c_int, ctp.c_void_p, ctp.c_char_p)
def print_func(file_pointer, message): # pylint: disable=unused-argument
def print_func(file_pointer, message):
"""
Callback function that the GMT C API will use to print log and
error messages.
Expand All @@ -389,7 +388,6 @@ def print_func(file_pointer, message): # pylint: disable=unused-argument

# Need to store a copy of the function because ctypes doesn't and it
# will be garbage collected otherwise
# pylint: disable=attribute-defined-outside-init
self._print_callback = print_func

padding = self["GMT_PAD_DEFAULT"]
Expand Down Expand Up @@ -1468,7 +1466,7 @@ def virtualfile_from_grid(self, grid):
yield vfile

@fmt_docstring
def virtualfile_from_data(
def virtualfile_from_data( # noqa: PLR0912
self,
check_kind=None,
data=None,
Expand Down
1 change: 0 additions & 1 deletion pygmt/datasets/tile_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
* y (y) float64 -7.081e-10 -7.858e+04 ... -1.996e+07 ...
* x (x) float64 -2.004e+07 -1.996e+07 ... 1.996e+07 2.004e+07
"""
# pylint: disable=too-many-locals
if contextily is None:
raise ImportError(
"Package `contextily` is required to be installed to use this function. "
Expand Down
14 changes: 5 additions & 9 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
try:
import IPython
except ImportError:
IPython = None # pylint: disable=invalid-name

IPython = None

from pygmt.clib import Session
from pygmt.exceptions import GMTError, GMTInvalidInput
Expand All @@ -34,7 +33,7 @@

# Show figures in Jupyter notebooks if available
if IPython:
get_ipython = IPython.get_ipython() # pylint: disable=invalid-name
get_ipython = IPython.get_ipython()
if get_ipython and "IPKernelApp" in get_ipython.config: # Jupyter Notebook enabled
SHOW_CONFIG["method"] = "notebook"

Expand Down Expand Up @@ -82,9 +81,7 @@ class Figure:

def __init__(self):
self._name = unique_name()
self._preview_dir = TemporaryDirectory( # pylint: disable=consider-using-with
prefix=f"{self._name}-preview-"
)
self._preview_dir = TemporaryDirectory(prefix=f"{self._name}-preview-")
self._activate_figure()

def __del__(self):
Expand Down Expand Up @@ -251,7 +248,7 @@ def psconvert(self, **kwargs):
module="psconvert", args=f"{prefix_arg} {build_arg_string(kwargs)}"
)

def savefig(
def savefig( # noqa: PLR0912
self,
fname,
transparent=False,
Expand Down Expand Up @@ -320,7 +317,6 @@ def savefig(
:meth:`pygmt.Figure.psconvert`. Valid parameters are ``gs_path``,
``gs_option``, ``resize``, ``bb_style``, and ``verbose``.
"""
# pylint: disable=too-many-branches
# All supported formats
fmts = {
"bmp": "b",
Expand Down Expand Up @@ -518,7 +514,7 @@ def _repr_html_(self):
html = '<img src="data:image/png;base64,{image}" width="{width}px">'
return html.format(image=base64_png.decode("utf-8"), width=500)

from pygmt.src import ( # pylint: disable=import-outside-toplevel
from pygmt.src import (
basemap,
coast,
colorbar,
Expand Down
1 change: 0 additions & 1 deletion pygmt/helpers/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ def tempfile_from_geojson(geojson):
E.g. '1a2b3c4d5e6.gmt'.
"""
with GMTTempFile(suffix=".gmt") as tmpfile:
# pylint: disable=import-outside-toplevel
import geopandas as gpd

os.remove(tmpfile.name) # ensure file is deleted first
Expand Down
2 changes: 0 additions & 2 deletions pygmt/helpers/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,10 @@ def check_figures_equal(*, extensions=("png",), tol=0.0, result_dir="result_imag
...
>>> shutil.rmtree(path="tmp_result_images") # cleanup folder if tests pass
"""
# pylint: disable=invalid-name
ALLOWED_CHARS = set(string.digits + string.ascii_letters + "_-[]()")
KEYWORD_ONLY = inspect.Parameter.KEYWORD_ONLY

def decorator(func):
# pylint: disable=import-outside-toplevel
import pytest
from matplotlib.testing.compare import compare_images

Expand Down
2 changes: 1 addition & 1 deletion pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@
elif os_name == "darwin": # Darwin is macOS
subprocess.run(["open", fname], check=False, **run_args)
elif os_name == "win32":
os.startfile(fname) # pylint: disable=no-member
os.startfile(fname)

Check warning on line 502 in pygmt/helpers/utils.py

View check run for this annotation

Codecov / codecov/patch

pygmt/helpers/utils.py#L502

Added line #L502 was not covered by tests
else:
webbrowser.open_new_tab(f"file://{fname}")
if waiting > 0:
Expand Down
2 changes: 1 addition & 1 deletion pygmt/sphinx_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pygmt.figure import SHOWED_FIGURES


class PyGMTScraper: # pylint: disable=too-few-public-methods
class PyGMTScraper:
"""
Save ``pygmt.Figure`` objects that had their ``show`` method called.

Expand Down
2 changes: 0 additions & 2 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
Source code for PyGMT methods.
"""
# pylint: disable=import-outside-toplevel

from pygmt.src.basemap import basemap
from pygmt.src.binstats import binstats
from pygmt.src.blockm import blockmean, blockmedian, blockmode
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/basemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ def basemap(self, **kwargs):
{perspective}
{transparency}
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)
with Session() as lib:
lib.call_module(module="basemap", args=build_arg_string(kwargs))
2 changes: 1 addition & 1 deletion pygmt/src/coast.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def coast(self, **kwargs):
>>> # Show the plot
>>> fig.show()
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)
if not args_in_kwargs(args=["C", "G", "S", "I", "N", "E", "Q", "W"], kwargs=kwargs):
raise GMTInvalidInput(
"""At least one of the following parameters must be specified:
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ def colorbar(self, **kwargs):
>>> # Show the plot
>>> fig.show()
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)
with Session() as lib:
lib.call_module(module="colorbar", args=build_arg_string(kwargs))
2 changes: 1 addition & 1 deletion pygmt/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pygmt.clib import Session


class config: # pylint: disable=invalid-name
class config:
"""
Set GMT defaults globally or locally.

Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):
{perspective}
{transparency}
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)

with Session() as lib:
file_context = lib.virtualfile_from_data(
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/grdcontour.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def grdcontour(self, grid, **kwargs):
>>> # show the plot
>>> fig.show()
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as fname:
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/grdhisteq.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
__doctest_skip__ = ["grdhisteq.*"]


class grdhisteq: # pylint: disable=invalid-name
class grdhisteq:
r"""
Perform histogram equalization for a grid.

Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/grdimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def grdimage(self, grid, **kwargs):
>>> # show the plot
>>> fig.show()
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)

with Session() as lib:
with lib.virtualfile_from_data(
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/grdview.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def grdview(self, grid, **kwargs):
>>> # show the plot
>>> fig.show()
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)
with Session() as lib:
with lib.virtualfile_from_data(
check_kind="raster", data=grid
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def histogram(self, data, **kwargs):
{transparency}
{wrap}
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="vector", data=data)
with file_context as infile:
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ def image(self, imagefile, **kwargs):
{perspective}
{transparency}
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)
with Session() as lib:
lib.call_module(module="image", args=build_arg_string(kwargs, infile=imagefile))
2 changes: 1 addition & 1 deletion pygmt/src/inset.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def inset(self, **kwargs):
>>> fig.logo(position="jBR+o0.2c+w3c")
>>> fig.show()
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)
with Session() as lib:
try:
lib.call_module(module="inset", args=f"begin {build_arg_string(kwargs)}")
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwarg
{perspective}
{transparency}
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
kwargs = self._preprocess(**kwargs)

if kwargs.get("D") is None:
kwargs["D"] = position
Expand Down
Loading