Skip to content

Commit

Permalink
Fixed formatting tests failing on Python 3.13
Browse files Browse the repository at this point in the history
Fixes #122.
  • Loading branch information
agronholm committed Jul 12, 2024
1 parent eb8fbbc commit bb43ee0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", pypy-3.10]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13", pypy-3.10]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ warn_unused_ignores = true
[tool.tox]
legacy_tox_ini = """
[tox]
envlist = py37, py38, py39, py310, py311, py312, pypy3
envlist = py37, py38, py39, py310, py311, py312, py313, pypy3
labels =
typing = py{310,311,312}-typing
skip_missing_interpreters = true
Expand Down
38 changes: 29 additions & 9 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ def test_exceptionhook(capsys: CaptureFixture) -> None:
local_lineno = test_exceptionhook.__code__.co_firstlineno
lineno = raise_excgroup.__code__.co_firstlineno
module_prefix = "" if sys.version_info >= (3, 11) else "exceptiongroup."
underline_suffix = (
"" if sys.version_info < (3, 13) else "\n | ~~~~~~~~~~~~~~^^"
)
output = capsys.readouterr().err
assert output == (
f"""\
+ Exception Group Traceback (most recent call last):
| File "{__file__}", line {local_lineno + 2}, in test_exceptionhook
| raise_excgroup()
| raise_excgroup(){underline_suffix}
| File "{__file__}", line {lineno + 15}, in raise_excgroup
| raise exc
| {module_prefix}ExceptionGroup: test message (2 sub-exceptions)
Expand Down Expand Up @@ -163,13 +166,16 @@ def test_exceptionhook_format_exception_only(capsys: CaptureFixture) -> None:
local_lineno = test_exceptionhook_format_exception_only.__code__.co_firstlineno
lineno = raise_excgroup.__code__.co_firstlineno
module_prefix = "" if sys.version_info >= (3, 11) else "exceptiongroup."
underline_suffix = (
"" if sys.version_info < (3, 13) else "\n | ~~~~~~~~~~~~~~^^"
)
output = capsys.readouterr().err
assert output == (
f"""\
+ Exception Group Traceback (most recent call last):
| File "{__file__}", line {local_lineno + 2}, in \
test_exceptionhook_format_exception_only
| raise_excgroup()
| raise_excgroup(){underline_suffix}
| File "{__file__}", line {lineno + 15}, in raise_excgroup
| raise exc
| {module_prefix}ExceptionGroup: test message (2 sub-exceptions)
Expand Down Expand Up @@ -204,13 +210,14 @@ def test_formatting_syntax_error(capsys: CaptureFixture) -> None:
underline = "\n ^"

lineno = test_formatting_syntax_error.__code__.co_firstlineno
underline_suffix = "" if sys.version_info < (3, 13) else "\n ~~~~^^^^^^^^^^^^"
output = capsys.readouterr().err
assert output == (
f"""\
Traceback (most recent call last):
File "{__file__}", line {lineno + 2}, \
in test_formatting_syntax_error
exec("//serser")
exec("//serser"){underline_suffix}
File "<string>", line 1
//serser{underline}
SyntaxError: invalid syntax
Expand Down Expand Up @@ -254,11 +261,14 @@ def test_format_exception(
lineno = raise_excgroup.__code__.co_firstlineno
assert isinstance(lines, list)
module_prefix = "" if sys.version_info >= (3, 11) else "exceptiongroup."
underline_suffix = (
"" if sys.version_info < (3, 13) else "\n | ~~~~~~~~~~~~~~^^"
)
assert "".join(lines) == (
f"""\
+ Exception Group Traceback (most recent call last):
| File "{__file__}", line {local_lineno + 25}, in test_format_exception
| raise_excgroup()
| raise_excgroup(){underline_suffix}
| File "{__file__}", line {lineno + 15}, in raise_excgroup
| raise exc
| {module_prefix}ExceptionGroup: test message (2 sub-exceptions)
Expand Down Expand Up @@ -303,6 +313,10 @@ def raise_exc(max_level: int, level: int = 1) -> NoReturn:
except Exception as exc:
lines = format_exception(type(exc), exc, exc.__traceback__)

underline_suffix1 = (
"" if sys.version_info < (3, 13) else "\n ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^"
)
underline_suffix2 = "" if sys.version_info < (3, 13) else "\n ~~~~~~~~~^^^"
local_lineno = test_format_nested.__code__.co_firstlineno + 20
raise_exc_lineno1 = raise_exc.__code__.co_firstlineno + 2
raise_exc_lineno2 = raise_exc.__code__.co_firstlineno + 5
Expand All @@ -312,7 +326,7 @@ def raise_exc(max_level: int, level: int = 1) -> NoReturn:
f"""\
Traceback (most recent call last):
File "{__file__}", line {raise_exc_lineno2}, in raise_exc
raise_exc(max_level, level + 1)
raise_exc(max_level, level + 1){underline_suffix1}
File "{__file__}", line {raise_exc_lineno1}, in raise_exc
raise Exception(f"LEVEL_{{level}}")
Exception: LEVEL_3
Expand All @@ -321,7 +335,7 @@ def raise_exc(max_level: int, level: int = 1) -> NoReturn:
Traceback (most recent call last):
File "{__file__}", line {raise_exc_lineno2}, in raise_exc
raise_exc(max_level, level + 1)
raise_exc(max_level, level + 1){underline_suffix1}
File "{__file__}", line {raise_exc_lineno3}, in raise_exc
raise Exception(f"LEVEL_{{level}}")
Exception: LEVEL_2
Expand All @@ -330,7 +344,7 @@ def raise_exc(max_level: int, level: int = 1) -> NoReturn:
Traceback (most recent call last):
File "{__file__}", line {local_lineno}, in test_format_nested
raise_exc(3)
raise_exc(3){underline_suffix2}
File "{__file__}", line {raise_exc_lineno3}, in raise_exc
raise Exception(f"LEVEL_{{level}}")
Exception: LEVEL_1
Expand Down Expand Up @@ -388,12 +402,15 @@ def test_print_exception(
local_lineno = test_print_exception.__code__.co_firstlineno
lineno = raise_excgroup.__code__.co_firstlineno
module_prefix = "" if sys.version_info >= (3, 11) else "exceptiongroup."
underline_suffix = (
"" if sys.version_info < (3, 13) else "\n | ~~~~~~~~~~~~~~^^"
)
output = capsys.readouterr().err
assert output == (
f"""\
+ Exception Group Traceback (most recent call last):
| File "{__file__}", line {local_lineno + 13}, in test_print_exception
| raise_excgroup()
| raise_excgroup(){underline_suffix}
| File "{__file__}", line {lineno + 15}, in raise_excgroup
| raise exc
| {module_prefix}ExceptionGroup: test message (2 sub-exceptions)
Expand Down Expand Up @@ -433,12 +450,15 @@ def test_print_exc(
local_lineno = test_print_exc.__code__.co_firstlineno
lineno = raise_excgroup.__code__.co_firstlineno
module_prefix = "" if sys.version_info >= (3, 11) else "exceptiongroup."
underline_suffix = (
"" if sys.version_info < (3, 13) else "\n | ~~~~~~~~~~~~~~^^"
)
output = capsys.readouterr().err
assert output == (
f"""\
+ Exception Group Traceback (most recent call last):
| File "{__file__}", line {local_lineno + 13}, in test_print_exc
| raise_excgroup()
| raise_excgroup(){underline_suffix}
| File "{__file__}", line {lineno + 15}, in raise_excgroup
| raise exc
| {module_prefix}ExceptionGroup: test message (2 sub-exceptions)
Expand Down

0 comments on commit bb43ee0

Please sign in to comment.