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

Switch to use ruff #2795

Merged
merged 52 commits into from
Oct 3, 2023
Merged

Switch to use ruff #2795

merged 52 commits into from
Oct 3, 2023

Conversation

CoolCat467
Copy link
Member

This PR has the project switch to using ruff instead of flake8 and handles the slight differences it has.

They have a quite compelling advertisement of sorts of why you should use their system in their readme, but in the end I use it myself in a lot of my projects because it's got nearly all the warnings flake8 does but also has autofix support, and it's way faster.

@codecov
Copy link

codecov bot commented Sep 8, 2023

Codecov Report

Merging #2795 (c53f3d1) into master (c30c76f) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2795   +/-   ##
=======================================
  Coverage   99.13%   99.13%           
=======================================
  Files         115      115           
  Lines       17206    17229   +23     
  Branches     3082     3084    +2     
=======================================
+ Hits        17057    17080   +23     
  Misses        104      104           
  Partials       45       45           
Files Coverage Δ
trio/__init__.py 100.00% <ø> (ø)
trio/_channel.py 100.00% <100.00%> (ø)
trio/_core/_instrumentation.py 100.00% <100.00%> (ø)
trio/_core/_multierror.py 100.00% <100.00%> (ø)
trio/_core/_run.py 100.00% <100.00%> (ø)
trio/_core/_tests/test_asyncgen.py 99.09% <100.00%> (ø)
trio/_core/_tests/test_guest_mode.py 99.69% <ø> (ø)
trio/_core/_tests/test_io.py 100.00% <100.00%> (ø)
trio/_core/_tests/test_ki.py 97.82% <100.00%> (ø)
trio/_core/_tests/test_run.py 100.00% <100.00%> (ø)
... and 16 more

@CoolCat467 CoolCat467 marked this pull request as ready for review September 8, 2023 02:11
@CoolCat467 CoolCat467 changed the title [WIP] Switch to use ruff Switch to use ruff Sep 8, 2023
@CoolCat467 CoolCat467 added the dependencies Pull requests that update a dependency file label Sep 8, 2023
@A5rocks
Copy link
Contributor

A5rocks commented Sep 8, 2023

Opinion: ruff not supporting custom plugins is kind of a big deal for a project like trio, where we try to test, lint, etc. to fix future problems.

But I haven't used ruff myself so I may be wrong to focus on that.

@jakkdl
Copy link
Member

jakkdl commented Sep 8, 2023

ruff is surprisingly on top of mirroring the - functionality in a lot of flake8 plugins, I added a check to flake8-bugbear and in less than 24 hours it was added to ruff. But if there's a flake8 plugin we need that's not implemented I don't mind having flake8 just for that one plugin

@CoolCat467
Copy link
Member Author

ruff is surprisingly on top of mirroring the - functionality in a lot of flake8 plugins, I added a check to flake8-bugbear and in less than 24 hours it was added to ruff. But if there's a flake8 plugin we need that's not implemented I don't mind having flake8 just for that one plugin

Is there anything I need to change in this PR for this? I didn't know trio used any plugins

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

Questions about configuration are probably because I don't know much about ruff.

.pre-commit-config.yaml Outdated Show resolved Hide resolved
check.sh Outdated Show resolved Hide resolved
# noqa'd as it's an exported symbol
# as it's an exported symbol, noqa'd
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this something we should report to ruff? (something like "hey look if the comment doesn't start with noqa: and it isn't just noqa and the line is blank, it's probably just an explanation"?)

Copy link
Member

Choose a reason for hiding this comment

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

trio/__init__.py Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
pyproject.toml Show resolved Hide resolved
@A5rocks
Copy link
Contributor

A5rocks commented Sep 12, 2023

Is there anything I need to change in this PR for this? I didn't know trio used any plugins

I was more thinking about the open issue to make a flake8 plugin (which eventually spun out to make flake8-trio but ... maybe we want to include something in trio or something), in which case we'd probably want to run it against trio too.

But as jakkdl pointed out, we can just add that in at the time of.

@A5rocks
Copy link
Contributor

A5rocks commented Sep 12, 2023

Quick note that ruff now doubles up as an autoformatter; let's hold off on using that until it's not in alpha :^)

@jakkdl
Copy link
Member

jakkdl commented Sep 13, 2023

Is there anything I need to change in this PR for this? I didn't know trio used any plugins

I was more thinking about the open issue to make a flake8 plugin (which eventually spun out to make flake8-trio but ... maybe we want to include something in trio or something), in which case we'd probably want to run it against trio too.

But as jakkdl pointed out, we can just add that in at the time of.

