Skip to content

Commit ca81892

Browse files
committed
chore: add flake8 linter
1 parent 204db44 commit ca81892

30 files changed

+419
-411
lines changed

.flake8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
max-line-length = 80
3+
select = C,E,F,W,B,B950
4+
ignore = E501, E402

sentry_minimal.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def get_current_hub():
5757
from sentry_sdk.hub import Hub
5858
from sentry_sdk.scope import Scope
5959
except ImportError:
60+
6061
class Hub(object):
6162
current = main = None
6263

sentry_sdk/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .api import *
2-
from .api import __all__
1+
from .api import * # noqa
2+
from .api import __all__ # noqa

sentry_sdk/_compat.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,33 @@
44
PY2 = sys.version_info[0] == 2
55

66
if PY2:
7-
import urlparse
8-
text_type = unicode
9-
import Queue as queue
10-
number_types = (int, long, float)
7+
import urlparse # noqa
8+
9+
text_type = unicode # noqa
10+
import Queue as queue # noqa
11+
12+
number_types = (int, long, float) # noqa
1113

1214
def implements_str(cls):
1315
cls.__unicode__ = cls.__str__
14-
cls.__str__ = lambda x: unicode(x).encode('utf-8')
16+
cls.__str__ = lambda x: unicode(x).encode("utf-8") # noqa
1517
return cls
18+
19+
1620
else:
17-
import urllib.parse as urlparse
18-
import queue
21+
import urllib.parse as urlparse # noqa
22+
import queue # noqa
23+
1924
text_type = str
20-
implements_str = lambda x: x
2125
number_types = (int, float)
2226

27+
def implements_str(x):
28+
return x
29+
2330

2431
def with_metaclass(meta, *bases):
2532
class metaclass(type):
2633
def __new__(cls, name, this_bases, d):
2734
return meta(name, bases, d)
28-
return type.__new__(metaclass, 'temporary_class', (), {})
35+
36+
return type.__new__(metaclass, "temporary_class", (), {})

sentry_sdk/api.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
from contextlib import contextmanager
2-
31
from .hub import Hub
4-
from .scope import Scope
52
from .client import Client
63

74

8-
95
class _InitGuard(object):
10-
116
def __init__(self, client):
127
self._client = client
138

@@ -29,7 +24,7 @@ def init(*args, **kwargs):
2924

3025
import sentry_minimal
3126

32-
__all__ = ['Hub', 'Scope', 'Client', 'init'] + sentry_minimal.__all__
27+
__all__ = ["Hub", "Scope", "Client", "init"] + sentry_minimal.__all__
3328

3429

3530
for _key in sentry_minimal.__all__:

sentry_sdk/client.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111

1212

1313
class Client(object):
14-
1514
def __init__(self, dsn=None, *args, **kwargs):
1615
if dsn is NO_DSN:
1716
dsn = None
1817
else:
1918
if dsn is None:
20-
dsn = os.environ.get('SENTRY_DSN')
19+
dsn = os.environ.get("SENTRY_DSN")
2120
if not dsn:
2221
dsn = None
2322
else:
@@ -43,20 +42,20 @@ def disabled(cls):
4342
return cls(NO_DSN)
4443

4544
def _prepare_event(self, event, scope):
46-
if event.get('event_id') is None:
47-
event['event_id'] = uuid.uuid4().hex
45+
if event.get("event_id") is None:
46+
event["event_id"] = uuid.uuid4().hex
4847

4948
if scope is not None:
5049
scope.apply_to_event(event)
5150

52-
for key in 'release', 'environment', 'server_name':
51+
for key in "release", "environment", "server_name":
5352
if event.get(key) is None:
5453
event[key] = self.options[key]
55-
if event.get('sdk') is None:
56-
event['sdk'] = SDK_INFO
54+
if event.get("sdk") is None:
55+
event["sdk"] = SDK_INFO
5756

58-
if event.get('platform') is None:
59-
event['platform'] = 'python'
57+
if event.get("platform") is None:
58+
event["platform"] = "python"
6059

6160
event = strip_event(event)
6261
event = flatten_metadata(event)
@@ -71,7 +70,7 @@ def capture_event(self, event, scope=None):
7170

7271
def drain_events(self, timeout=None):
7372
if timeout is None:
74-
timeout = self.options['drain_timeout']
73+
timeout = self.options["drain_timeout"]
7574
if self._transport is not None:
7675
self._transport.drain_events(timeout)
7776

sentry_sdk/consts.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import socket
22

3-
VERSION = '0.1'
4-
DEFAULT_SERVER_NAME = socket.gethostname() if hasattr(socket, 'gethostname') else None
3+
VERSION = "0.1"
4+
DEFAULT_SERVER_NAME = socket.gethostname() if hasattr(socket, "gethostname") else None
55
DEFAULT_OPTIONS = {
6-
'with_locals': True,
7-
'max_breadcrumbs': 100,
8-
'release': None,
9-
'environment': None,
10-
'server_name': DEFAULT_SERVER_NAME,
11-
'drain_timeout': 2.0,
6+
"with_locals": True,
7+
"max_breadcrumbs": 100,
8+
"release": None,
9+
"environment": None,
10+
"server_name": DEFAULT_SERVER_NAME,
11+
"drain_timeout": 2.0,
1212
}
1313

14-
SDK_INFO = {
15-
'name': 'sentry-python',
16-
'version': VERSION,
17-
}
14+
SDK_INFO = {"name": "sentry-python", "version": VERSION}

sentry_sdk/hub.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import sys
22
import copy
3-
import linecache
43
from contextlib import contextmanager
54

