An exception type is already caught by an earlier except clause

tx0/python:except-shadoweddefaultwarningbug

What it finds

except OSError: ... followed by except FileNotFoundError: ...: handlers are tried in order, and the first one already catches every FileNotFoundError, so the second never runs.

Only builtin exceptions are compared, by their place in Python's own hierarchy; for a type of the project or of a library, the base classes are not known here. A type is reported when an earlier handler of the same try names it or one of its bases.

From the rule's source, except_shadowed.rs.

Example from the rule's tests
def read(path):    try:        return open(path).read()    except OSError:        return None    except FileNotFoundError:tx0 reports: FileNotFoundError is already caught by the earlier clause for OSError        return ""    except (ValueError, IOError):tx0 reports: IOError is already caught by the earlier clause for OSError        return ""  def convert(text, Custom):    try:        return int(text)    except ValueError:        return 0    except ValueError:tx0 reports: ValueError is already caught by an earlier clause        return 1    except Custom:        return 2    except LookupError:        return 3    except KeyError:tx0 reports: KeyError is already caught by the earlier clause for LookupError        return 4
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/python:except-shadowed" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:except-shadowed] reason="..."

tx0 explain tx0/python:except-shadowed shows the same in the terminal. Found a false positive? Report it.