Enable ruff's literal-membership (PLR6201) rule and fix violations#3317
Enable ruff's literal-membership (PLR6201) rule and fix violations#3317
Conversation
| return self.loaded_libgmt | ||
|
|
||
| @pytest.fixture() | ||
| @pytest.fixture |
There was a problem hiding this comment.
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.
|
Three geopandas tests start to fail, but not sure if it's related to changes in this PR. |
pygmt/helpers/tempfile.py
Outdated
| # 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"}: |
There was a problem hiding this comment.
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 |
|---|---|---|
![]() |
![]() |
![]() |
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:
| if geojson[col].dtype in {"int", "int64", "Int64"}: | |
| if geojson[col].dtype.name in {"int", "int64", "Int64"}: |



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[]:References:
Addresses #2741 (comment)
Reminders
make formatandmake checkto make sure the code follows the style guide.doc/api/index.rst.Slash Commands
You can write slash commands (
/command) in the first line of a comment to performspecific operations. Supported slash command is:
/format: automatically format and lint the code