Skip to content

Simplify Single Exception Tuple

Sourcery refactoring id: simplify-single-exception-tuple

Description

Replace length-one exception tuple with exception.

Before

try: read_file() except (FileNotFoundError,) as e: log_error(e) create_file() 

After

try: read_file() except FileNotFoundError as e: log_error(e) create_file() 

Explanation

Python supports catching multiple exception types at once by using tuples. However, when only a single exception type is being handled, there is no need to wrap it in a tuple.

Using the exception type itself makes your code easier to read, and your intent clearer.