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

Add some ruff rules for pyupgrade #183

Merged
merged 1 commit into from
Apr 30, 2024
Merged

Add some ruff rules for pyupgrade #183

merged 1 commit into from
Apr 30, 2024

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Apr 28, 2024

% ruff check --select=UP --ignore=UP015,UP031 --statistics | sort -k2

10	UP004 	[*] Class `BaseTestCase` inherits from `object`
 1	UP009 	[*] UTF-8 encoding declaration is unnecessary
 1	UP020 	[*] Use builtin `open`
18	UP024 	[*] Replace aliased errors with `OSError`
 9	UP025 	[*] Remove unicode literals from strings

% ruff rule UP024

os-error-alias (UP024)

Derived from the pyupgrade linter.

Fix is always available.

What it does

Checks for uses of exceptions that alias OSError.

Why is this bad?

OSError is the builtin error type used for exceptions that relate to the
operating system.

In Python 3.3, a variety of other exceptions, like WindowsError were
aliased to OSError. These aliases remain in place for compatibility with
older versions of Python, but may be removed in future versions.

Prefer using OSError directly, as it is more idiomatic and future-proof.

Example

raise IOError

Use instead:

raise OSError

References

@corona10
Copy link
Member

corona10 commented Apr 29, 2024

If you want to submit the patch related code style issue, would you like to submit the patch including the CI works?
cc @vstinner

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

Would it be possible to exclude changes converting code to "expr1 if test else expr2"?

pyperf/__main__.py Outdated Show resolved Hide resolved
pyperf/_bench.py Outdated Show resolved Hide resolved
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

@corona10: What do you think of this change?

pyperf/_utils.py Outdated Show resolved Hide resolved
pyperf/tests/test_runner.py Outdated Show resolved Hide resolved
@cclauss cclauss closed this Apr 30, 2024
@cclauss cclauss deleted the pyupgrade branch April 30, 2024 08:54
@vstinner
Copy link
Member

@cclauss cclauss closed

This change combines many different changes. Some of them are uncontroversial, I have an opinion on the others :-)

@cclauss
Copy link
Contributor Author

cclauss commented Apr 30, 2024

I understand.

These PRs are super easy to create locally with syntax like:
% ruff check --select=UP004 --fix

@vstinner
Copy link
Member

I'm fine with these changes, coming from the old time when pyperf worked on Python 2:

10	UP004 	[*] Class `BaseTestCase` inherits from `object`
 1	UP009 	[*] UTF-8 encoding declaration is unnecessary
 1	UP020 	[*] Use builtin `open`
18	UP024 	[*] Replace aliased errors with `OSError`
 9	UP025 	[*] Remove unicode literals from strings

@cclauss cclauss restored the pyupgrade branch April 30, 2024 15:50
@cclauss cclauss reopened this Apr 30, 2024
@cclauss cclauss changed the title Simplify and upgrade with ruff rules SIM and UP Add some ruff rules for pyupgrade Apr 30, 2024
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

@vstinner vstinner merged commit c964503 into psf:main Apr 30, 2024
12 checks passed
@vstinner
Copy link
Member

Merged, thanks.

@cclauss cclauss deleted the pyupgrade branch April 30, 2024 16:13
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.

3 participants