Skip to content

Commit 03a5e65

Browse files
authored
ref: Introduce linter for proper naming conventions (getsentry#636)
* ref: Introduce linter for proper naming conventions * ref: Document reasons for ignoring lints
1 parent 909ecaa commit 03a5e65

File tree

16 files changed

+156
-133
lines changed

16 files changed

+156
-133
lines changed

.flake8

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
[flake8]
22
ignore =
3-
E203, E266, E501, W503, E402, E731, C901, B950, B011,
4-
B014 // does not apply to Python 2
3+
E203, // Handled by black (Whitespace before ':' -- handled by black)
4+
E266, // Handled by black (Too many leading '#' for block comment)
5+
E501, // Handled by black (Line too long)
6+
W503, // Handled by black (Line break occured before a binary operator)
7+
E402, // Sometimes not possible due to execution order (Module level import is not at top of file)
8+
E731, // I don't care (Do not assign a lambda expression, use a def)
9+
C901, // I don't care (Function is too complex)
10+
B950, // Handled by black (Line too long by flake8-bugbear)
11+
B011, // I don't care (Do not call assert False)
12+
B014, // does not apply to Python 2 (redundant exception types by flake8-bugbear)
13+
N812, // I don't care (Lowercase imported as non-lowercase by pep8-naming)
14+
N804 // is a worse version of and conflicts with B902 (first argument of a classmethod should be named cls)
515
max-line-length = 80
616
max-complexity = 18
7-
select = B,C,E,F,W,T4,B9
17+
select = N,B,C,E,F,W,T4,B9
818
exclude=checkouts,lol*,.tox

linter-requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ flake8
33
flake8-import-order
44
mypy==0.761
55
flake8-bugbear>=19.8.0
6+
pep8-naming

sentry_sdk/_compat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ def reraise(tp, value, tb=None):
5959

6060
def with_metaclass(meta, *bases):
6161
# type: (Any, *Any) -> Any
62-
class metaclass(type):
62+
class MetaClass(type):
6363
def __new__(metacls, name, this_bases, d):
6464
# type: (Any, Any, Any, Any) -> Any
6565
return meta(name, bases, d)
6666

67-
return type.__new__(metaclass, "temporary_class", (), {})
67+
return type.__new__(MetaClass, "temporary_class", (), {})
6868

6969

7070
def check_thread_support():

sentry_sdk/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def __exit__(self, exc_type, exc_value, tb):
389389
# Use `ClientConstructor` to define the argument types of `init` and
390390
# `Dict[str, Any]` to tell static analyzers about the return type.
391391

392-
class get_options(ClientConstructor, Dict[str, Any]):
392+
class get_options(ClientConstructor, Dict[str, Any]): # noqa: N801
393393
pass
394394

395395
class Client(ClientConstructor, _Client):

sentry_sdk/hub.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _init(*args, **kwargs):
118118
# Use `ClientConstructor` to define the argument types of `init` and
119119
# `ContextManager[Any]` to tell static analyzers about the return type.
120120

121-
class init(ClientConstructor, ContextManager[Any]):
121+
class init(ClientConstructor, ContextManager[Any]): # noqa: N801
122122
pass
123123

124124

sentry_sdk/integrations/bottle.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
raise DidNotEnable("Bottle not installed")
3535

3636

37+
TRANSACTION_STYLE_VALUES = ("endpoint", "url")
38+
39+
3740
class BottleIntegration(Integration):
3841
identifier = "bottle"
3942

@@ -42,7 +45,6 @@ class BottleIntegration(Integration):
4245
def __init__(self, transaction_style="endpoint"):
4346
# type: (str) -> None
4447

45-
TRANSACTION_STYLE_VALUES = ("endpoint", "url")
4648
if transaction_style not in TRANSACTION_STYLE_VALUES:
4749
raise ValueError(
4850
"Invalid value for transaction_style: %s (must be in %s)"

sentry_sdk/integrations/django/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def is_authenticated(request_user):
7171
return request_user.is_authenticated
7272

7373

74+
TRANSACTION_STYLE_VALUES = ("function_name", "url")
75+
76+
7477
class DjangoIntegration(Integration):
7578
identifier = "django"
7679

@@ -79,7 +82,6 @@ class DjangoIntegration(Integration):
7982

8083
def __init__(self, transaction_style="url", middleware_spans=True):
8184
# type: (str, bool) -> None
82-
TRANSACTION_STYLE_VALUES = ("function_name", "url")
8385
if transaction_style not in TRANSACTION_STYLE_VALUES:
8486
raise ValueError(
8587
"Invalid value for transaction_style: %s (must be in %s)"

sentry_sdk/integrations/falcon.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ def process_request(self, req, resp, *args, **kwargs):
8181
scope.add_event_processor(_make_request_event_processor(req, integration))
8282

8383

84+
TRANSACTION_STYLE_VALUES = ("uri_template", "path")
85+
86+
8487
class FalconIntegration(Integration):
8588
identifier = "falcon"
8689

8790
transaction_style = None
8891

8992
def __init__(self, transaction_style="uri_template"):
9093
# type: (str) -> None
91-
TRANSACTION_STYLE_VALUES = ("uri_template", "path")
9294
if transaction_style not in TRANSACTION_STYLE_VALUES:
9395
raise ValueError(
9496
"Invalid value for transaction_style: %s (must be in %s)"

sentry_sdk/integrations/flask.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@
4646
raise DidNotEnable("Flask is not installed")
4747

4848

49+
TRANSACTION_STYLE_VALUES = ("endpoint", "url")
50+
51+
4952
class FlaskIntegration(Integration):
5053
identifier = "flask"
5154

5255
transaction_style = None
5356

5457
def __init__(self, transaction_style="endpoint"):
5558
# type: (str) -> None
56-
TRANSACTION_STYLE_VALUES = ("endpoint", "url")
5759
if transaction_style not in TRANSACTION_STYLE_VALUES:
5860
raise ValueError(
5961
"Invalid value for transaction_style: %s (must be in %s)"

sentry_sdk/integrations/pyramid.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ def authenticated_userid(request):
4343
from pyramid.security import authenticated_userid # type: ignore
4444

4545

46+
TRANSACTION_STYLE_VALUES = ("route_name", "route_pattern")
47+
48+
4649
class PyramidIntegration(Integration):
4750
identifier = "pyramid"
4851

4952
transaction_style = None
5053

5154
def __init__(self, transaction_style="route_name"):
5255
# type: (str) -> None
53-
TRANSACTION_STYLE_VALUES = ("route_name", "route_pattern")
5456
if transaction_style not in TRANSACTION_STYLE_VALUES:
5557
raise ValueError(
5658
"Invalid value for transaction_style: %s (must be in %s)"

sentry_sdk/integrations/spark/spark_driver.py

+43-41
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def _set_app_properties():
2929
"""
3030
from pyspark import SparkContext
3131

32-
sparkContext = SparkContext._active_spark_context
33-
if sparkContext:
34-
sparkContext.setLocalProperty("sentry_app_name", sparkContext.appName)
35-
sparkContext.setLocalProperty(
36-
"sentry_application_id", sparkContext.applicationId
32+
spark_context = SparkContext._active_spark_context
33+
if spark_context:
34+
spark_context.setLocalProperty("sentry_app_name", spark_context.appName)
35+
spark_context.setLocalProperty(
36+
"sentry_application_id", spark_context.applicationId
3737
)
3838

3939

@@ -106,99 +106,101 @@ def process_event(event, hint):
106106

107107

108108
class SparkListener(object):
109-
def onApplicationEnd(self, applicationEnd):
109+
def onApplicationEnd(self, applicationEnd): # noqa: N802,N803
110110
# type: (Any) -> None
111111
pass
112112

113-
def onApplicationStart(self, applicationStart):
113+
def onApplicationStart(self, applicationStart): # noqa: N802,N803
114114
# type: (Any) -> None
115115
pass
116116

117-
def onBlockManagerAdded(self, blockManagerAdded):
117+
def onBlockManagerAdded(self, blockManagerAdded): # noqa: N802,N803
118118
# type: (Any) -> None
119119
pass
120120

121-
def onBlockManagerRemoved(self, blockManagerRemoved):
121+
def onBlockManagerRemoved(self, blockManagerRemoved): # noqa: N802,N803
122122
# type: (Any) -> None
123123
pass
124124

125-
def onBlockUpdated(self, blockUpdated):
125+
def onBlockUpdated(self, blockUpdated): # noqa: N802,N803
126126
# type: (Any) -> None
127127
pass
128128

129-
def onEnvironmentUpdate(self, environmentUpdate):
129+
def onEnvironmentUpdate(self, environmentUpdate): # noqa: N802,N803
130130
# type: (Any) -> None
131131
pass
132132

133-
def onExecutorAdded(self, executorAdded):
133+
def onExecutorAdded(self, executorAdded): # noqa: N802,N803
134134
# type: (Any) -> None
135135
pass
136136

137-
def onExecutorBlacklisted(self, executorBlacklisted):
137+
def onExecutorBlacklisted(self, executorBlacklisted): # noqa: N802,N803
138138
# type: (Any) -> None
139139
pass
140140

141-
def onExecutorBlacklistedForStage(self, executorBlacklistedForStage):
141+
def onExecutorBlacklistedForStage( # noqa: N802
142+
self, executorBlacklistedForStage # noqa: N803
143+
):
142144
# type: (Any) -> None
143145
pass
144146

145-
def onExecutorMetricsUpdate(self, executorMetricsUpdate):
147+
def onExecutorMetricsUpdate(self, executorMetricsUpdate): # noqa: N802,N803
146148
# type: (Any) -> None
147149
pass
148150

149-
def onExecutorRemoved(self, executorRemoved):
151+
def onExecutorRemoved(self, executorRemoved): # noqa: N802,N803
150152
# type: (Any) -> None
151153
pass
152154

153-
def onJobEnd(self, jobEnd):
155+
def onJobEnd(self, jobEnd): # noqa: N802,N803
154156
# type: (Any) -> None
155157
pass
156158

157-
def onJobStart(self, jobStart):
159+
def onJobStart(self, jobStart): # noqa: N802,N803
158160
# type: (Any) -> None
159161
pass
160162

161-
def onNodeBlacklisted(self, nodeBlacklisted):
163+
def onNodeBlacklisted(self, nodeBlacklisted): # noqa: N802,N803
162164
# type: (Any) -> None
163165
pass
164166

165-
def onNodeBlacklistedForStage(self, nodeBlacklistedForStage):
167+
def onNodeBlacklistedForStage(self, nodeBlacklistedForStage): # noqa: N802,N803
166168
# type: (Any) -> None
167169
pass
168170

169-
def onNodeUnblacklisted(self, nodeUnblacklisted):
171+
def onNodeUnblacklisted(self, nodeUnblacklisted): # noqa: N802,N803
170172
# type: (Any) -> None
171173
pass
172174

173-
def onOtherEvent(self, event):
175+
def onOtherEvent(self, event): # noqa: N802,N803
174176
# type: (Any) -> None
175177
pass
176178

177-
def onSpeculativeTaskSubmitted(self, speculativeTask):
179+
def onSpeculativeTaskSubmitted(self, speculativeTask): # noqa: N802,N803
178180
# type: (Any) -> None
179181
pass
180182

181-
def onStageCompleted(self, stageCompleted):
183+
def onStageCompleted(self, stageCompleted): # noqa: N802,N803
182184
# type: (Any) -> None
183185
pass
184186

185-
def onStageSubmitted(self, stageSubmitted):
187+
def onStageSubmitted(self, stageSubmitted): # noqa: N802,N803
186188
# type: (Any) -> None
187189
pass
188190

189-
def onTaskEnd(self, taskEnd):
191+
def onTaskEnd(self, taskEnd): # noqa: N802,N803
190192
# type: (Any) -> None
191193
pass
192194

193-
def onTaskGettingResult(self, taskGettingResult):
195+
def onTaskGettingResult(self, taskGettingResult): # noqa: N802,N803
194196
# type: (Any) -> None
195197
pass
196198

197-
def onTaskStart(self, taskStart):
199+
def onTaskStart(self, taskStart): # noqa: N802,N803
198200
# type: (Any) -> None
199201
pass
200202

201-
def onUnpersistRDD(self, unpersistRDD):
203+
def onUnpersistRDD(self, unpersistRDD): # noqa: N802,N803
202204
# type: (Any) -> None
203205
pass
204206

@@ -211,13 +213,13 @@ def __init__(self):
211213
# type: () -> None
212214
self.hub = Hub.current
213215

214-
def onJobStart(self, jobStart):
216+
def onJobStart(self, jobStart): # noqa: N802,N803
215217
# type: (Any) -> None
216218
message = "Job {} Started".format(jobStart.jobId())
217219
self.hub.add_breadcrumb(level="info", message=message)
218220
_set_app_properties()
219221

220-
def onJobEnd(self, jobEnd):
222+
def onJobEnd(self, jobEnd): # noqa: N802,N803
221223
# type: (Any) -> None
222224
level = ""
223225
message = ""
@@ -232,30 +234,30 @@ def onJobEnd(self, jobEnd):
232234

233235
self.hub.add_breadcrumb(level=level, message=message, data=data)
234236

235-
def onStageSubmitted(self, stageSubmitted):
237+
def onStageSubmitted(self, stageSubmitted): # noqa: N802,N803
236238
# type: (Any) -> None
237-
stageInfo = stageSubmitted.stageInfo()
238-
message = "Stage {} Submitted".format(stageInfo.stageId())
239-
data = {"attemptId": stageInfo.attemptId(), "name": stageInfo.name()}
239+
stage_info = stageSubmitted.stageInfo()
240+
message = "Stage {} Submitted".format(stage_info.stageId())
241+
data = {"attemptId": stage_info.attemptId(), "name": stage_info.name()}
240242
self.hub.add_breadcrumb(level="info", message=message, data=data)
241243
_set_app_properties()
242244

243-
def onStageCompleted(self, stageCompleted):
245+
def onStageCompleted(self, stageCompleted): # noqa: N802,N803
244246
# type: (Any) -> None
245247
from py4j.protocol import Py4JJavaError # type: ignore
246248

247-
stageInfo = stageCompleted.stageInfo()
249+
stage_info = stageCompleted.stageInfo()
248250
message = ""
249251
level = ""
250-
data = {"attemptId": stageInfo.attemptId(), "name": stageInfo.name()}
252+
data = {"attemptId": stage_info.attemptId(), "name": stage_info.name()}
251253

252254
# Have to Try Except because stageInfo.failureReason() is typed with Scala Option
253255
try:
254-
data["reason"] = stageInfo.failureReason().get()
255-
message = "Stage {} Failed".format(stageInfo.stageId())
256+
data["reason"] = stage_info.failureReason().get()
257+
message = "Stage {} Failed".format(stage_info.stageId())
256258
level = "warning"
257259
except Py4JJavaError:
258-
message = "Stage {} Completed".format(stageInfo.stageId())
260+
message = "Stage {} Completed".format(stage_info.stageId())
259261
level = "info"
260262

261263
self.hub.add_breadcrumb(level=level, message=message, data=data)

sentry_sdk/integrations/spark/spark_worker.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,31 @@ def process_event(event, hint):
7676
# type: (Event, Hint) -> Optional[Event]
7777
with capture_internal_exceptions():
7878
integration = Hub.current.get_integration(SparkWorkerIntegration)
79-
taskContext = TaskContext.get()
79+
task_context = TaskContext.get()
8080

81-
if integration is None or taskContext is None:
81+
if integration is None or task_context is None:
8282
return event
8383

8484
event.setdefault("tags", {}).setdefault(
85-
"stageId", taskContext.stageId()
85+
"stageId", task_context.stageId()
8686
)
87-
event["tags"].setdefault("partitionId", taskContext.partitionId())
88-
event["tags"].setdefault("attemptNumber", taskContext.attemptNumber())
89-
event["tags"].setdefault("taskAttemptId", taskContext.taskAttemptId())
87+
event["tags"].setdefault("partitionId", task_context.partitionId())
88+
event["tags"].setdefault("attemptNumber", task_context.attemptNumber())
89+
event["tags"].setdefault("taskAttemptId", task_context.taskAttemptId())
9090

91-
if taskContext._localProperties:
92-
if "sentry_app_name" in taskContext._localProperties:
91+
if task_context._localProperties:
92+
if "sentry_app_name" in task_context._localProperties:
9393
event["tags"].setdefault(
94-
"app_name", taskContext._localProperties["sentry_app_name"]
94+
"app_name", task_context._localProperties["sentry_app_name"]
9595
)
9696
event["tags"].setdefault(
9797
"application_id",
98-
taskContext._localProperties["sentry_application_id"],
98+
task_context._localProperties["sentry_application_id"],
9999
)
100100

101-
if "callSite.short" in taskContext._localProperties:
101+
if "callSite.short" in task_context._localProperties:
102102
event.setdefault("extra", {}).setdefault(
103-
"callSite", taskContext._localProperties["callSite.short"]
103+
"callSite", task_context._localProperties["callSite.short"]
104104
)
105105

106106
return event

0 commit comments

Comments
 (0)