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 preview #1045

Merged
merged 1 commit into from Nov 19, 2023
Merged

Enable Ruff preview #1045

merged 1 commit into from Nov 19, 2023

Conversation

ofek
Copy link
Sponsor Collaborator

@ofek ofek commented Nov 19, 2023

No description provided.

@ofek ofek merged commit 977f882 into master Nov 19, 2023
33 checks passed
@ofek ofek deleted the l branch November 19, 2023 00:34
Copy link
Contributor

Code Coverage

Package Statements
hatch 96.53% (4732 / 4902)
hatchling 97.66% (3725 / 3814)
tests 99.97% (14241 / 14245)
Summary 98.85% (22698 / 22961)

github-actions bot pushed a commit that referenced this pull request Nov 19, 2023
@@ -15,7 +15,7 @@ def get_files(**kwargs):
if str(f.path) == 'LICENSE.txt':
files.append(File(Path(metadata_directory, 'licenses', f.path), f.contents))

if f.path.parts[0] not in (kwargs['package_name'], 'tests'):
if f.path.parts[0] not in {kwargs['package_name'], 'tests'}:
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a cool lint but I think it actively harms performance. In the vast vast vast majority of cases, membership checks are much faster against a tuple literal vs a set literal.

  1. A tuple literal is often optimizable to a global singleton and so has 0 construction cost. A set literal is quite expensive to construct every time.

  2. Regardless of it being a literal, linearly checking against <5 members is faster than hashing the item and then checking against it in a hashmap.

Copy link
Sponsor Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Sponsor Collaborator Author

@ofek ofek Nov 19, 2023

Choose a reason for hiding this comment

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

I'm glad someone else thinks the way I do! Do you think I should revert this particular one? I would like to.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think just don't apply this lint and move on. There are many questionable lints 😃

Copy link
Sponsor Collaborator Author

Choose a reason for hiding this comment

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

Will do! Which others are you not fond of?

Copy link
Contributor

Choose a reason for hiding this comment

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

You're doing great by maintaining your own allowlist of lints 😄 so I have nothing to add
https://github.com/pypa/hatch/blob/977f88285e9747a0962f7a068b92b356043ff88d/ruff.toml

Here's my allowlist
https://github.com/oprypin/mkdocs-literate-nav/blob/95194eb34bd0eb6fcd4432329be7decd4163305b/pyproject.toml#L106

I don't remember which rules I didn't like. When I didn't like them, I just didn't add them, that's it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@oprypin it doesn’t harm performance thanks to Python’s optimizer, see astral-sh/ruff#8758

A tuple literal is often optimizable to a global singleton and so has 0 construction cost. A set literal is quite expensive to construct every time.

Not when it’s a set lookup like Ruff creates here (x in {...}), then it gets optimized to a global frozenset just like tuples.

Copy link
Contributor

Choose a reason for hiding this comment

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

@flying-sheep but this is a case where the optimizer doesn't kick in

Copy link
Contributor

Choose a reason for hiding this comment

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

I see mostly expressions like

if token not in {'or', 'and', 'with', '(', ')'}
version in {'a', 'b', 'c', 'rc', 'alpha', 'beta', 'pre', 'preview'}

which are cases where the optimizer kicks in. Which means that your original comment is wrong:

In the vast vast vast majority of cases, membership checks are much faster against a tuple literal vs a set literal.

The above isn’t true for this code base. You’re right in your latest comment that for this single line (a set literal containing non-literal values), the optimizer doesn’t kick in. But in the vast majority of cases changed in this PR, it does.

Copy link
Sponsor Collaborator Author

Choose a reason for hiding this comment

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

Okay I acquiesce, I won't be ignoring this rule 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants