Skip to content

Commit a7426c7

Browse files
ci(ruff): Enable rules for inline imports, prints, unused noqa and pytest.raises
Turn on PLC0415, T20, RUF100, PT011 and PT012. Existing violations get a noqa comment so new code is checked without a big cleanup first. RUF100 removes the noqa comments for rules that ruff doesn't enable, most of them left over from the flake8 plugins. Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent ae73d8c commit a7426c7

97 files changed

Lines changed: 363 additions & 366 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎pyproject.toml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ select = [
301301
"I",
302302
# Note: B and N rules are NOT enabled by default in flake8
303303
# They were only active through the plugins, which may not have been fully enabled
304+
"PLC0415", # Import not at the top level of the file
305+
"PT011", # pytest.raises() too broad
306+
"PT012", # pytest.raises() block with more than one statement
307+
"RUF100", # Unused noqa directive
308+
"T20", # print() and pprint() calls
304309
]
305310

306311
# Use ONLY the same ignores as the original flake8 config + compatibility for this codebase
@@ -317,6 +322,10 @@ ignore = [
317322
"E721", # Use isinstance instead of type() == - existing pattern in this codebase
318323
]
319324

325+
[tool.ruff.lint.per-file-ignores]
326+
"scripts/**" = ["T20"]
327+
"tests/**" = ["PLC0415", "T20"]
328+
320329
[tool.ruff.format]
321330
# ruff format already excludes the same files as specified in extend-exclude
322331
# Ensure Python 3.7 compatibility - avoid using Python 3.9+ syntax features

‎sentry_sdk/_compat.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def check_uwsgi_thread_support() -> bool:
3535
# --py-call-uwsgi-fork-hooks to force uWSGI to run regular cpython
3636
# after-fork hooks that take care of cleaning up stale thread data.
3737
try:
38-
from uwsgi import opt # type: ignore
38+
from uwsgi import opt # type: ignore # noqa: PLC0415
3939
except ImportError:
4040
return True
4141

42-
from sentry_sdk.consts import FALSE_VALUES
42+
from sentry_sdk.consts import FALSE_VALUES # noqa: PLC0415
4343

4444
def enabled(option: str) -> bool:
4545
value = opt.get(option, False)
@@ -61,7 +61,7 @@ def enabled(option: str) -> bool:
6161
lazy_mode = enabled("lazy-apps") or enabled("lazy")
6262

6363
if lazy_mode and not threads_enabled:
64-
from warnings import warn
64+
from warnings import warn # noqa: PLC0415
6565

