-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[pyupgrade] Preserve parenthesis when fixing native literals containing newlines (UP018)
#17220
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
Conversation
|
| int(- | ||
| 1) # Carriage return as newline |
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 does not seem to have a carriage return character as newline. It might have been stripped by your either / git. You'd need to create a separate file for this test case and include it in https://github.com/astral-sh/ruff/blob/main/.gitattributes similar to how other files are included there.
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.
Dang. Mus've been git or pre-commit hooks because i was aware of editor autoformatting and checked it before committing.
pyupgrade] Preserve parenthesis when fixing native literals containing newlines (UP018)
|
I'm unsure how the testfiles will behave when running on different machines and I couldn't test as I have only access to windows machines. So let me know if my approach doesn't work. For the rules Alternatively, we could merge those and use |
That's a problem that exists anyway. Not when commiting, but it means they may accidentally change the line ending locally which might be confusing when debugging a test. You can create an |
|
I added the I think it makes sense to keep the separate files for the With I've had some issues getting the linebreak's to render in different editors. What do you think? |
.gitattributes
Outdated
| crates/ruff_python_parser/resources/invalid/re_lex_logical_token_mac_eol.py text eol=cr | ||
|
|
||
| crates/ruff_linter/resources/test/fixtures/ruff/RUF046_CR.py text eol=cr | ||
| crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF046_RUF046_CR.py.snap text eol=cr |
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.
Do we need to include the snapshot files in here? I don't think so considering that other snapshot files aren't included and there are .editorconfig files present in both ruff and pyupgrade snapshot directories.
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 think we need them.
I added both mechanisms for specific reasons:
.editorconfig(added in 67348be): Prevents editors from automatically reformatting the test fixtures when opened.gitattributes: Ensures Git maintains specific line endings (CR/LF) during checkout/staging irrespective of OS
This way i want to avoid (1) tests from passing incorrectly (if both files get normalized) or (2) breaking unexpectedly (if only one gets normalized).
In 5edfdc8 I wanted to add tests for CR and LF lineendings but local normalization made tests passing as both were normalized thus not testing for CRs.
The other test cases don't seem to be sensitive to the specific line endings and should be fine as long as both fixture and snapshot are regularized on checkout by git. But fixing only the fixture, not the snapshot could make tests flaky.
Or am I missing something?
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.
Looking at the crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF046_RUF046_CR.py.snap snapshot, it does seem to render the \r differently (^M). That's why I think adding the snapshot here isn't needed.
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.
Allright, addressed in 005a528.
* main: [red-knot] fix collapsing literal and its negation to object (#17605) [red-knot] Add more tests for protocols (#17603) [red-knot] Ban direct instantiations of `Protocol` classes (#17597) [`pyupgrade`] Preserve parenthesis when fixing native literals containing newlines (`UP018`) (#17220) [`airflow`] fix typos (`AIR302`, `AIR312`) (#17574) [red-knot] Special case `@abstractmethod` for function type (#17591) [red-knot] Emit diagnostics for isinstance() and issubclass() calls where a non-runtime-checkable protocol is the second argument (#17561) [red-knot] Infer the members of a protocol class (#17556) [red-knot] Add `FunctionType::to_overloaded` (#17585) [red-knot] Add mdtests for `global` statement (#17563) [syntax-errors] Make duplicate parameter names a semantic error (#17131)
Closes #17124
Summary
Add a check whether a native literal contains a newline (e.g.
int(+\n1)) in which case we must preserve the parenthesis to not break syntax.Note: An alternative could have been stripping the newline, but I think if it's there it's there for a reason; if not other formatting rules can take care of it.
Test Plan
Extended the existing snapshot test by the affected case.