ruff/crates/ruff_linter/resources/test/fixtures/tryceratops/TRY400.py
Charlie Marsh bc9b4571eb
Avoid failures due to non-deterministic binding ordering (#10478)
## Summary

We're seeing failures in https://github.com/astral-sh/ruff/issues/10470
because `resolve_qualified_import_name` isn't guaranteed to return a
specific import if a symbol is accessible in two ways (e.g., you have
both `import logging` and `from logging import error` in scope, and you
want `logging.error`). This PR breaks up the failing tests such that the
imports aren't in the same scope.

Closes https://github.com/astral-sh/ruff/issues/10470.

## Test Plan

I added a `bindings.reverse()` to `resolve_qualified_import_name` to
ensure that the tests pass regardless of the binding order.
2024-03-19 18:01:33 +00:00

143 lines
2.3 KiB
Python

"""
Violation:
Use '.exception' over '.error' inside except blocks
"""
def bad():
import logging
try:
a = 1
except Exception:
logging.error("Context message here")
if True:
logging.error("Context message here")
def bad():
import logging
logger = logging.getLogger(__name__)
try:
a = 1
except Exception:
logger.error("Context message here")
if True:
logger.error("Context message here")
def bad():
try:
a = 1
except Exception:
log.error("Context message here")
if True:
log.error("Context message here")
def bad():
try:
a = 1
except Exception:
self.logger.error("Context message here")
if True:
self.logger.error("Context message here")
def good():
import logging
logger = logging.getLogger(__name__)
try:
a = 1
except Exception:
logger.exception("Context message here")
def good():
try:
a = 1
except Exception:
foo.exception("Context message here")
def fine():
import logging
logger = logging.getLogger(__name__)
try:
a = 1
except Exception:
logger.error("Context message here", exc_info=True)
def fine():
import logging
import sys
logger = logging.getLogger(__name__)
try:
a = 1
except Exception:
logger.error("Context message here", exc_info=sys.exc_info())
def bad():
from logging import error, exception
try:
a = 1
except Exception:
error("Context message here")
if True:
error("Context message here")
def good():
from logging import error, exception
try:
a = 1
except Exception:
exception("Context message here")
def fine():
from logging import error, exception
try:
a = 1
except Exception:
error("Context message here", exc_info=True)
def fine():
from logging import error, exception
import sys
try:
a = 1
except Exception:
error("Context message here", exc_info=sys.exc_info())
def nested():
from logging import error, exception
try:
a = 1
except Exception:
try:
b = 2
except Exception:
error("Context message here")