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 literal-membership (PLR6201) rule and fix violations #3317

Merged
merged 4 commits into from
Jul 8, 2024

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Jul 8, 2024

Description of proposed changes

Enable ruff's literal membership (PLR6201) rule to ensure a set literal is used when testing for membership. Note that this is a preview mode feature.

This offers a bit of a speed/performance improvement, as Python's peephole optimizer will optimize set membership tests (by converting the set to a frozenset), making it faster than tuple or list membership tests.

Example speed tests on my machine, about a 4.5x speedup using set {} compared to tuple () or list []:

$ python -m timeit -s 'convention="t"' 'convention in {"a", "c", "m", "d", "z", "p", "x", "y", "t"}'
50000000 loops, best of 5: 9.52 nsec per loop

$ python -m timeit -s 'convention="t"' 'convention in ("a", "c", "m", "d", "z", "p", "x", "y", "t")'
5000000 loops, best of 5: 42.4 nsec per loop

$ python -m timeit -s 'convention="t"' 'convention in ["a", "c", "m", "d", "z", "p", "x", "y", "t"]'
5000000 loops, best of 5: 42.4 nsec per loop

References:

Addresses #2741 (comment)

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 command is:

  • /format: automatically format and lint the code

@weiji14 weiji14 added the maintenance Boring but important stuff for the core devs label Jul 8, 2024
@weiji14 weiji14 added this to the 0.13.0 milestone Jul 8, 2024
@weiji14 weiji14 self-assigned this Jul 8, 2024
@weiji14 weiji14 added the run/benchmark Trigger the benchmark workflow in PRs label Jul 8, 2024
@@ -131,7 +131,7 @@ def _mock_ctypes_cdll_return(self, libname):
# libname is a loaded GMT library
return self.loaded_libgmt

@pytest.fixture()
@pytest.fixture
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that this change is to fix a PT001 rule violation that was triggered because of the preview mode setting. In non-preview mode, @pytest.fixture() is valid, but in preview mode, @pytest.fixture() is valid. See https://docs.astral.sh/ruff/rules/pytest-fixture-incorrect-parentheses-style/ and astral-sh/ruff#12106.

@weiji14 weiji14 marked this pull request as ready for review July 8, 2024 01:40
@seisman
Copy link
Member

seisman commented Jul 8, 2024

Three geopandas tests start to fail, but not sure if it's related to changes in this PR.

overflow = geojson[col].abs().max() > 2**31 - 1
schema["properties"][col] = "float" if overflow else "int32"
ogrgmt_kwargs["schema"] = schema
else: # GeoPandas v1.x.
# The default engine "pyogrio" doesn't support the 'schema' parameter
# but we can change the dtype directly.
for col in geojson.columns:
if geojson[col].dtype in ("int", "int64", "Int64"):
if geojson[col].dtype in {"int", "int64", "Int64"}:
Copy link
Member Author

@weiji14 weiji14 Jul 8, 2024

Choose a reason for hiding this comment

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

Three geopandas tests start to fail, but not sure if it's related to changes in this PR.

Interesting, I can reproduce the geopandas test failures locally, and it seems to be due to the change from () to {} here on this line. Example test failure on pygmt.tests.test_geopandas.test_geopandas_plot_int64_as_float:

baseline (correct) result (incorrect) diff
baseline result result-failed-diff

It seems to be because geojson[col].dtype is a class instance like numpy.dtypes.Int64DType rather than a str, and that might be causing some issues with the membership test?

The fix is straightforward though:

Suggested change
if geojson[col].dtype in {"int", "int64", "Int64"}:
if geojson[col].dtype.name in {"int", "int64", "Int64"}:

@weiji14 weiji14 merged commit 0f31b7a into main Jul 8, 2024
20 checks passed
@weiji14 weiji14 deleted the ruff/literal-membership branch July 8, 2024 07:41
@weiji14 weiji14 removed the run/benchmark Trigger the benchmark workflow in PRs label Jul 8, 2024
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.

2 participants