mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:56 +00:00
Avoid parenthesizing multiline strings in binary expressions (#6973)
This commit is contained in:
parent
e2b2b1759f
commit
eb552da8a9
6 changed files with 315 additions and 40 deletions
|
@ -227,27 +227,17 @@ class C:
|
|||
|
||||
assert expected(
|
||||
value, is_going_to_be="too long to fit in a single line", srsly=True
|
||||
@@ -153,7 +154,8 @@
|
||||
" because it's too long"
|
||||
)
|
||||
|
||||
- dis_c_instance_method = """\
|
||||
+ dis_c_instance_method = (
|
||||
+ """\
|
||||
%3d 0 LOAD_FAST 1 (x)
|
||||
2 LOAD_CONST 1 (1)
|
||||
4 COMPARE_OP 2 (==)
|
||||
@@ -161,8 +163,8 @@
|
||||
@@ -161,9 +162,7 @@
|
||||
8 STORE_ATTR 0 (x)
|
||||
10 LOAD_CONST 0 (None)
|
||||
12 RETURN_VALUE
|
||||
- """ % (
|
||||
- _C.__init__.__code__.co_firstlineno + 1,
|
||||
+ """
|
||||
+ % (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
)
|
||||
- )
|
||||
+ """ % (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
|
||||
assert (
|
||||
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
@ -409,8 +399,7 @@ class C:
|
|||
" because it's too long"
|
||||
)
|
||||
|
||||
dis_c_instance_method = (
|
||||
"""\
|
||||
dis_c_instance_method = """\
|
||||
%3d 0 LOAD_FAST 1 (x)
|
||||
2 LOAD_CONST 1 (1)
|
||||
4 COMPARE_OP 2 (==)
|
||||
|
@ -418,9 +407,7 @@ class C:
|
|||
8 STORE_ATTR 0 (x)
|
||||
10 LOAD_CONST 0 (None)
|
||||
12 RETURN_VALUE
|
||||
"""
|
||||
% (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
)
|
||||
""" % (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
|
||||
assert (
|
||||
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
|
||||
|
|
|
@ -227,27 +227,17 @@ class C:
|
|||
|
||||
assert expected(
|
||||
value, is_going_to_be="too long to fit in a single line", srsly=True
|
||||
@@ -153,7 +154,8 @@
|
||||
" because it's too long"
|
||||
)
|
||||
|
||||
- dis_c_instance_method = """\
|
||||
+ dis_c_instance_method = (
|
||||
+ """\
|
||||
%3d 0 LOAD_FAST 1 (x)
|
||||
2 LOAD_CONST 1 (1)
|
||||
4 COMPARE_OP 2 (==)
|
||||
@@ -161,8 +163,8 @@
|
||||
@@ -161,9 +162,7 @@
|
||||
8 STORE_ATTR 0 (x)
|
||||
10 LOAD_CONST 0 (None)
|
||||
12 RETURN_VALUE
|
||||
- """ % (
|
||||
- _C.__init__.__code__.co_firstlineno + 1,
|
||||
+ """
|
||||
+ % (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
)
|
||||
- )
|
||||
+ """ % (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
|
||||
assert (
|
||||
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
@ -409,8 +399,7 @@ class C:
|
|||
" because it's too long"
|
||||
)
|
||||
|
||||
dis_c_instance_method = (
|
||||
"""\
|
||||
dis_c_instance_method = """\
|
||||
%3d 0 LOAD_FAST 1 (x)
|
||||
2 LOAD_CONST 1 (1)
|
||||
4 COMPARE_OP 2 (==)
|
||||
|
@ -418,9 +407,7 @@ class C:
|
|||
8 STORE_ATTR 0 (x)
|
||||
10 LOAD_CONST 0 (None)
|
||||
12 RETURN_VALUE
|
||||
"""
|
||||
% (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
)
|
||||
""" % (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
|
||||
assert (
|
||||
expectedexpectedexpectedexpectedexpectedexpectedexpectedexpectedexpect
|
||||
|
|
|
@ -228,6 +228,97 @@ x = (
|
|||
x = (
|
||||
() - () #
|
||||
)
|
||||
|
||||
|
||||
# Avoid unnecessary parentheses around multiline strings.
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
""" % (
|
||||
self.base_url,
|
||||
date.today(),
|
||||
)
|
||||
|
||||
expected_content = (
|
||||
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
"""
|
||||
# Needs parentheses
|
||||
% (
|
||||
self.base_url,
|
||||
date.today(),
|
||||
)
|
||||
)
|
||||
|
||||
expected_content = (
|
||||
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
"""
|
||||
%
|
||||
# Needs parentheses
|
||||
(
|
||||
self.base_url,
|
||||
date.today(),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
""" + a.call.expression(
|
||||
self.base_url,
|
||||
date.today(),
|
||||
)
|
||||
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
""" + sssssssssssssssssssssssssssssssssssssssssooooo * looooooooooooooooooooooooooooooongggggggggggg
|
||||
|
||||
call(arg1, arg2, """
|
||||
short
|
||||
""", arg3=True)
|
||||
|
||||
expected_content = (
|
||||
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
"""
|
||||
%
|
||||
(
|
||||
self.base_url
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
expected_content = (
|
||||
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
"""
|
||||
%
|
||||
(
|
||||
# Needs parentheses
|
||||
self.base_url
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -512,6 +603,100 @@ x = (
|
|||
x = (
|
||||
() - () #
|
||||
)
|
||||
|
||||
|
||||
# Avoid unnecessary parentheses around multiline strings.
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
""" % (
|
||||
self.base_url,
|
||||
date.today(),
|
||||
)
|
||||
|
||||
expected_content = (
|
||||
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
"""
|
||||
# Needs parentheses
|
||||
% (
|
||||
self.base_url,
|
||||
date.today(),
|
||||
)
|
||||
)
|
||||
|
||||
expected_content = (
|
||||
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
"""
|
||||
%
|
||||
# Needs parentheses
|
||||
(
|
||||
self.base_url,
|
||||
date.today(),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
""" + a.call.expression(
|
||||
self.base_url,
|
||||
date.today(),
|
||||
)
|
||||
|
||||
expected_content = (
|
||||
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
"""
|
||||
+ sssssssssssssssssssssssssssssssssssssssssooooo
|
||||
* looooooooooooooooooooooooooooooongggggggggggg
|
||||
)
|
||||
|
||||
call(
|
||||
arg1,
|
||||
arg2,
|
||||
"""
|
||||
short
|
||||
""",
|
||||
arg3=True,
|
||||
)
|
||||
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
""" % (self.base_url)
|
||||
|
||||
|
||||
expected_content = (
|
||||
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
|
||||
</sitemap>
|
||||
</sitemapindex>
|
||||
"""
|
||||
%
|
||||
(
|
||||
# Needs parentheses
|
||||
self.base_url
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue