-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
🔧 Ruff format select sphinx modules with minimal diffs #12146
🔧 Ruff format select sphinx modules with minimal diffs #12146
Conversation
Any plans to include some/all of these changesets in I'm supportive of the styling changes, but I spend a lot of time in (note: there could be odd side-effects from that file, like making it difficult to intentionally track down when style-related changes occurred) |
I've never used this really before, so I'm open to suggestions |
Ok. Don't let it block any progress -- we can't use it until we know the merged |
logger.warning(__('The %s extension is required by needs_extensions settings, ' | ||
'but it is not loaded.'), extname) | ||
logger.warning( | ||
__( | ||
'The %s extension is required by needs_extensions settings, ' | ||
'but it is not loaded.' | ||
), | ||
extname, | ||
) |
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.
Maybe a manual tweak here: would it look better to extract a msg
variable for the localized warning message?
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.
but the msg still does not fit on one line, without going over the line-length limit, so it will not change the number of lines
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.
Possible I missed something, but this works for me with ruff
locally:
- logger.warning(
- __(
- 'The %s extension is required by needs_extensions settings, '
- 'but it is not loaded.'
- ),
- extname,
- )
+ msg = __('The %s extension is required by needs_extensions settings, '
+ 'but it is not loaded.')
+ logger.warning(msg)
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.
ah, yes - the extname
. That's important...
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.
(even with extname
added back in, it seems to pass ruff
checks OK. I think it makes the overall diff marginally easier to comprehend, too, which is sometimes nice)
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.
That is extremely strange that it passes on my machine. Ok, ignore my suggestion - I'll spend some time to figure out how that happened soon. Apologies for the distraction!
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.
(and thanks for providing a CI-based confirmation, I should have thought to try that!)
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.
no worries cheers
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.
Ok; I'm pausing for a moment just in case this is relevant.
When using the full file path (ruff check sphinx/extension.py
) it seems that our .ruff.toml
config is ignored.
That's why this passed when I ran the command above.
When I run ruff check
in the root sphinx.git
directory with my suggestion in place, it fails (F821
).
I think that means that something in our project configuration (either flake8
or ruff
related) introduces this failure. I'd like to confirm what that is.
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.
Ok, confirmed: it is due to our config choice here:
Lines 116 to 117 in 392358d
# pyflakes ('F') | |
"F", |
logger.warning(__('file %r on theme path is not a valid ' | ||
'zipfile or contains no theme'), entry) | ||
logger.warning( | ||
__( | ||
'file %r on theme path is not a valid ' | ||
'zipfile or contains no theme' | ||
), | ||
entry, | ||
) |
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.
Ditto here re: possible variable extraction. Consistent styling will be great; 2 lines compared to 7 seems a significant difference, though!
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.
same as above, the msg won't fit on a single line, so it seems not worth the effort to extract out a msg
variable (you'll still have the same number of lines)
sphinx/jinja2glue.py
Outdated
template = template[1:] | ||
for loader in loaders: | ||
try: | ||
return loader.get_source(environment, template) | ||
except TemplateNotFound: | ||
pass | ||
msg = (f"{template!r} not found in " | ||
f"{self.environment.loader.pathchain}") # type: ignore[union-attr] | ||
pathchain = f'{self.environment.loader.pathchain}' # type: ignore[union-attr] |
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.
This f-string is useless.
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.
Sure, I had to move this to a seperate variable only because of flake8: ruff actually allows for leeway in the line-length, if there is a comment making it longer, but flake8 does not
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.
i suggest suppressing the flak8 lint
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.
i suggest suppressing the flak8 lint
Ok I just increased the flake8 line-length to 120, and then put all this back on one line
sphinx/deprecation.py
Outdated
message = (f'The alias {qualified_name!r} is deprecated, ' | ||
f'use {canonical_name!r} instead.') | ||
message = ( | ||
f'The alias {qualified_name!r} is deprecated, ' f'use {canonical_name!r} instead.' |
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.
This one is confusing in the sense that two f-strings are concatenated on the same line.
Is there a way for ruff to merge the f-strings when possible?
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.
fixed manually
Is there a way for ruff to merge the f-strings when possible?
It does not appear so, maybe a good feature request to open on ruff 😄
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.
Except for the bad f-strings that were introduced, I think I'll need to live with this.
sphinx/extension.py
Outdated
msg = __('The %s extension is required by needs_extensions settings, ' | ||
'but it is not loaded.') | ||
logger.warning(msg, extname) |
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.
@jayaddison would you confirm this is how you proposed formatting it?
and then can you also see, this fails the CI checks:
I feel something in your set up is wrong 😅
thanks for the review, obviously a few of my answers "if you have a problem, take it up with ruff" 😄 , |
Well.. that's usually what I do when I want something upstream. It's just I'll need to change my PyCharm's bindings to avoid bad autoformats (but that's something for me only). |
It doesn't have ruff integration? |
.flake8
Outdated
@@ -1,7 +1,8 @@ | |||
[flake8] | |||
max-line-length = 95 | |||
max-line-length = 120 |
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.
Let's update this in ruff.toml
(and anywhere else?) too.
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.
In fact: I think ruff
will read it from flake8
's config if it can. So we should remove it from ruff.toml
, I think.
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.
no, this is just to stop flake8 complaining in places ruff does do not; ruff allows leniency in line-length for lines with comments, flake8 does not
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.
we are just deferring the line-length checks to ruff, obviously eventually we will hopefully be removing flake8 entirely
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.
In fact: I think
ruff
will read it fromflake8
's config if it can. So we should remove it fromruff.toml
, I think.
No, I may be talking rubbish unfortunately. I was distracted by the fact that ruff config line-length
mentions 120
, which is, by chance, what we're updating flake8
to use here. But it doesn't confirm that ruff
is reading that value.
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.
i would actually suggest suppressing the lint in flake8 entirely, rather than just making the line length limit greater
how do you do that?
(I'm not that bothered either way, because I hope we just get rid of flake8 sooner rather than later 😄 )
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.
i would actually suggest suppressing the lint in flake8 entirely, rather than just making the line length limit greater
Only for comments, ideally?
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.
max-doc-length
could achieve that I think, if so.
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.
how do you do that?
add E501
to the 'ignore' list in the flake8 config file
Only for comments, ideally?
ruff ignores comments when calculating line length, flake8 does not.
ruff allows longer lines in a few select cases:
Overlong lines can hurt readability. PEP 8, for example, recommends limiting lines to 79 characters. By default, this rule enforces a limit of 88 characters for compatibility with Black, though that limit is configurable via the line-length setting.
In the interest of pragmatism, this rule makes a few exceptions when determining whether a line is overlong. Namely, it:
Ignores lines that consist of a single "word" (i.e., without any whitespace between its characters).
Ignores lines that end with a URL, as long as the URL starts before the line-length threshold.
Ignores line that end with a pragma comment (e.g., # type: ignore or # noqa), as long as the pragma comment starts before the line-length threshold. That is, a line will not be flagged as overlong if a pragma comment causes it to exceed the line length. (This behavior aligns with that of the Ruff formatter.)
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.
add E501 to the 'ignore' list in the flake8 config file
done 👍
This PR removes select sphinx top-level modules from thr
ruff format
exclude list.They were selected based on the fact that they have the least diffs when formatted, as hopefully you can see.
I don't believe these changes should adversely affect any existing PRs.