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

Apply more assorted Pyugrade suggestions #4125

Merged
merged 12 commits into from
Jan 5, 2024

Conversation

DimitriPapadopoulos
Copy link
Contributor

Summary of changes

Apply more suggestions from pyupgrade.

Pull Request Checklist

Copy link
Contributor

@abravalheri abravalheri left a comment

Choose a reason for hiding this comment

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

Thank you very much @DimitriPapadopoulos.

I will not merge this immediately because I am waiting to see if any other bugfixes are necessary for v69, but I think we can merge the PR soon in the future.

@DimitriPapadopoulos
Copy link
Contributor Author

Thank you for looking into this.

Would you agree to add the pyupgrade linter once I have finished fixing the issues reported by pyupgrade?

@abravalheri
Copy link
Contributor

Would you agree to add the pyupgrade linter once I have finished fixing the issues reported by pyupgrade?

If @jaraco is OK with that, I think this change would be fine (we will need to prevent/ignore changes in the setuptools._distutils and setuptools._vendor paths, though).

@DimitriPapadopoulos
Copy link
Contributor Author

By the way, I don't know how you feel about ruff, but it lets you disable pyupgrade rules, while the original pyupgrade is rather opinionated and does not let you disable any of its rules.

@abravalheri
Copy link
Contributor

I think we are already using pytest-ruff for some stuff... But I guess pytest-ruff requires a more verbose configuration in order to do the pyupgrade stuff (it would be nice if they implement profiles like isort did once...).

@Avasam
Copy link
Contributor

Avasam commented Nov 22, 2023

A sample ruff.toml would look like:

extend-select = [
  "UP", # pyupgrade
]
extend-ignore = [
  "UP015",  # redundant-open-modes, explicit is prefered
]
extend-exclude = [
  # Vendored
  "**/_vendor",
  "setuptools/_distutils",
] 

@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented Nov 22, 2023

Doesn't pytest-ruff search its config in pyproject.toml like ruff, in addition to ruff.toml? I am asking because we could avoid an extra config file.

@Avasam
Copy link
Contributor

Avasam commented Nov 22, 2023

Doesn't pytest-ruff search its config in pyproject.toml like ruff, in addition to ruff.toml? I am asking because we could avoid an extra config file.

@jaraco 's previously written opinion on this, which I think will apply just the same to Ruff: #3979 (review)

I do wish to retain the mypy settings in mypy.ini for two reasons:

  • The skeleton for this project is maintained upstream, which defines the mypy settings in its own file. Moving the settings to a different file makes it difficult to compare and manage the differences with settings inherited from upstream.
  • Piling all project settings in pyproject.toml is a bad idea in general. I've been meaning to write a blog post about it, but the tl;dr is that because there's no ordering or namespacing, the settings lack structure and consistency (compared with mypy which always appears after docs/ and before newsfragments/ and is clearly distinct from towncrier or tox or pytest settings.

@DimitriPapadopoulos
Copy link
Contributor Author

I have added a ruff.toml file in 7e986ee.

I have disabled warnings about string interpolation for now, because the changes are really pervasive. Also, I find f-strings might be less readable when string interpolation involves a complex expression instead of a mere variable. In such cases, I feel keeping .format() or adding a temporary variable to store the result of the complex expression is preferable. Do you want me to proceed nevertheless?

@abravalheri
Copy link
Contributor

Let's wait to see if Jason is Ok with the approach. Worst case scenario we can remove the ruff.toml file and merge the PR as it is.

@jaraco
Copy link
Member

jaraco commented Nov 22, 2023 via email

@abravalheri
Copy link
Contributor

@DimitriPapadopoulos, let's merge this PR as it is then (but I will not cutting a release right now, just add the changes to main, I hope that is OK).
I will just wait another day to see if we don't have any other backlash on v69 before doing the merge.

I have disabled warnings about string interpolation for now, because the changes are really pervasive. Also, I find f-strings might be less readable when string interpolation involves a complex expression instead of a mere variable. In such cases, I feel keeping .format() or adding a temporary variable to store the result of the complex expression is preferable. Do you want me to proceed nevertheless?

If that would allow us to reduce the configuration size for Ruff, I like the idea of temporary variable. I think it is worth to be as streamline as possible. But let's do that in a follow up PR...

@DimitriPapadopoulos
Copy link
Contributor Author

I’m pleased to see the use of Ruff over pyupgrade. Id also be interested to see Ruff replace black and implement other common formatting/linking like import sorts.

I have started working on this in jaraco/skeleton#99.

@abravalheri
Copy link
Contributor

I will just wait another day to see if we don't have any other backlash on v69 before doing the merge.

Postponed until we figure out what happened on #4136

@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented Nov 23, 2023

@abravalheri I have noticed that jaraco/skeleton uses the recent skip-string-normalization black option:

jaraco/skeleton/pyproject.toml

[tool.black]
skip-string-normalization = true

Should I instruct black to leave quotes as they are, as in jaraco/skeleton? On the other hand, this project has already been black'ified with double quotes everywhere.

@abravalheri
Copy link
Contributor

abravalheri commented Nov 23, 2023

I think that setuptools should target to have ruff.toml as close as possible to skeleton, so that it can be merged easily.

ruff.toml Outdated
@@ -1,4 +1,3 @@
[lint]
Copy link
Contributor

@abravalheri abravalheri Jan 4, 2024

Choose a reason for hiding this comment

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

Should we just leave the [lint] section header so it differs less from the skeleton configuration?

Copy link
Contributor Author

@DimitriPapadopoulos DimitriPapadopoulos Jan 4, 2024

Choose a reason for hiding this comment

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

Good catch. I should rather fix the skeleton configuration as ignore is part of the Top-level Ruff Settings:
jaraco/skeleton#106

Copy link
Contributor

@abravalheri abravalheri Jan 4, 2024

Choose a reason for hiding this comment

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

I think that the ruff docs are a bit outdated, the main example uses the [lint] section, and the detailed reference puts them the the global scope... Which is a bit confusing :P

Henry once told me that in the past Ruff docs would use the global config but now they are going towards [lint]...
In this PR we can see the author referencing the new lint sections:

  • Breaking up "Configuration" into "Configuring Ruff" (for generic configuration), and new Linter- and Formatter-specific sections.
  • Updating all example configurations to use [tool.ruff.lint] and [tool.ruff.format].

So that sounds like it is the most up-to-date thing to do (at least until the next release 😅)

Copy link
Contributor Author

@DimitriPapadopoulos DimitriPapadopoulos Jan 4, 2024

Choose a reason for hiding this comment

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

They are going towards lint for all options that are related to lint, that's for sure. However, ignore applies to both lint and format. At least that's my understanding.

See jaraco/skeleton#106 (comment).

Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect, we got a definitive answer from the maintainers: astral-sh/ruff#8297 (reply in thread)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll put back [lint].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. But note this requires to duplicate extend-exclude, both under [lint] and [format].

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you very much @DimitriPapadopoulos.

Yeah, not ideal. But I guess it is good to go with Ruff maintainer's recommendation.

In Python 3, io.open() is an alias for the builtin open() function.

https://docs.python.org/3/library/io.html#io.open

This is a suggestion from pyupgrade:
https://github.com/asottile/pyupgrade#open-alias
Fixed by running `ruff --select UP018 --fix .`:
UP018 [*] Unnecessary `str` call (rewrite as a literal)

Original suggestions from pyupgrade:
https://github.com/asottile/pyupgrade#forced-strnative-literals
This is an alias for the built-in OSError exception:
https://docs.python.org/3/library/os.html#os.error

Fixed by running `ruff --select UP024 --fix .`:
UP024 [*] Replace aliased errors with `OSError`
In Python ≥ 3.7, `capture_output` can be used instead of `stdout=PIPE` / `stderr=PIPE`.

Fixed by running `ruff --select UP022 --fix --unsafe-fixes .`:
UP022 Sending `stdout` and `stderr` to `PIPE` is deprecated, use `capture_output`
Fixed by running `ruff --select UP034 --fix .`:
UP034 [*] Avoid extraneous parentheses
In Python 3, the source encodign is implict, UTF-8 by default.

This is a suggestion from pyupgrade:
https://github.com/asottile/pyupgrade#-coding--comment

Fixed by running `ruff --select UP009 --fix .`:
UP009 [*] UTF-8 encoding declaration is unnecessary
This is a suggestion from pyupgrade:
https://github.com/asottile/pyupgrade#encode-to-bytes-literals

Fixed by running `ruff --select UP012 --fix .`:
UP012 [*] Unnecessary call to `encode` as UTF-8
Fixed by running `ruff --select UP027 --fix .`:
UP027 [*] Replace unpacked list comprehension with a generator expression
@abravalheri
Copy link
Contributor

abravalheri commented Jan 5, 2024

I just squashed the first and last commit together because I want to avoid as much as I can future git conflicts.

ruff.toml Show resolved Hide resolved
ruff.toml Show resolved Hide resolved
@abravalheri abravalheri merged commit d3048fc into pypa:main Jan 5, 2024
21 of 23 checks passed
@abravalheri
Copy link
Contributor

Thank you very much for the contribution!

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.

4 participants