fun fact: flake8-trio can now be run standalone: https://github.com/Zac-HD/flake8-trio#install-and-run-as-standalone
This is because doing autofixes while running as a flake8 plugin wasn't possible.

pyproject.toml Outdated Show resolved Hide resolved
pyproject.toml Show resolved Hide resolved
trio/__init__.py Show resolved Hide resolved
# noqa'd as it's an exported symbol
# as it's an exported symbol, noqa'd
Copy link
Member

Choose a reason for hiding this comment

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

.pre-commit-config.yaml Outdated Show resolved Hide resolved
check.sh Outdated Show resolved Hide resolved
Comment on lines -115 to -125
# TODO: remove this once we have a py.typed file
touch "$INSTALLDIR/py.typed"

Copy link
Member

Choose a reason for hiding this comment

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

If being a stickler I'd remove all the instances of generating/removing py.typed in a dedicated PR.

pyproject.toml Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
@charliermarsh
Copy link

You can allow certain ambiguous characters via allowed-confusables:

[tool.ruff]
allowed-confusables = [""]

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

Accidentally reverted the noqa explanation fix

trio/_tests/test_highlevel_ssl_helpers.py Outdated Show resolved Hide resolved
trio/_tests/test_highlevel_ssl_helpers.py Outdated Show resolved Hide resolved
@CoolCat467 CoolCat467 requested a review from A5rocks September 27, 2023 04:11
Copy link
Member

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

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

I'd recommend reverting the – changes - I think that's the more correct form of dash, using the ASCII character is just a poor substitute.

While I don't disagree in theory, I think standardizing and limiting the character set in code (as opposed to e.g. documentation) is very important for the sake of diffs and sanity of developers not used to differentiating them/don't have an easy way of inputting em-dash/using some bad font that makes them look similar/identical. If it was only allowed in docstrings and comments I'd be less hesitant, though either way is probably fine.

trio/_tools/gen_exports.py Show resolved Hide resolved
setup.py Outdated Show resolved Hide resolved
Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

Really rough (haha, ruff) review. After these are resolved this should look good to me though.

@@ -13,8 +13,7 @@ cryptography>=41.0.0 # cryptography<41 segfaults on pypy3.10
black; implementation_name == "cpython"
mypy; implementation_name == "cpython"
types-pyOpenSSL; implementation_name == "cpython" # and annotations
flake8
flake8-pyproject
ruff >= 0.0.291
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless we're depending on some 0.0.291 functionality, I think we should not have a version constraint here -- the lockfile can have that for us!

Copy link
Member Author

Choose a reason for hiding this comment

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

--format is depreciated, 0.0.291 has the new version, --output-format dcce938

trio/_core/_multierror.py Outdated Show resolved Hide resolved
Comment on lines +1481 to 1483


@attr.s(eq=False, hash=False, slots=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm unintentional new whitespace?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a part of one of the formatting rules that says that there should always be two newlines between any new definitions in the global scope

Comment on lines 168 to 174
== '''def combine_and(data: list[str]) -> str:
"""Join values of text, and have 'and' with the last one properly."""
if len(data) >= 2:
data[-1] = 'and ' + data[-1]
if len(data) > 2:
return ', '.join(data)
return ' '.join(data)'''
Copy link
Contributor

Choose a reason for hiding this comment

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

To my untrained eye this looks the same as the file above. Maybe maybe a variable so you only have to type it once?

Also, just wondering where this sample code comes from!

Copy link
Member Author

@CoolCat467 CoolCat467 Sep 30, 2023

Choose a reason for hiding this comment

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

Checked and it is indeed identical, will make a variable shortly.

Sample code was in one of the files I have on my desktop when I'm testing things, but digging deeper it looks like a modified version of one of the functions from my discord bot (https://github.com/CoolCat467/StatusBot/blob/4c4a0808ecbba4bcd3a1a6f0bff8169bc8a2bbd5/src/StatusBot/bot.py#L169C1-L176C26)

Edit: To be more precise, this function's idea is used all over the place in a lot of my projects, usually when displaying lists of something to end users in a nice way, and is usually either specialized in joining the end with and or with or, and in a few iterations it's generic and can have the end be one of the inputs. combine_and, combine_end, and list_or are all examples of different names I've used for this idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

Made variable in af0547a

trio/_tools/gen_exports.py Outdated Show resolved Hide resolved
trio/_tools/gen_exports.py Outdated Show resolved Hide resolved
@A5rocks
Copy link
Contributor

A5rocks commented Oct 3, 2023

This looks good!

@A5rocks A5rocks merged commit bbb4be7 into python-trio:master Oct 3, 2023
27 of 28 checks passed
@CoolCat467 CoolCat467 deleted the replace-with-ruff branch October 3, 2023 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants