Skip to content

Commit 46ae70f

Browse files
authored
Merge pull request #2464 from rkoumis/mypy-agent
Run mypy static analysis against agent
2 parents 9898245 + 21b1434 commit 46ae70f

File tree

5 files changed

+99
-12
lines changed

5 files changed

+99
-12
lines changed

.github/workflows/python-package.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ jobs:
4242
- name: Run unit tests
4343
run: poetry run python -m pytest --import-mode=append
4444

45+
# see the mypy configuration in pyproject.toml
46+
- name: Run mypy
47+
run: poetry run mypy
48+
4549
format:
4650
runs-on: ubuntu-latest
4751
timeout-minutes: 20

agent/agent.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
import time
2626
import traceback
2727
from io import StringIO
28+
from multiprocessing.synchronize import Event as EventClass
2829
from threading import Lock
29-
from typing import Iterable
30+
from typing import Iterable, Optional
3031
from zipfile import ZipFile
3132

3233
try:
33-
import re2 as re
34+
import re2 as re # type: ignore
3435
except ImportError:
3536
import re
3637

@@ -96,7 +97,7 @@ def _missing_(cls, value):
9697
AGENT_BROWSER_EXT_PATH = ""
9798
AGENT_BROWSER_LOCK = Lock()
9899
ANALYZER_FOLDER = ""
99-
agent_mutexes = {}
100+
agent_mutexes: dict[str, str] = {}
100101
"""Holds handles of mutexes held by the agent."""
101102
state = {
102103
"status": Status.INIT,
@@ -177,12 +178,12 @@ def __init__(self):
177178

178179
def run(
179180
self,
180-
host: ipaddress.IPv4Address = "0.0.0.0",
181+
host: ipaddress.IPv4Address = ipaddress.IPv4Address("0.0.0.0"),
181182
port: int = 8000,
182-
event: multiprocessing.Event = None,
183+
event: Optional[EventClass] = None,
183184
):
184185
socketserver.ThreadingTCPServer.allow_reuse_address = True
185-
self.s = socketserver.ThreadingTCPServer((host, port), self.handler)
186+
self.s = socketserver.ThreadingTCPServer((str(host), port), self.handler)
186187

187188
# tell anyone waiting that they're good to go
188189
if event:
@@ -248,7 +249,7 @@ def __init__(self, status_code=200, **kwargs):
248249
def init(self):
249250
pass
250251

251-
def json(self):
252+
def json(self) -> str:
252253
for valkey in self.values:
253254
if isinstance(self.values[valkey], bytes):
254255
self.values[valkey] = self.values[valkey].decode("utf8", "replace")
@@ -324,8 +325,8 @@ def headers(self, obj):
324325

325326

326327
class request:
327-
form = {}
328-
files = {}
328+
form: dict[str, str] = {}
329+
files: dict[str, str] = {}
329330
client_ip = None
330331
client_port = None
331332
method = None
@@ -334,7 +335,7 @@ class request:
334335
}
335336

336337

337-
app = MiniHTTPServer()
338+
app: MiniHTTPServer = MiniHTTPServer()
338339

339340

340341
def isAdmin():

agent/test_agent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616
import uuid
1717
import zipfile
18+
from typing import Optional
1819
from unittest import mock
1920
from urllib.parse import urljoin
2021

@@ -180,7 +181,7 @@ def test_delete_mutex_win32_200(self):
180181
class TestAgent:
181182
"""Test the agent API."""
182183

183-
agent_process: multiprocessing.Process = None
184+
agent_process: Optional[multiprocessing.Process] = None
184185

185186
def setup_method(self):
186187
agent.state = {"status": agent.Status.INIT, "description": "", "async_subprocess": None}

poetry.lock

Lines changed: 74 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ maco = ["maco"]
9898
[tool.poetry.dev-dependencies]
9999
black = "^24.3.0"
100100
isort = "^5.10.1"
101+
mypy = "1.14.1"
101102
pytest = "7.2.2"
102103
pytest-pretty = "1.1.0"
103104
pytest-cov = "3.0.0"
@@ -108,6 +109,7 @@ pytest-xdist = "3.6.1"
108109
pytest-asyncio = "0.18.3"
109110
pytest-freezer = "0.4.8"
110111
tenacity = "8.1.0"
112+
types-requests = "^2.32"
111113
httpretty = "^1.1.4"
112114
func-timeout = "^4.3.5"
113115
pre-commit = "^2.19.0"
@@ -147,3 +149,9 @@ build-backend = "poetry.core.masonry.api"
147149
[lint]
148150
select = ["E", "F"]
149151
ignore = ["E402","E501"]
152+
153+
[tool.mypy]
154+
warn_unused_configs = true
155+
files = [
156+
"agent/**/*.py",
157+
]

0 commit comments

Comments
 (0)