Skip to content

Rewrite exception references in PT027 fix #8650

@spaceone

Description

@spaceone
from unittest import TestCase, main


class TestFoo(TestCase):

    def test_foo(self):
        with self.assertRaises(Exception) as exc:
            raise Exception('foo')
        assert str(exc.exception) == 'foo'


main()

gets converted by ruff --fix --select PT027 --unsafe-fixes foo.py into:

from unittest import TestCase, main
import pytest


class TestFoo(TestCase):

    def test_foo(self):
        with pytest.raises(Exception) as exc:
            raise Exception('foo')
        assert str(exc.exception) == 'foo'


main()

causing:

E
======================================================================
ERROR: test_foo (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "foo.py", line 10, in test_foo
    assert str(exc.exception) == 'foo'
AttributeError: 'ExceptionInfo' object has no attribute 'exception'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

correct would be:

from unittest import TestCase, main
import pytest


class TestFoo(TestCase):

    def test_foo(self):
        with pytest.raises(Exception) as exc:
            raise Exception('foo')
        assert str(exc.value) == 'foo'


main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions