Skip to content

Python 3.11 improved tracebacks break the tests #2451

@hroncok

Description

@hroncok

search tried in the issue tracker

3.11

describe your issue

When we rebuild pre-commit in Fedora 37 with Python 3.11, we see the following failures:

=================================== FAILURES ===================================
________________________ test_error_handler_fatal_error ________________________
mocked_log_and_exit = <MagicMock name='_log_and_exit' id='140736176514768'>
    def test_error_handler_fatal_error(mocked_log_and_exit):
        exc = FatalError('just a test')
        with error_handler.error_handler():
            raise exc
    
        mocked_log_and_exit.assert_called_once_with(
            'An error has occurred',
            1,
            exc,
            # Tested below
            mock.ANY,
        )
    
        pattern = re_assert.Matches(
            r'Traceback \(most recent call last\):\n'
            r'  File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
            r'    yield\n'
            r'  File ".+tests.error_handler_test.py", line \d+, '
            r'in test_error_handler_fatal_error\n'
            r'    raise exc\n'
            r'(pre_commit\.errors\.)?FatalError: just a test\n',
        )
>       pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
tests/error_handler_test.py:53: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = Matches('Traceback \\(most recent call last\\):\\n  File ".+pre_commit.error_handler.py", line \\d+, in error_handler\...st_error_handler_fatal_error
    #>     raise exc
    #>     ^^^^^^^^^
    #> pre_commit.errors.FatalError: just a test
s = 'Traceback (most recent call last):\n  File "/builddir/build/BUILD/pre-commit-2.19.0/pre_commit/error_handler.py", lin... line 34, in test_error_handler_fatal_error\n    raise exc\n    ^^^^^^^^^\npre_commit.errors.FatalError: just a test\n'
    def assert_matches(self, s: str) -> None:
>       assert self == s, self._fail
E       AssertionError:  regex failed to match at:
E       
E       > Traceback (most recent call last):
E       >   File "/builddir/build/BUILD/pre-commit-2.19.0/pre_commit/error_handler.py", line 73, in error_handler
E       >     yield
E       >     ^^^^^
E           ^
E       >   File "/builddir/build/BUILD/pre-commit-2.19.0/tests/error_handler_test.py", line 34, in test_error_handler_fatal_error
E       >     raise exc
E       >     ^^^^^^^^^
E       > pre_commit.errors.FatalError: just a test
/usr/lib/python3.11/site-packages/re_assert.py:63: AssertionError
______________________ test_error_handler_uncaught_error _______________________
mocked_log_and_exit = <MagicMock name='_log_and_exit' id='140736174639952'>
    def test_error_handler_uncaught_error(mocked_log_and_exit):
        exc = ValueError('another test')
        with error_handler.error_handler():
            raise exc
    
        mocked_log_and_exit.assert_called_once_with(
            'An unexpected error has occurred',
            3,
            exc,
            # Tested below
            mock.ANY,
        )
        pattern = re_assert.Matches(
            r'Traceback \(most recent call last\):\n'
            r'  File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
            r'    yield\n'
            r'  File ".+tests.error_handler_test.py", line \d+, '
            r'in test_error_handler_uncaught_error\n'
            r'    raise exc\n'
            r'ValueError: another test\n',
        )
>       pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
tests/error_handler_test.py:77: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = Matches('Traceback \\(most recent call last\\):\\n  File ".+pre_commit.error_handler.py", line \\d+, in error_handler\...line 59, in test_error_handler_uncaught_error
    #>     raise exc
    #>     ^^^^^^^^^
    #> ValueError: another test
s = 'Traceback (most recent call last):\n  File "/builddir/build/BUILD/pre-commit-2.19.0/pre_commit/error_handler.py", lin...dler_test.py", line 59, in test_error_handler_uncaught_error\n    raise exc\n    ^^^^^^^^^\nValueError: another test\n'
    def assert_matches(self, s: str) -> None:
>       assert self == s, self._fail
E       AssertionError:  regex failed to match at:
E       
E       > Traceback (most recent call last):
E       >   File "/builddir/build/BUILD/pre-commit-2.19.0/pre_commit/error_handler.py", line 73, in error_handler
E       >     yield
E       >     ^^^^^
E           ^
E       >   File "/builddir/build/BUILD/pre-commit-2.19.0/tests/error_handler_test.py", line 59, in test_error_handler_uncaught_error
E       >     raise exc
E       >     ^^^^^^^^^
E       > ValueError: another test
/usr/lib/python3.11/site-packages/re_assert.py:63: AssertionError
_____________________ test_error_handler_keyboardinterrupt _____________________
mocked_log_and_exit = <MagicMock name='_log_and_exit' id='140736174647248'>
    def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
        exc = KeyboardInterrupt()
        with error_handler.error_handler():
            raise exc
    
        mocked_log_and_exit.assert_called_once_with(
            'Interrupted (^C)',
            130,
            exc,
            # Tested below
            mock.ANY,
        )
        pattern = re_assert.Matches(
            r'Traceback \(most recent call last\):\n'
            r'  File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
            r'    yield\n'
            r'  File ".+tests.error_handler_test.py", line \d+, '
            r'in test_error_handler_keyboardinterrupt\n'
            r'    raise exc\n'
            r'KeyboardInterrupt\n',
        )
>       pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
tests/error_handler_test.py:101: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = Matches('Traceback \\(most recent call last\\):\\n  File ".+pre_commit.error_handler.py", line \\d+, in error_handler\...y", line 83, in test_error_handler_keyboardinterrupt
    #>     raise exc
    #>     ^^^^^^^^^
    #> KeyboardInterrupt
s = 'Traceback (most recent call last):\n  File "/builddir/build/BUILD/pre-commit-2.19.0/pre_commit/error_handler.py", lin..._handler_test.py", line 83, in test_error_handler_keyboardinterrupt\n    raise exc\n    ^^^^^^^^^\nKeyboardInterrupt\n'
    def assert_matches(self, s: str) -> None:
>       assert self == s, self._fail
E       AssertionError:  regex failed to match at:
E       
E       > Traceback (most recent call last):
E       >   File "/builddir/build/BUILD/pre-commit-2.19.0/pre_commit/error_handler.py", line 73, in error_handler
E       >     yield
E       >     ^^^^^
E           ^
E       >   File "/builddir/build/BUILD/pre-commit-2.19.0/tests/error_handler_test.py", line 83, in test_error_handler_keyboardinterrupt
E       >     raise exc
E       >     ^^^^^^^^^
E       > KeyboardInterrupt
/usr/lib/python3.11/site-packages/re_assert.py:63: AssertionError

I believe all of them are caused by extra ^^^^^^^^^ in the tracebacks.

I might be able to submit a PR for this if you are interested.

pre-commit --version

2.19.0

.pre-commit-config.yaml

not relevant

~/.cache/pre-commit/pre-commit.log (if present)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions