Skip to content

Commit 521a910

Browse files
committed
refactor: clean up some pylint warnings
1 parent 65f106b commit 521a910

14 files changed

Lines changed: 60 additions & 69 deletions

appmap/_implementation/django.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from . import remote_recording
1313

1414

15-
class RemoteRecording:
15+
class RemoteRecording: # pylint: disable=missing-class-docstring
1616
def __init__(self, get_response):
1717
super().__init__()
1818
self.get_response = get_response

appmap/_implementation/event.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
12
import inspect
23
import logging
34
import threading

appmap/_implementation/flask.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414

1515
@app.route("/record", methods=["GET"])
1616
def status():
17-
body, status = remote_recording.status()
18-
return Response(body, status=status, mimetype="application/json")
17+
body, rrstatus = remote_recording.status()
18+
return Response(body, status=rrstatus, mimetype="application/json")
1919

2020

2121
@app.route("/record", methods=["POST"])
2222
def start():
23-
body, status = remote_recording.start()
24-
return Response(body, status=status, mimetype="application/json")
23+
body, rrstatus = remote_recording.start()
24+
return Response(body, status=rrstatus, mimetype="application/json")
2525

2626

2727
@app.route("/record", methods=["DELETE"])
2828
def stop():
29-
body, status = remote_recording.stop()
30-
return Response(body, status=status, mimetype="application/json")
29+
body, rrstatus = remote_recording.stop()
30+
return Response(body, status=rrstatus, mimetype="application/json")

appmap/_implementation/recorder.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import threading
33
import traceback
44
from abc import ABC, abstractmethod
5-
from copy import copy
65

76
from appmap._implementation.utils import appmap_tls
87

98
logger = logging.getLogger(__name__)
109

10+
# pylint: disable=global-statement
1111
_default_recorder = None
1212

1313

@@ -70,19 +70,19 @@ def new_global(cls):
7070

7171
@classmethod
7272
def get_enabled(cls):
73-
return cls.get_current()._enabled
73+
return cls.get_current()._enabled # pylint: disable=protected-access
7474

7575
@classmethod
7676
def set_enabled(cls, e):
77-
cls.get_current()._enabled = e
77+
cls.get_current()._enabled = e # pylint: disable=protected-access
7878

7979
@classmethod
8080
def start_recording(cls):
81-
cls.get_current()._start_recording()
81+
cls.get_current()._start_recording() # pylint: disable=protected-access
8282

8383
@classmethod
8484
def stop_recording(cls):
85-
return cls.get_current()._stop_recording()
85+
return cls.get_current()._stop_recording() # pylint: disable=protected-access
8686

8787
@classmethod
8888
def add_event(cls, event):
@@ -91,15 +91,14 @@ def add_event(cls, event):
9191
one).
9292
"""
9393
perthread, shared = cls._get_current()
94-
shared._add_event(event)
94+
shared._add_event(event) # pylint: disable=protected-access
9595
if perthread:
96-
perthread._add_event(event)
96+
perthread._add_event(event) # pylint: disable=protected-access
9797

9898
_RECORDER_KEY = "appmap_recorder"
9999

100100
@classmethod
101101
def _get_current(cls):
102-
global _default_recorder
103102
perthread = appmap_tls().get(cls._RECORDER_KEY, None)
104103

105104
return [perthread, _default_recorder]
@@ -153,6 +152,8 @@ class ThreadRecorder(Recorder):
153152
def events(self):
154153
return super().events
155154

155+
# They're not useless, because they're abtract with a default implementation
156+
# pragma pylint: disable=useless-super-delegation
156157
def _start_recording(self):
157158
super()._start_recording()
158159

@@ -162,6 +163,8 @@ def _stop_recording(self):
162163
def _add_event(self, event):
163164
super()._add_event(event)
164165

166+
# pragma pylint: enable=useless-super-delegation
167+
165168

166169
class SharedRecorder(Recorder):
167170
"""
@@ -197,4 +200,4 @@ def _add_event(self, event):
197200

198201

199202
def initialize():
200-
Recorder._initialize()
203+
Recorder._initialize() # pylint: disable=protected-access