6666
warn(
6767
Warning(
@@ -75,7 +75,7 @@ def enabled(option: str) -> bool:
7575
return False
7676

7777
elif not lazy_mode and (not threads_enabled or not fork_hooks_on):
78-
from warnings import warn
78+
from warnings import warn # noqa: PLC0415
7979

8080
warn(
8181
Warning(

‎sentry_sdk/_init_implementation.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _init(*args: "Optional[str]", **kwargs: "Any") -> "ContextManager[Any]":
6767
# Use `ClientConstructor` to define the argument types of `init` and
6868
# `ContextManager[Any]` to tell static analyzers about the return type.
6969

70-
class init(sentry_sdk.consts.ClientConstructor, _InitGuard): # noqa: N801
70+
class init(sentry_sdk.consts.ClientConstructor, _InitGuard):
7171
pass
7272

7373
else:

‎sentry_sdk/client.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def _record_lost_event(
653653

654654
# Setup Spotlight before creating batchers so _capture_envelope can use it.
655655
# setup_spotlight handles all config/env var resolution per the SDK spec.
656-
from sentry_sdk.spotlight import setup_spotlight
656+
from sentry_sdk.spotlight import setup_spotlight # noqa: PLC0415
657657

658658
self.spotlight = setup_spotlight(self.options)
659659
if self.spotlight is not None and not self.options["dsn"]:
@@ -1559,7 +1559,7 @@ async def __aexit__(self, exc_type: "Any", exc_value: "Any", tb: "Any") -> None:
15591559
# Use `ClientConstructor` to define the argument types of `init` and
15601560
# `Dict[str, Any]` to tell static analyzers about the return type.
15611561

1562-
class get_options(ClientConstructor, Dict[str, Any]): # noqa: N801
1562+
class get_options(ClientConstructor, Dict[str, Any]):
15631563
pass
15641564

15651565
class Client(ClientConstructor, _Client):

‎sentry_sdk/consts.py‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,10 +1388,10 @@ def __init__(
13881388
environment: "Optional[str]" = None,
13891389
server_name: "Optional[str]" = None,
13901390
shutdown_timeout: float = 2,
1391-
integrations: "Sequence[sentry_sdk.integrations.Integration]" = [], # noqa: B006
1391+
integrations: "Sequence[sentry_sdk.integrations.Integration]" = [],
13921392
ignore_spans: "Optional[IgnoreSpansConfig]" = None,
1393-
in_app_include: "List[str]" = [], # noqa: B006
1394-
in_app_exclude: "List[str]" = [], # noqa: B006
1393+
in_app_include: "List[str]" = [],
1394+
in_app_exclude: "List[str]" = [],
13951395
default_integrations: bool = True,
13961396
dist: "Optional[str]" = None,
13971397
transport: "Optional[Union[sentry_sdk.transport.Transport, Type[sentry_sdk.transport.Transport], Callable[[Event], None]]]" = None,
@@ -1401,7 +1401,7 @@ def __init__(
14011401
data_collection: "Optional[DataCollectionUserOptions]" = None,
14021402
http_proxy: "Optional[str]" = None,
14031403
https_proxy: "Optional[str]" = None,
1404-
ignore_errors: "Sequence[Union[type, str]]" = [], # noqa: B006
1404+
ignore_errors: "Sequence[Union[type, str]]" = [],
14051405
max_request_body_size: str = "medium",
14061406
socket_options: "Optional[List[Tuple[int, int, int | bytes]]]" = None,
14071407
keep_alive: "Optional[bool]" = None,
@@ -1423,18 +1423,16 @@ def __init__(
14231423
disabled_integrations: "Optional[Sequence[Union[sentry_sdk.integrations.Integration, type[sentry_sdk.integrations.Integration]]]]" = None,
14241424
auto_session_tracking: bool = True,
14251425
send_client_reports: bool = True,
1426-
_experiments: "Experiments" = {}, # noqa: B006
1426+
_experiments: "Experiments" = {},
14271427
proxy_headers: "Optional[Dict[str, str]]" = None,
14281428
instrumenter: "Optional[str]" = INSTRUMENTER.SENTRY,
14291429
before_send_transaction: "Optional[TransactionProcessor]" = None,
14301430
project_root: "Optional[str]" = None,
14311431
enable_tracing: "Optional[bool]" = None,
14321432
include_local_variables: "Optional[bool]" = True,
14331433
include_source_context: "Optional[bool]" = True,
1434-
trace_propagation_targets: "Optional[Sequence[str]]" = [ # noqa: B006
1435-
MATCH_ALL
1436-
],
1437-
functions_to_trace: "Sequence[Dict[str, str]]" = [], # noqa: B006
1434+
trace_propagation_targets: "Optional[Sequence[str]]" = [MATCH_ALL],
1435+
functions_to_trace: "Sequence[Dict[str, str]]" = [],
14381436
event_scrubber: "Optional[sentry_sdk.scrubber.EventScrubber]" = None,
14391437
max_value_length: "Optional[int]" = DEFAULT_MAX_VALUE_LENGTH,
14401438
enable_backpressure_handling: bool = True,
@@ -1912,7 +1910,7 @@ def __init__(
19121910

19131911

19141912
def _get_default_options() -> "dict[str, Any]":
1915-
import inspect
1913+
import inspect # noqa: PLC0415
19161914

19171915
a = inspect.getfullargspec(ClientConstructor.__init__)
19181916
defaults = a.defaults or ()

‎sentry_sdk/crons/decorator.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
R = TypeVar("R")
2727

2828

29-
class monitor: # noqa: N801
29+
class monitor:
3030
"""
3131
Decorator/context manager to capture checkin events for a monitor.
3232

‎sentry_sdk/integrations/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def iter_default_integrations(
3030
with_auto_enabling_integrations: bool,
3131
) -> "Iterator[Type[Integration]]":
3232
"""Returns an iterator of the default integration classes:"""
33-
from importlib import import_module
33+
from importlib import import_module # noqa: PLC0415
3434

3535
if with_auto_enabling_integrations:
3636
all_import_strings = integrations + auto_enabling_integrations
@@ -315,7 +315,7 @@ def _check_minimum_version(
315315
return
316316

317317

318-
class DidNotEnable(Exception): # noqa: N818
318+
class DidNotEnable(Exception):
319319
"""
320320
The integration could not be enabled due to a trivial user error like
321321
`flask` not being installed for the `FlaskIntegration`.

‎sentry_sdk/integrations/anthropic.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def setup_once() -> None:
281281
AsyncMessageStream.close = _wrap_async_close(AsyncMessageStream.close) # type: ignore[method-assign]
282282

283283
try:
284-
from functools import cached_property
284+
from functools import cached_property # noqa: PLC0415
285285
except ImportError:
286286
return
287287

‎sentry_sdk/integrations/asgi.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def _looks_like_asgi3(app: "Any") -> bool:
9797
elif inspect.isfunction(app):
9898
return iscoroutinefunction(app)
9999
else:
100-
call = getattr(app, "__call__", None) # noqa
100+
call = getattr(app, "__call__", None)
101101
return iscoroutinefunction(call)
102102

103103

‎sentry_sdk/integrations/beam.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BeamIntegration(Integration):
3232

3333
@staticmethod
3434
def setup_once() -> None:
35-
from apache_beam.transforms.core import DoFn, ParDo # type: ignore
35+
from apache_beam.transforms.core import DoFn, ParDo # type: ignore # noqa: PLC0415
3636

3737
ignore_logger("root")
3838
ignore_logger("bundle_processor.create")
@@ -94,11 +94,11 @@ def _inspect(self: "Any") -> "Any":
9494
# getfullargspec is deprecated in more recent beam versions and get_function_args_defaults
9595
# (which uses Signatures internally) should be used instead.
9696
try:
97-
from apache_beam.transforms.core import get_function_args_defaults
97+
from apache_beam.transforms.core import get_function_args_defaults # noqa: PLC0415
9898

9999
return get_function_args_defaults(process_func)
100100
except ImportError:
101-
from apache_beam.typehints.decorators import getfullargspec # type: ignore
101+
from apache_beam.typehints.decorators import getfullargspec # type: ignore # noqa: PLC0415
102102

103103
return getfullargspec(process_func)
104104

0 commit comments

Comments
 (0)