GeezerVison is showing github.com — Enable ruff's literal-membership (PLR6201) rule and fix violations by weiji14 · Pull Request #3317 · GenericMappingTools/pygmt
Conversation
weiji14
commented
Jul 8, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
return self.loaded_libgmt
|
|
|
|
|
|
@pytest.fixture()
|
|
|
@pytest.fixture
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Three geopandas tests start to fail, but not sure if it's related to changes in this PR.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
# 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"}:
|
weiji14
Jul 8, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 |
 |
 |
 |
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"}: |
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
seisman
seisman approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Labels
maintenance
Boring but important stuff for the core devs
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|