appmap/_implementation/remote_recording.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .detect_enabled import DetectEnabled
77
from .recorder import Recorder
88

9+
# pylint: disable=global-statement
910
_enabled_lock = Lock()
1011
_enabled = False
1112

appmap/_implementation/web_framework.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from . import generation, remote_recording
1414
from .detect_enabled import DetectEnabled
1515
from .env import Env
16-
from .event import Event, ReturnEvent, _EventIds, describe_value
16+
from .event import Event, ReturnEvent, describe_value
1717
from .recorder import Recorder, ThreadRecorder
1818
from .utils import root_relative_path, scenario_filename
1919

appmap/django.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
from django.db.backends.signals import connection_created
1717
from django.db.backends.utils import CursorDebugWrapper
1818
from django.dispatch import receiver
19-
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
2019
from django.template import Template
2120
from django.urls import get_resolver, resolve
2221
from django.urls.exceptions import Resolver404
2322
from django.urls.resolvers import _route_to_regex
2423

2524
from appmap._implementation.detect_enabled import DetectEnabled
2625

27-
from ._implementation import generation
2826
from ._implementation.event import (
2927
ExceptionEvent,
3028
HttpServerRequestEvent,

appmap/flask.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import pdb
21
import re
3-
import sys
42
import time
53

64
import flask
75
import flask.cli
86
import jinja2
9-
from flask import _app_ctx_stack, current_app, request
7+
from flask import g, request
108
from flask.cli import ScriptInfo
119
from werkzeug.exceptions import BadRequest
1210
from werkzeug.middleware.dispatcher import DispatcherMiddleware
@@ -16,7 +14,6 @@
1614
from appmap._implementation.env import Env
1715
from appmap._implementation.event import HttpServerRequestEvent, HttpServerResponseEvent
1816
from appmap._implementation.flask import app as remote_recording_app
19-
from appmap._implementation.recorder import Recorder
2017
from appmap._implementation.web_framework import AppmapMiddleware
2118
from appmap._implementation.web_framework import TemplateHandler as BaseTemplateHandler
2219

@@ -65,9 +62,6 @@ class AppmapFlask(AppmapMiddleware):
6562
```
6663
"""
6764

68-
def __init__(self):
69-
super().__init__()
70-
7165
def init_app(self, app):
7266
if DetectEnabled.should_enable("remote"):
7367
app.wsgi_app = DispatcherMiddleware(
@@ -87,13 +81,15 @@ def before_request_main(self, rec, request):
8781
Metadata.add_framework("flask", flask.__version__)
8882
np = None
8983
if request.url_rule:
84+
# pragma pylint: disable=line-too-long
9085
# Transform request.url to the expected normalized-path form. For example,
9186
# "/post/<username>/<post_id>/summary" becomes "/post/{username}/{post_id}/summary".
9287
# Notes:
9388
# * the value of `repr` of this rule begins with "<Rule '/post/<username>/<post_id>/summary'"
9489
# * the variable names in a rule can only contain alphanumerics:
9590
# * flask 1: https://github.com/pallets/werkzeug/blob/1dde4b1790f9c46b7122bb8225e6b48a5b22a615/src/werkzeug/routing.py#L143
9691
# * flask 2: https://github.com/pallets/werkzeug/blob/99f328cf2721e913bd8a3128a9cdd95ca97c334c/src/werkzeug/routing/rules.py#L56
92+
# pragma pylint: enable=line-too-long
9793
r = repr(request.url_rule)
9894
np = NP_PARAMS.findall(r)[0].translate(NP_PARAM_DELIMS)
9995

@@ -107,10 +103,10 @@ def before_request_main(self, rec, request):
107103
)
108104
rec.add_event(call_event)
109105

110-
appctx = _app_ctx_stack.top
111-
appctx.appmap_request_event = call_event
112-
appctx.appmap_request_start = time.monotonic()
113-
106+
# Flask 2 removed the suggestion to use _app_ctx_stack.top, and instead says extensions
107+
# should use g with a private property.
108+
g._appmap_request_event = call_event # pylint: disable=protected-access
109+
g._appmap_request_start = time.monotonic() # pylint: disable=protected-access
114110
return None, None
115111

116112
def after_request(self, response):
@@ -128,10 +124,9 @@ def after_request(self, response):
128124
)
129125

130126
def after_request_main(self, rec, response, start, call_event_id):
131-
appctx = _app_ctx_stack.top
132-
parent_id = appctx.appmap_request_event.id
133-
duration = time.monotonic() - appctx.appmap_request_start
134-
127+
parent_id = g._appmap_request_event.id # pylint: disable=protected-access
128+
start = g._appmap_request_start # pylint: disable=protected-access
129+
duration = time.monotonic() - start
135130
return_event = HttpServerResponseEvent(
136131
parent_id=parent_id,
137132
elapsed=duration,

appmap/test/test_django.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# flake8: noqa: E402
2-
# pylint: disable=unused-import, redefined-outer-name, missing-function-docstring
2+
# pylint: disable=missing-function-docstring,redefined-outer-name
33

44
import json
55
import sys
66
from pathlib import Path
7+
from threading import Thread
78

89
import django
910
import django.conf
@@ -19,23 +20,17 @@
1920
import appmap.django # noqa: F401
2021
from appmap.test.helpers import DictIncluding
2122

23+
from .._implementation.metadata import Metadata
24+
from .web_framework import TestRecordRequests, exec_cmd, wait_until_port_is
25+
2226
sys.path += [str(Path(__file__).parent / "data" / "django")]
23-
import app
2427

25-
from .._implementation.metadata import Metadata
28+
# Import app just for the side-effects. It must happen after sys.path has been modified.
29+
import app # pyright: ignore pylint: disable=import-error, unused-import,wrong-import-order
2630

2731
# Make sure assertions in web_framework get rewritten (e.g. to show
2832
# diffs in generated appmaps)
2933
pytest.register_assert_rewrite("appmap.test.web_framework")
30-
from threading import Thread
31-
32-
from .web_framework import (
33-
TestRecording,
34-
TestRecordRequests,
35-
TestRequestCapture,
36-
exec_cmd,
37-
wait_until_port_is,
38-
)
3934

4035

4136
@pytest.mark.django_db
@@ -235,7 +230,11 @@ def server_start(env_vars_str):
235230
@staticmethod
236231
def server_stop():
237232
exec_cmd(
238-
"ps -ef | grep -i 'manage.py runserver' | grep -v grep | awk '{ print $2 }' | xargs kill -9"
233+
"ps -ef"
234+
+ "| grep -i 'manage.py runserver'"
235+
+ "| grep -v grep"
236+
+ "| awk '{ print $2 }'"
237+
+ "| xargs kill -9"
239238
)
240239
wait_until_port_is("127.0.0.1", TestRecordRequests.server_port, "closed")
241240

appmap/test/test_flask.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@
1212
from appmap.test.helpers import DictIncluding
1313

1414
from .._implementation.metadata import Metadata
15-
16-
# Make sure assertions in web_framework get rewritten (e.g. to show
17-
# diffs in generated appmaps)
18-
pytest.register_assert_rewrite("appmap.test.web_framework")
19-
2015
from .web_framework import ( # pylint: disable=unused-import
21-
TestRecording,
2216
TestRecordRequests,
23-
TestRequestCapture,
2417
exec_cmd,
2518
wait_until_port_is,
2619
)
2720

21+
# Make sure assertions in web_framework get rewritten (e.g. to show
22+
# diffs in generated appmaps)
23+
pytest.register_assert_rewrite("appmap.test.web_framework")
24+
2825

2926
@pytest.fixture(name="client")
3027
def flask_client(app):
@@ -38,7 +35,7 @@ def flask_app(data_dir, monkeypatch):
3835

3936
Env.current.set("APPMAP_CONFIG", data_dir / "flask" / "appmap.yml")
4037

41-
import app # pylint: disable=import-error
38+
import app # pyright: ignore pylint: disable=import-error,import-outside-toplevel
4239

4340
importlib.reload(app)
4441

0 commit comments

Comments
 (0)