Skip to content

Avoid parenthesizing octal/hex or binary literals in object positions#8160

Merged
MichaReiser merged 1 commit intomainfrom
dont-parenthesize-octal-hex-and-binary-literals
Oct 24, 2023
Merged

Avoid parenthesizing octal/hex or binary literals in object positions#8160
MichaReiser merged 1 commit intomainfrom
dont-parenthesize-octal-hex-and-binary-literals

Conversation

@MichaReiser
Copy link
Member

Summary

This fixes a Black deviation that is only relevant for unformatted code.

Python (and many other languages) require parenthesizing number literals in the object position of a member expression to disambiguate the . of the member expression from a decimal separator:

5.test # invalid 
(5).test # valid
5 .test # valid, but not as nice

However, the parentheses are not required for binary, octal, or hex literals because they can't have a decimal separator

0b00.test
0x00.test
0o00.test

This PR changes our formatter to not insert parentheses for binary, octal, and hex literals.

Test Plan

Fixed black compatibility test.

Verified that the similarity index remains unchanged.

@MichaReiser
Copy link
Member Author

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@MichaReiser MichaReiser added the formatter Related to the formatter label Oct 24, 2023
@MichaReiser MichaReiser requested a review from konstin October 24, 2023 09:22
let text = &source[*range];
!matches!(
text.as_bytes().get(0..2),
Some([b'0', b'x' | b'X' | b'o' | b'O' | b'b' | b'B'])
Copy link
Member

Choose a reason for hiding this comment

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

There can't be any spaces here, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. Underscores are also not allowed

@MichaReiser MichaReiser merged commit 8b665f4 into main Oct 24, 2023
@MichaReiser MichaReiser deleted the dont-parenthesize-octal-hex-and-binary-literals branch October 24, 2023 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants