Skip to content

Commit

Permalink
Run mypy static analysis against agent
Browse files Browse the repository at this point in the history
- A few changes to the agent to make mypy happy
- Added to github actions
  • Loading branch information
rkoumis committed Jan 17, 2025
1 parent 9898245 commit 21b1434
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ jobs:
- name: Run unit tests
run: poetry run python -m pytest --import-mode=append

# see the mypy configuration in pyproject.toml
- name: Run mypy
run: poetry run mypy

format:
runs-on: ubuntu-latest
timeout-minutes: 20
Expand Down
21 changes: 11 additions & 10 deletions agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import time
import traceback
from io import StringIO
from multiprocessing.synchronize import Event as EventClass
from threading import Lock
from typing import Iterable
from typing import Iterable, Optional
from zipfile import ZipFile

try:
import re2 as re
import re2 as re # type: ignore
except ImportError:
import re

Expand Down Expand Up @@ -96,7 +97,7 @@ def _missing_(cls, value):
AGENT_BROWSER_EXT_PATH = ""
AGENT_BROWSER_LOCK = Lock()
ANALYZER_FOLDER = ""
agent_mutexes = {}
agent_mutexes: dict[str, str] = {}
"""Holds handles of mutexes held by the agent."""
state = {
"status": Status.INIT,
Expand Down Expand Up @@ -177,12 +178,12 @@ def __init__(self):

def run(
self,
host: ipaddress.IPv4Address = "0.0.0.0",
host: ipaddress.IPv4Address = ipaddress.IPv4Address("0.0.0.0"),
port: int = 8000,
event: multiprocessing.Event = None,
event: Optional[EventClass] = None,
):
socketserver.ThreadingTCPServer.allow_reuse_address = True
self.s = socketserver.ThreadingTCPServer((host, port), self.handler)
self.s = socketserver.ThreadingTCPServer((str(host), port), self.handler)

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

def json(self):
def json(self) -> str:
for valkey in self.values:
if isinstance(self.values[valkey], bytes):
self.values[valkey] = self.values[valkey].decode("utf8", "replace")
Expand Down Expand Up @@ -324,8 +325,8 @@ def headers(self, obj):


class request:
form = {}
files = {}
form: dict[str, str] = {}
files: dict[str, str] = {}
client_ip = None
client_port = None
method = None
Expand All @@ -334,7 +335,7 @@ class request:
}


app = MiniHTTPServer()
app: MiniHTTPServer = MiniHTTPServer()


def isAdmin():
Expand Down
3 changes: 2 additions & 1 deletion agent/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import unittest
import uuid
import zipfile
from typing import Optional
from unittest import mock
from urllib.parse import urljoin

Expand Down Expand Up @@ -180,7 +181,7 @@ def test_delete_mutex_win32_200(self):
class TestAgent:
"""Test the agent API."""

agent_process: multiprocessing.Process = None
agent_process: Optional[multiprocessing.Process] = None

def setup_method(self):
agent.state = {"status": agent.Status.INIT, "description": "", "async_subprocess": None}
Expand Down
75 changes: 74 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ maco = ["maco"]
[tool.poetry.dev-dependencies]
black = "^24.3.0"
isort = "^5.10.1"
mypy = "1.14.1"
pytest = "7.2.2"
pytest-pretty = "1.1.0"
pytest-cov = "3.0.0"
Expand All @@ -108,6 +109,7 @@ pytest-xdist = "3.6.1"
pytest-asyncio = "0.18.3"
pytest-freezer = "0.4.8"
tenacity = "8.1.0"
types-requests = "^2.32"
httpretty = "^1.1.4"
func-timeout = "^4.3.5"
pre-commit = "^2.19.0"
Expand Down Expand Up @@ -147,3 +149,9 @@ build-backend = "poetry.core.masonry.api"
[lint]
select = ["E", "F"]
ignore = ["E402","E501"]

[tool.mypy]
warn_unused_configs = true
files = [
"agent/**/*.py",
]

0 comments on commit 21b1434

Please sign in to comment.