-
Notifications
You must be signed in to change notification settings - Fork 705
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests to validate exception formatting of new Python syntax
These tests ensure that there are no unexpected errors during syntax highlighting and local variable collection. However, this also shows that the implementation can be improved. Some new keywords (such as "match") are not recognized. The addition of "--force-exclude" is necessary to prevent "black" formatter from failing (despite "fmt: off") due to a parsing error (caused by "--target-version py35" when the file contains a new syntax).
- Loading branch information
Showing
14 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
+ [33m[1mException Group Traceback (most recent call last):[0m | ||
| | ||
| File "[32mtests/exceptions/source/modern/[0m[32m[1mexception_group_catch.py[0m", line [33m14[0m, in [35ma[0m | ||
| [35m[1mraise[0m [1mExceptionGroup[0m[1m([0m[36m"group"[0m[1m,[0m [1m[[0m[1mValueError[0m[1m([0m[34m[1m1[0m[1m)[0m[1m][0m[1m)[0m | ||
| | ||
| [31m[1mExceptionGroup[0m:[1m group (1 sub-exception)[0m | ||
+-+---------------- 1 ---------------- | ||
| [31m[1mValueError[0m:[1m 1[0m | ||
+------------------------------------ | ||
|
||
|
||
[1mDuring handling of the above exception, another exception occurred:[0m | ||
|
||
|
||
[33m[1mTraceback (most recent call last):[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mexception_group_catch.py[0m", line [33m25[0m, in [35m<module>[0m | ||
[1mb[0m[1m([0m[1m)[0m | ||
[36m└ [0m[36m[1m<function b at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mexception_group_catch.py[0m", line [33m21[0m, in [35mb[0m | ||
[35m[1mexcept[0m[35m[1m*[0m [1mTypeError[0m[1m:[0m [1ma[0m[1m([0m[1m)[0m | ||
[36m └ [0m[36m[1m<function a at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mexception_group_catch.py[0m", line [33m15[0m, in [35ma[0m | ||
[35m[1mexcept[0m[35m[1m*[0m [1mx[0m [35m[1mas[0m [1me[0m[1m:[0m [35m[1mraise[0m [1mValueError[0m[1m([0m[34m[1m2[0m[1m)[0m | ||
[36m └ [0m[36m[1m<class 'ValueError'>[0m | ||
|
||
[31m[1mValueError[0m:[1m 2[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
[33m[1mTraceback (most recent call last):[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mf_string.py[0m", line [33m17[0m, in [35m<module>[0m | ||
[1mhello[0m[1m([0m[1m)[0m | ||
[36m└ [0m[36m[1m<function hello at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mf_string.py[0m", line [33m13[0m, in [35mhello[0m | ||
[36mf"{name + f}"[0m [35m[1mor[0m [36mf'{{ {10*10} }}'[0m | ||
|
||
[31m[1mTypeError[0m:[1m can only concatenate str (not "int") to str[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
[33m[1mTraceback (most recent call last):[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mmatch_statement.py[0m", line [33m21[0m, in [35m<module>[0m | ||
[1mmatch[0m[1m([0m[34m[1m1[0m[1m)[0m | ||
[36m└ [0m[36m[1m<function match at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mmatch_statement.py[0m", line [33m18[0m, in [35mmatch[0m | ||
[1mcase[0m [1my[0m[1m:[0m [1mcase[0m[1m([0m[1mx[0m[1m)[0m | ||
[36m│ │ │ └ [0m[36m[1m1[0m | ||
[36m│ │ └ [0m[36m[1m<function case at 0xDEADBEEF>[0m | ||
[36m│ └ [0m[36m[1m1[0m | ||
[36m└ [0m[36m[1m<function case at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mmatch_statement.py[0m", line [33m11[0m, in [35mcase[0m | ||
[1mmatch[0m [1my[0m [35m[1m/[0m [34m[1m0[0m[1m:[0m | ||
[36m│ └ [0m[36m[1m1[0m | ||
[36m└ [0m[36m[1m<function match at 0xDEADBEEF>[0m | ||
|
||
[31m[1mZeroDivisionError[0m:[1m division by zero[0m |
20 changes: 20 additions & 0 deletions
20
tests/exceptions/output/modern/positional_only_argument.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
[33m[1mTraceback (most recent call last):[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mpositional_only_argument.py[0m", line [33m23[0m, in [35m<module>[0m | ||
[1mmain[0m[1m([0m[1m)[0m | ||
[36m└ [0m[36m[1m<function main at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mpositional_only_argument.py[0m", line [33m19[0m, in [35mmain[0m | ||
[1mfoo[0m[1m([0m[34m[1m1[0m[1m,[0m [34m[1m2[0m[1m,[0m [1mc[0m[35m[1m=[0m[34m[1m3[0m[1m)[0m | ||
[36m└ [0m[36m[1m<function foo at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mpositional_only_argument.py[0m", line [33m15[0m, in [35mfoo[0m | ||
[35m[1mdef[0m [1mfoo[0m[1m([0m[1ma[0m[1m,[0m [35m[1m/[0m[1m,[0m [1mb[0m[1m,[0m [35m[1m*[0m[1m,[0m [1mc[0m[1m,[0m [35m[1m**[0m[1md[0m[1m)[0m[1m:[0m [34m[1m1[0m [35m[1m/[0m [34m[1m0[0m | ||
[36m │ │ │ │ └ [0m[36m[1m{}[0m | ||
[36m │ │ │ └ [0m[36m[1m3[0m | ||
[36m │ │ └ [0m[36m[1m2[0m | ||
[36m │ └ [0m[36m[1m1[0m | ||
[36m └ [0m[36m[1m<function foo at 0xDEADBEEF>[0m | ||
|
||
[31m[1mZeroDivisionError[0m:[1m division by zero[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
[33m[1mTraceback (most recent call last):[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mtype_hints.py[0m", line [33m23[0m, in [35m<module>[0m | ||
[1mmain[0m[1m([0m[1m)[0m | ||
[36m└ [0m[36m[1m<function main at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mtype_hints.py[0m", line [33m19[0m, in [35mmain[0m | ||
[1mbar[0m[1m:[0m [1mName[0m [35m[1m=[0m [1mfoo[0m[1m([0m[34m[1m1[0m[1m,[0m [34m[1m2[0m[1m,[0m [34m[1m3[0m[1m)[0m | ||
[36m └ [0m[36m[1m<function foo at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mtype_hints.py[0m", line [33m15[0m, in [35mfoo[0m | ||
[35m[1mdef[0m [1mfoo[0m[1m([0m[1ma[0m[1m:[0m [1mint[0m[1m,[0m [1mb[0m[1m:[0m [1mUnion[0m[1m[[0m[1mName[0m[1m,[0m [1mfloat[0m[1m][0m[1m,[0m [1mc[0m[1m:[0m [36m"Name"[0m[1m)[0m [35m[1m->[0m [1mT[0m[1m:[0m [34m[1m1[0m [35m[1m/[0m [34m[1m0[0m | ||
[36m │ │ │ │ │ │ └ [0m[36m[1m~T[0m | ||
[36m │ │ │ │ │ └ [0m[36m[1m3[0m | ||
[36m │ │ │ │ └ [0m[36m[1m<class 'str'>[0m | ||
[36m │ │ │ └ [0m[36m[1mtyping.Union[0m | ||
[36m │ │ └ [0m[36m[1m2[0m | ||
[36m │ └ [0m[36m[1m1[0m | ||
[36m └ [0m[36m[1m<function foo at 0xDEADBEEF>[0m | ||
|
||
[31m[1mZeroDivisionError[0m:[1m division by zero[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
[33m[1mTraceback (most recent call last):[0m | ||
|
||
> File "[32mtests/exceptions/source/modern/[0m[32m[1mwalrus_operator.py[0m", line [33m25[0m, in [35m<module>[0m | ||
[1mmain[0m[1m([0m[1m)[0m | ||
[36m└ [0m[36m[1m<function main at 0xDEADBEEF>[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mwalrus_operator.py[0m", line [33m19[0m, in [35mmain[0m | ||
[1m([0m[1mwalrus[0m [35m[1m:=[0m [1mfoo[0m[1m([0m[1m)[0m[1m)[0m | ||
[36m │ └ [0m[36m[1m<function foo at 0xDEADBEEF>[0m | ||
[36m └ [0m[36m[1mFalse[0m | ||
|
||
File "[32mtests/exceptions/source/modern/[0m[32m[1mwalrus_operator.py[0m", line [33m8[0m, in [35mfoo[0m | ||
[35m[1mif[0m [1ma[0m [35m[1m:=[0m [36m"a"[0m [35m[1m+[0m [1m([0m[1mx[0m[35m[1m:=[0m[34m[1m1[0m[1m)[0m[1m:[0m | ||
[36m └ [0m[36m[1m1[0m | ||
|
||
[31m[1mTypeError[0m:[1m can only concatenate str (not "int") to str[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# fmt: off | ||
import sys | ||
|
||
from loguru import logger | ||
|
||
logger.remove() | ||
logger.add(sys.stderr, format="", colorize=True, backtrace=False, diagnose=True) | ||
|
||
x = ValueError | ||
|
||
|
||
def a(): | ||
try: | ||
raise ExceptionGroup("group", [ValueError(1)]) | ||
except* x as e: raise ValueError(2) | ||
|
||
|
||
def b(): | ||
try: | ||
raise ExceptionGroup("group", [TypeError(1)]) | ||
except* TypeError: a() | ||
|
||
|
||
with logger.catch(): | ||
b() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# fmt: off | ||
import sys | ||
|
||
from loguru import logger | ||
|
||
logger.remove() | ||
logger.add(sys.stderr, format="", colorize=True, backtrace=False, diagnose=True) | ||
|
||
|
||
def hello(): | ||
name = "world" | ||
f = 1 | ||
f"{name + f}" or f'{{ {10*10} }}' | ||
|
||
|
||
with logger.catch(): | ||
hello() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# fmt: off | ||
import sys | ||
|
||
from loguru import logger | ||
|
||
logger.remove() | ||
logger.add(sys.stderr, format="", colorize=True, backtrace=False, diagnose=True) | ||
|
||
def case(x): | ||
y = 1 | ||
match y / 0: | ||
case 1: | ||
pass | ||
|
||
def match(x): | ||
y = 1 | ||
match x: | ||
case y: case(x) | ||
|
||
with logger.catch(): | ||
match(1) |
23 changes: 23 additions & 0 deletions
23
tests/exceptions/source/modern/positional_only_argument.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# fmt: off | ||
import sys | ||
from typing import TypeVar, Union | ||
|
||
from loguru import logger | ||
|
||
logger.remove() | ||
logger.add(sys.stderr, format="", colorize=True, backtrace=False, diagnose=True) | ||
|
||
|
||
T = TypeVar("T") | ||
Name = str | ||
|
||
|
||
def foo(a, /, b, *, c, **d): 1 / 0 | ||
|
||
|
||
def main(): | ||
foo(1, 2, c=3) | ||
|
||
|
||
with logger.catch(): | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# fmt: off | ||
import sys | ||
from typing import TypeVar, Union | ||
|
||
from loguru import logger | ||
|
||
logger.remove() | ||
logger.add(sys.stderr, format="", colorize=True, backtrace=False, diagnose=True) | ||
|
||
|
||
T = TypeVar("T") | ||
Name = str | ||
|
||
|
||
def foo(a: int, b: Union[Name, float], c: "Name") -> T: 1 / 0 | ||
|
||
|
||
def main(): | ||
bar: Name = foo(1, 2, 3) | ||
|
||
|
||
with logger.catch(): | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# fmt: off | ||
import sys | ||
|
||
from loguru import logger | ||
|
||
|
||
def foo(): | ||
if a := "a" + (x:=1): | ||
pass | ||
|
||
|
||
def bar(): | ||
return [y for x in [1, 2] if (y := foo()) != 0] | ||
|
||
|
||
@logger.catch | ||
def main(): | ||
walrus = False | ||
(walrus := foo()) | ||
|
||
|
||
logger.remove() | ||
logger.add(sys.stderr, format="", diagnose=True, backtrace=True, colorize=True) | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters