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 flake8-bugbear (B) rules #2825

Merged
merged 5 commits into from
Nov 25, 2023
Merged

Enable ruff's flake8-bugbear (B) rules #2825

merged 5 commits into from
Nov 25, 2023

Conversation

seisman
Copy link
Member

@seisman seisman commented Nov 21, 2023

Description of proposed changes

Related to #2741.

Enabling flake8-bugbear rules result in following errors and they look good to me. Actually, some of the errors were reported by pylint but not by ruff's pylint rules (see #2815).

ruff check pygmt doc/conf.py examples
examples/gallery/images/rgb_image.py:31:1: B018 Found useless expression. Either assign it to a variable or remove it.
   |
29 |     image = img.rio.clip_box(minx=738000, maxx=755000, miny=2300000, maxy=2318000)
30 |     image = image.load()  # Force loading the DataArray into memory
31 | image
   | ^^^^^ B018
32 |
33 | # %%
   |

pygmt/clib/conversion.py:106:13: B028 No explicit `stacklevel` keyword argument found
    |
104 |                 f"{coord_inc} is assumed in the '{dim}' dimension."
105 |             )
106 |             warnings.warn(msg, category=RuntimeWarning)
    |             ^^^^^^^^^^^^^ B028
107 |         if coord_inc == 0:
108 |             raise GMTInvalidInput(
    |

pygmt/clib/session.py:351:13: B018 Found useless expression. Either assign it to a variable or remove it.
    |
349 |         try:
350 |             # Won't raise an exception if there is a currently open session
351 |             self.session_pointer
    |             ^^^^^^^^^^^^^^^^^^^^ B018
352 |             # In this case, fail to create a new session until the old one is
353 |             # destroyed
    |

pygmt/tests/test_clib.py:59:5: B010 [*] Do not call `setattr` with a constant attribute value. It is not any safer than normal property access.
   |
57 |         return get_libgmt_func(name, argtypes, restype)
58 |
59 |     setattr(session, "get_libgmt_func", mock_get_libgmt_func)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B010
60 |
61 |     yield
   |
   = help: Replace `setattr` with assignment

pygmt/tests/test_clib.py:63:5: B010 [*] Do not call `setattr` with a constant attribute value. It is not any safer than normal property access.
   |
61 |     yield
62 |
63 |     setattr(session, "get_libgmt_func", get_libgmt_func)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B010
   |
   = help: Replace `setattr` with assignment

pygmt/tests/test_clib.py:97:13: B018 Found useless expression. Either assign it to a variable or remove it.
   |
95 |     for __ in range(2):
96 |         with pytest.raises(GMTCLibNoSessionError):
97 |             ses.session_pointer
   |             ^^^^^^^^^^^^^^^^^^^ B018
98 |         ses.create("session1")
99 |         assert ses.session_pointer is not None
   |

pygmt/tests/test_clib.py:102:13: B018 Found useless expression. Either assign it to a variable or remove it.
    |
100 |         ses.destroy()
101 |         with pytest.raises(GMTCLibNoSessionError):
102 |             ses.session_pointer
    |             ^^^^^^^^^^^^^^^^^^^ B018
    |

pygmt/tests/test_clib.py:186:9: B018 Found useless expression. Either assign it to a variable or remove it.
    |
184 |         lib.call_module("gmtdefaults", "")
185 |     with pytest.raises(GMTCLibNoSessionError):
186 |         lib.session_pointer
    |         ^^^^^^^^^^^^^^^^^^^ B018
    |

Found 8 errors.

Fixes #

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@seisman seisman added maintenance Boring but important stuff for the core devs needs review This PR has higher priority and needs review. labels Nov 21, 2023
@seisman seisman added this to the 0.11.0 milestone Nov 21, 2023
@michaelgrund michaelgrund added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Nov 24, 2023
@@ -28,7 +28,7 @@
# Subset to area of Lāhainā in EPSG:32604 coordinates
image = img.rio.clip_box(minx=738000, maxx=755000, miny=2300000, maxy=2318000)
image = image.load() # Force loading the DataArray into memory
image
print(image)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now shows the plaintext version of the xarray.DataArray instead of the repr - https://pygmt-dev--2825.org.readthedocs.build/en/2825/gallery/images/rgb_image.html#sphx-glr-gallery-images-rgb-image-py. Maybe try

Suggested change
print(image)
repr(image)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks worse now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fancy representation on this page (https://www.pygmt.org/dev/gallery/images/rgb_image.html#sphx-glr-gallery-images-rgb-image-py) is done by the xarray.DataArray's _repr_html_ method (https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display), which is different the repr function.

Our options:

  1. Use print(image)
  2. Completely remove imgae
  3. Ignore the error image # noqa: B018

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try Option 3 (noqa). Doesn't look nice, but at least the repr_html shows up.

image

Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
pygmt/clib/conversion.py Outdated Show resolved Hide resolved
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seisman seisman merged commit ac4e184 into main Nov 25, 2023
16 checks passed
@seisman seisman deleted the ruff/bugbear branch November 25, 2023 05:04
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Nov 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants