GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Enable ruff's literal-membership (PLR6201) rule and fix violations by weiji14 · Pull Request #3317 · GenericMappingTools/pygmt

Conversation

@weiji14

@weiji14 weiji14 commented Jul 8, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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:

  • https://docs.python.org/3/whatsnew/3.2.html#optimizations
  • https://docs.astral.sh/ruff/rules/literal-membership/
  • https://docs.astral.sh/ruff/preview/#selecting-single-preview-rules

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.

All reactions
weiji14 added 2 commits July 8, 2024 13:11
@weiji14
Enable ruff's literal-membership (PLR6201) rule
024b069
Xref https://docs.astral.sh/ruff/rules/literal-membership.
@weiji14
Fix PLR6201 violations
b9dfc89
@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
@weiji14
Fix PT001 violation due to preview mode setting
a50641c
Xref https://docs.astral.sh/ruff/settings/#lint_flake8-pytest-style_fixture-parentheses and astral-sh/ruff#12106.
weiji14
weiji14 commented Jul 8, 2024
View reviewed changes
Comment thread pygmt/tests/test_clib_loading.py
return self.loaded_libgmt

@pytest.fixture()
@pytest.fixture

@weiji14 weiji14 Jul 8, 2024

Copy link
Copy Markdown
Member Author

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.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@weiji14
weiji14 marked this pull request as ready for review July 8, 2024 01:40
@seisman

seisman commented Jul 8, 2024

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weiji14
weiji14 commented Jul 8, 2024
View reviewed changes
Comment thread 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"}:

@weiji14 weiji14 Jul 8, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

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
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"}:

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@weiji14
Check geodataframe column dtype's str name instead of class object
c1e6dd5
seisman
seisman approved these changes Jul 8, 2024
View reviewed changes
@weiji14
weiji14 merged commit 0f31b7a into main Jul 8, 2024
@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

Reviewers

@seisman seisman seisman approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@weiji14 weiji14

Labels

maintenance Boring but important stuff for the core devs

Projects

None yet

Milestone

0.13.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@weiji14 @seisman
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.