65
from ._compat import with_metaclass
76
from .scope import Scope
8-
from .utils import exceptions_from_error_tuple, create_event, \
9-
skip_internal_frames, ContextVar
7+
from .utils import (
8+
exceptions_from_error_tuple,
9+
create_event,
10+
skip_internal_frames,
11+
ContextVar,
12+
)
1013

1114

12-
_local = ContextVar('sentry_current_hub')
15+
_local = ContextVar("sentry_current_hub")
1316

1417

1518
class HubMeta(type):
16-
1719
@property
1820
def current(self):
1921
rv = _local.get(None)
@@ -28,7 +30,6 @@ def main(self):
2830

2931

3032
class _HubManager(object):
31-
3233
def __init__(self, hub):
3334
self._old = Hub.current
3435
_local.set(hub)
@@ -38,7 +39,6 @@ def __exit__(self, exc_type, exc_value, tb):
3839

3940

4041
class _ScopeManager(object):
41-
4242
def __init__(self, hub, layer):
4343
self._hub = hub
4444
self._layer = layer
@@ -47,11 +47,10 @@ def __enter__(self):
4747
return self
4848

4949
def __exit__(self, exc_type, exc_value, tb):
50-
assert self._hub._stack.pop() == self._layer, 'popped wrong scope'
50+
assert self._hub._stack.pop() == self._layer, "popped wrong scope"
5151

5252

5353
class Hub(with_metaclass(HubMeta)):
54-
5554
def __init__(self, client_or_hub=None, scope=None):
5655
if isinstance(client_or_hub, Hub):
5756
hub = client_or_hub
@@ -92,18 +91,18 @@ def capture_event(self, event):
9291
client, scope = self._stack[-1]
9392
if client is not None:
9493
client.capture_event(event, scope)
95-
return event.get('event_id')
94+
return event.get("event_id")
9695

9796
def capture_message(self, message, level=None):
9897
"""Captures a message."""
9998
if self.client is None:
10099
return
101100
if level is None:
102-
level = 'info'
101+
level = "info"
103102
event = create_event()
104-
event['message'] = message
103+
event["message"] = message
105104
if level is not None:
106-
event['level'] = level
105+
event["level"] = level
107106
return self.capture_event(event)
108107

109108
def capture_exception(self, error=None):
@@ -116,7 +115,7 @@ def capture_exception(self, error=None):
116115
elif isinstance(error, tuple) and len(error) == 3:
117116
exc_type, exc_value, tb = error
118117
else:
119-
tb = getattr(error, '__traceback__', None)
118+
tb = getattr(error, "__traceback__", None)
120119
if tb is not None:
121120
exc_type = type(error)
122121
exc_value = error
@@ -132,9 +131,10 @@ def capture_exception(self, error=None):
132131

133132
event = create_event()
134133
try:
135-
event['exception'] = {
136-
'values': exceptions_from_error_tuple(
137-
exc_type, exc_value, tb, client.options['with_locals'])
134+
event["exception"] = {
135+
"values": exceptions_from_error_tuple(
136+
exc_type, exc_value, tb, client.options["with_locals"]
137+
)
138138
}
139139
return self.capture_event(event)
140140
except Exception:
@@ -154,7 +154,7 @@ def add_breadcrumb(self, crumb):
154154
crumb = crumb()
155155
if crumb is not None:
156156
scope._breadcrumbs.append(crumb)
157-
while len(scope._breadcrumbs) >= client.options['max_breadcrumbs']:
157+
while len(scope._breadcrumbs) >= client.options["max_breadcrumbs"]:
158158
scope._breadcrumbs.popleft()
159159

160160
def add_event_processor(self, factory):

sentry_sdk/integrations/_wsgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ def get_environ(environ):
22
"""
33
Returns our whitelisted environment variables.
44
"""
5-
for key in ('REMOTE_ADDR', 'SERVER_NAME', 'SERVER_PORT'):
5+
for key in ("REMOTE_ADDR", "SERVER_NAME", "SERVER_PORT"):
66
if key in environ:
77
yield key, environ[key]

sentry_sdk/integrations/celery.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from __future__ import absolute_import
22

3-
from celery.signals import (
4-
after_setup_logger, task_failure, task_prerun, task_postrun
5-
)
3+
from celery.signals import task_failure, task_prerun, task_postrun
64

75
from sentry_sdk import get_current_hub, configure_scope, capture_exception
8-
from sentry_sdk.utils import ContextVar
96

107

118
def install():
@@ -15,19 +12,15 @@ def install():
1512

1613

1714
def _process_failure_signal(sender, task_id, einfo, **kw):
18-
if hasattr(sender, 'throws') and isinstance(einfo.exception, sender.throws):
15+
if hasattr(sender, "throws") and isinstance(einfo.exception, sender.throws):
1916
return
2017

2118
capture_exception(einfo.exc_info)
2219

2320

24-
_task_scope = ContextVar('sentry_celery_task_scope')
25-
26-
2721
def _handle_task_prerun(sender, task, **kw):
2822
try:
29-
assert _task_scope.get(None) is None, 'race condition'
30-
_task_scope.set(get_current_hub().push_scope().__enter__())
23+
get_current_hub().push_scope()
3124

3225
with configure_scope() as scope:
3326
scope.transaction = task.name
@@ -36,10 +29,4 @@ def _handle_task_prerun(sender, task, **kw):
3629

3730

3831
def _handle_task_postrun(sender, task_id, task, **kw):
39-
try:
40-
val = _task_scope.get(None)
41-
assert val is not None, 'race condition'
42-
val.__exit__(None, None, None)
43-
_task_scope.set(None)
44-
except Exception:
45-
get_current_hub().capture_internal_exception()
32+
get_current_hub().pop_scope_unsafe()

0 commit comments

Comments
 (0)