mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:56 +00:00
parent
9e2fd0c620
commit
1979103ec0
11 changed files with 583 additions and 123 deletions
|
@ -108,7 +108,7 @@ async def wat():
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -9,16 +9,13 @@
|
||||
@@ -9,16 +9,16 @@
|
||||
Possibly also many, many lines.
|
||||
"""
|
||||
|
||||
|
@ -122,15 +122,16 @@ async def wat():
|
|||
+NOT_YET_IMPLEMENTED_StmtImport
|
||||
+NOT_YET_IMPLEMENTED_StmtImportFrom # some noqa comment
|
||||
|
||||
-try:
|
||||
try:
|
||||
- import fast
|
||||
-except ImportError:
|
||||
+ NOT_YET_IMPLEMENTED_StmtImport
|
||||
except ImportError:
|
||||
- import slow as fast
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
+ NOT_YET_IMPLEMENTED_StmtImport
|
||||
|
||||
|
||||
# Some comment before a function.
|
||||
@@ -35,7 +32,7 @@
|
||||
@@ -35,7 +35,7 @@
|
||||
Possibly many lines.
|
||||
"""
|
||||
# FIXME: Some comment about why this function is crap but still in production.
|
||||
|
@ -139,7 +140,7 @@ async def wat():
|
|||
|
||||
if inner_imports.are_evil():
|
||||
# Explains why we have this if.
|
||||
@@ -82,8 +79,7 @@
|
||||
@@ -82,8 +82,7 @@
|
||||
async def wat():
|
||||
# This comment, for some reason \
|
||||
# contains a trailing backslash.
|
||||
|
@ -149,7 +150,7 @@ async def wat():
|
|||
# Comment after ending a block.
|
||||
if result:
|
||||
print("A OK", file=sys.stdout)
|
||||
@@ -93,4 +89,4 @@
|
||||
@@ -93,4 +92,4 @@
|
||||
|
||||
# Some closing comments.
|
||||
# Maybe Vim or Emacs directives for formatting.
|
||||
|
@ -178,7 +179,10 @@ NOT_YET_IMPLEMENTED_StmtImport
|
|||
NOT_YET_IMPLEMENTED_StmtImport
|
||||
NOT_YET_IMPLEMENTED_StmtImportFrom # some noqa comment
|
||||
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
NOT_YET_IMPLEMENTED_StmtImport
|
||||
except ImportError:
|
||||
NOT_YET_IMPLEMENTED_StmtImport
|
||||
|
||||
|
||||
# Some comment before a function.
|
||||
|
|
|
@ -85,17 +85,9 @@ if __name__ == "__main__":
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -20,14 +20,9 @@
|
||||
with open(some_temp_file) as f:
|
||||
data = f.read()
|
||||
|
||||
-try:
|
||||
- with open(some_other_file) as w:
|
||||
- w.write(data)
|
||||
-
|
||||
-except OSError:
|
||||
- print("problems")
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
@@ -27,7 +27,7 @@
|
||||
except OSError:
|
||||
print("problems")
|
||||
|
||||
-import sys
|
||||
+NOT_YET_IMPLEMENTED_StmtImport
|
||||
|
@ -129,7 +121,12 @@ for i in range(100):
|
|||
with open(some_temp_file) as f:
|
||||
data = f.read()
|
||||
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
with open(some_other_file) as w:
|
||||
w.write(data)
|
||||
|
||||
except OSError:
|
||||
print("problems")
|
||||
|
||||
NOT_YET_IMPLEMENTED_StmtImport
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ async def test_async_with():
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,62 +1,55 @@
|
||||
@@ -1,62 +1,61 @@
|
||||
# Make sure a leading comment is not removed.
|
||||
-def some_func( unformatted, args ): # fmt: skip
|
||||
+def some_func(unformatted, args): # fmt: skip
|
||||
|
@ -134,12 +134,16 @@ async def test_async_with():
|
|||
|
||||
|
||||
-try : # fmt: skip
|
||||
- some_call()
|
||||
+try:
|
||||
+ # fmt: skip
|
||||
some_call()
|
||||
-except UnformattedError as ex: # fmt: skip
|
||||
- handle_exception()
|
||||
-finally : # fmt: skip
|
||||
- finally_call()
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
+except UnformattedError as ex: # fmt: skip
|
||||
+ handle_exception() # fmt: skip
|
||||
+finally:
|
||||
finally_call()
|
||||
|
||||
|
||||
-with give_me_context( unformatted, args ): # fmt: skip
|
||||
|
@ -202,7 +206,13 @@ async def test_async_for():
|
|||
NOT_YET_IMPLEMENTED_StmtAsyncFor # fmt: skip
|
||||
|
||||
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
# fmt: skip
|
||||
some_call()
|
||||
except UnformattedError as ex: # fmt: skip
|
||||
handle_exception() # fmt: skip
|
||||
finally:
|
||||
finally_call()
|
||||
|
||||
|
||||
with give_me_context(unformatted, args): # fmt: skip
|
||||
|
|
|
@ -65,7 +65,7 @@ with hmm_but_this_should_get_two_preceding_newlines():
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -32,34 +32,20 @@
|
||||
@@ -32,34 +32,28 @@
|
||||
|
||||
|
||||
if os.name == "posix":
|
||||
|
@ -76,18 +76,18 @@ with hmm_but_this_should_get_two_preceding_newlines():
|
|||
pass
|
||||
-
|
||||
elif os.name == "nt":
|
||||
- try:
|
||||
try:
|
||||
- import msvcrt
|
||||
-
|
||||
- def i_should_be_followed_by_only_one_newline():
|
||||
- pass
|
||||
+ NOT_YET_IMPLEMENTED_StmtTry
|
||||
+ NOT_YET_IMPLEMENTED_StmtImport
|
||||
|
||||
- except ImportError:
|
||||
-
|
||||
- def i_should_be_followed_by_only_one_newline():
|
||||
- pass
|
||||
def i_should_be_followed_by_only_one_newline():
|
||||
pass
|
||||
|
||||
except ImportError:
|
||||
-
|
||||
def i_should_be_followed_by_only_one_newline():
|
||||
pass
|
||||
|
||||
elif False:
|
||||
-
|
||||
class IHopeYouAreHavingALovelyDay:
|
||||
|
@ -146,7 +146,15 @@ if os.name == "posix":
|
|||
def i_should_be_followed_by_only_one_newline():
|
||||
pass
|
||||
elif os.name == "nt":
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
NOT_YET_IMPLEMENTED_StmtImport
|
||||
|
||||
def i_should_be_followed_by_only_one_newline():
|
||||
pass
|
||||
|
||||
except ImportError:
|
||||
def i_should_be_followed_by_only_one_newline():
|
||||
pass
|
||||
|
||||
elif False:
|
||||
class IHopeYouAreHavingALovelyDay:
|
||||
|
|
|
@ -47,77 +47,100 @@ except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.ov
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,42 +1,17 @@
|
||||
# These brackets are redundant, therefore remove.
|
||||
-try:
|
||||
- a.something
|
||||
-except AttributeError as err:
|
||||
@@ -2,7 +2,7 @@
|
||||
try:
|
||||
a.something
|
||||
except AttributeError as err:
|
||||
- raise err
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
+ NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
# This is tuple of exceptions.
|
||||
# Although this could be replaced with just the exception,
|
||||
# we do not remove brackets to preserve AST.
|
||||
-try:
|
||||
- a.something
|
||||
-except (AttributeError,) as err:
|
||||
@@ -10,28 +10,26 @@
|
||||
try:
|
||||
a.something
|
||||
except (AttributeError,) as err:
|
||||
- raise err
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
+ NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
# This is a tuple of exceptions. Do not remove brackets.
|
||||
-try:
|
||||
- a.something
|
||||
-except (AttributeError, ValueError) as err:
|
||||
try:
|
||||
a.something
|
||||
except (AttributeError, ValueError) as err:
|
||||
- raise err
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
+ NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
# Test long variants.
|
||||
-try:
|
||||
- a.something
|
||||
try:
|
||||
a.something
|
||||
-except (
|
||||
- some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error
|
||||
-) as err:
|
||||
- raise err
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
+except some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error as err:
|
||||
+ NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
-try:
|
||||
- a.something
|
||||
-except (
|
||||
- some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
-) as err:
|
||||
try:
|
||||
a.something
|
||||
except (
|
||||
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
) as err:
|
||||
- raise err
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
+ NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
-try:
|
||||
- a.something
|
||||
-except (
|
||||
- some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
- some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
-) as err:
|
||||
try:
|
||||
a.something
|
||||
@@ -39,4 +37,4 @@
|
||||
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
) as err:
|
||||
- raise err
|
||||
+NOT_YET_IMPLEMENTED_StmtTry
|
||||
+ NOT_YET_IMPLEMENTED_StmtRaise
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
||||
```py
|
||||
# These brackets are redundant, therefore remove.
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
a.something
|
||||
except AttributeError as err:
|
||||
NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
# This is tuple of exceptions.
|
||||
# Although this could be replaced with just the exception,
|
||||
# we do not remove brackets to preserve AST.
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
a.something
|
||||
except (AttributeError,) as err:
|
||||
NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
# This is a tuple of exceptions. Do not remove brackets.
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
a.something
|
||||
except (AttributeError, ValueError) as err:
|
||||
NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
# Test long variants.
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
a.something
|
||||
except some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error as err:
|
||||
NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
a.something
|
||||
except (
|
||||
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
) as err:
|
||||
NOT_YET_IMPLEMENTED_StmtRaise
|
||||
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
a.something
|
||||
except (
|
||||
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
|
||||
) as err:
|
||||
NOT_YET_IMPLEMENTED_StmtRaise
|
||||
```
|
||||
|
||||
## Black Output
|
||||
|
|
|
@ -67,22 +67,18 @@ def example8():
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -8,13 +8,7 @@
|
||||
|
||||
async def show_status():
|
||||
@@ -10,9 +10,7 @@
|
||||
while True:
|
||||
- try:
|
||||
- if report_host:
|
||||
try:
|
||||
if report_host:
|
||||
- data = (
|
||||
- f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||
- ).encode()
|
||||
- except Exception as e:
|
||||
- pass
|
||||
+ NOT_YET_IMPLEMENTED_StmtTry
|
||||
+ data = (NOT_YET_IMPLEMENTED_ExprJoinedStr).encode()
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
|
||||
def example():
|
||||
@@ -30,15 +24,11 @@
|
||||
@@ -30,15 +28,11 @@
|
||||
|
||||
|
||||
def example2():
|
||||
|
@ -100,7 +96,7 @@ def example8():
|
|||
|
||||
|
||||
def example4():
|
||||
@@ -50,35 +40,11 @@
|
||||
@@ -50,35 +44,11 @@
|
||||
|
||||
|
||||
def example6():
|
||||
|
@ -153,7 +149,11 @@ data = (
|
|||
|
||||
async def show_status():
|
||||
while True:
|
||||
NOT_YET_IMPLEMENTED_StmtTry
|
||||
try:
|
||||
if report_host:
|
||||
data = (NOT_YET_IMPLEMENTED_ExprJoinedStr).encode()
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
|
||||
def example():
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/try.py
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
try:
|
||||
...
|
||||
except:
|
||||
...
|
||||
|
||||
try:
|
||||
...
|
||||
except (KeyError): # should remove brackets and be a single line
|
||||
...
|
||||
|
||||
|
||||
try: # try
|
||||
...
|
||||
# end of body
|
||||
# before except
|
||||
except (Exception, ValueError) as exc: # except line
|
||||
...
|
||||
# before except 2
|
||||
except KeyError as key: # except line 2
|
||||
...
|
||||
# in body 2
|
||||
# before else
|
||||
else:
|
||||
...
|
||||
# before finally
|
||||
finally:
|
||||
...
|
||||
|
||||
|
||||
|
||||
# with line breaks
|
||||
try: # try
|
||||
...
|
||||
# end of body
|
||||
|
||||
# before except
|
||||
except (Exception, ValueError) as exc: # except line
|
||||
...
|
||||
|
||||
# before except 2
|
||||
except KeyError as key: # except line 2
|
||||
...
|
||||
# in body 2
|
||||
|
||||
# before else
|
||||
else:
|
||||
...
|
||||
|
||||
# before finally
|
||||
finally:
|
||||
...
|
||||
|
||||
|
||||
# with line breaks
|
||||
try:
|
||||
...
|
||||
|
||||
except:
|
||||
...
|
||||
|
||||
|
||||
try:
|
||||
...
|
||||
except (Exception, Exception, Exception, Exception, Exception, Exception, Exception) as exc: # splits exception over multiple lines
|
||||
...
|
||||
|
||||
|
||||
try:
|
||||
...
|
||||
except:
|
||||
a = 10 # trailing comment1
|
||||
b = 11 # trailing comment2
|
||||
```
|
||||
|
||||
## Output
|
||||
```py
|
||||
try:
|
||||
...
|
||||
except:
|
||||
...
|
||||
|
||||
try:
|
||||
...
|
||||
except KeyError: # should remove brackets and be a single line
|
||||
...
|
||||
|
||||
|
||||
try:
|
||||
# try
|
||||
...
|
||||
# end of body
|
||||
# before except
|
||||
except (Exception, ValueError) as exc: # except line
|
||||
...
|
||||
# before except 2
|
||||
except KeyError as key: # except line 2
|
||||
...
|
||||
# in body 2
|
||||
# before else
|
||||
else:
|
||||
...
|
||||
# before finally
|
||||
finally:
|
||||
...
|
||||
|
||||
|
||||
# with line breaks
|
||||
try:
|
||||
# try
|
||||
...
|
||||
# end of body
|
||||
|
||||
# before except
|
||||
except (Exception, ValueError) as exc: # except line
|
||||
...
|
||||
|
||||
# before except 2
|
||||
except KeyError as key: # except line 2
|
||||
...
|
||||
# in body 2
|
||||
|
||||
# before else
|
||||
else:
|
||||
...
|
||||
|
||||
# before finally
|
||||
finally:
|
||||
...
|
||||
|
||||
|
||||
# with line breaks
|
||||
try:
|
||||
...
|
||||
|
||||
except:
|
||||
...
|
||||
|
||||
|
||||
try:
|
||||
...
|
||||
except (
|
||||
Exception,
|
||||
Exception,
|
||||
Exception,
|
||||
Exception,
|
||||
Exception,
|
||||
Exception,
|
||||
Exception,
|
||||
) as exc: # splits exception over multiple lines
|
||||
...
|
||||
|
||||
|
||||
try:
|
||||
...
|
||||
except:
|
||||
a = 10 # trailing comment1
|
||||
b = 11 # trailing comment2
|
||||
```
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue