Skip to content

SIM115 should exempt unittest.TestCase.enterContext() and enterClassContext() #16438

@adamchainz

Description

@adamchainz

Summary

Like #1945, rule open-file-with-context-handler (SIM115) should exempt context managers passed to the TestCase.enterContext() and enterClassContext() methods. These methods methods act like with, so there’s no concern around the file not being closed.

Currently, an error is triggered for this code:



class ExampleTests(TestCase):
    def setUp(self):
        self.example_csv = self.enterContext(open("example.csv"))

Output:

example.py:6:46: SIM115 Use a context manager for opening files
  |
4 | class ExampleTests(TestCase):
5 |     def setUp(self):
6 |         self.example_csv = self.enterContext(open("example.csv"))
  |                                              ^^^^ SIM115
  |

Found 1 error.

Version

0.9.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedReady for implementationbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions