Skip to content

Commit

Permalink
Don't ignore type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
msm-code committed Oct 2, 2024
1 parent 3dc9e30 commit 4961490
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 43 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/test_code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ jobs:
run: pip3 install mypy==1.2.0
- name: install requirements
run: pip3 install -r requirements.txt
- name: run mypy
run: mypy src
- name: run mypy on main files
run: mypy src/app.py src/daemon.py
- name: run mypy on tests
run: MYPYPATH=src/ mypy src/tests/
- name: run mypy on utils
run: MYPYPATH=src/ mypy src/utils/
test_python_style:
name: python flake8
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ urllib3==2.2.3
uvicorn==0.30.6
wrapt==1.16.0
yara-python==4.5.1
yaramod==3.23.0
yaramod==3.23.0
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exclude = mqueryfront/

[mypy]
python_version = 3.10
ignore_missing_imports = True

[mypy-yaramod.*]
ignore_missing_imports = True
Expand Down
6 changes: 3 additions & 3 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,13 @@ def backend_status() -> BackendStatusSchema:
ursadb_version = status["result"]["ursadb_version"]
agents.append(
AgentSchema(
name=name, alive=True, tasks=tasks, spec=agent_spec # type: ignore
name=name, alive=True, tasks=tasks, spec=agent_spec
)
)
components[f"ursadb ({name})"] = ursadb_version
except Again:
agents.append(
AgentSchema(name=name, alive=False, tasks=[], spec=agent_spec) # type: ignore
AgentSchema(name=name, alive=False, tasks=[], spec=agent_spec)
)
components[f"ursadb ({name})"] = "unknown"

Expand Down Expand Up @@ -534,7 +534,7 @@ def job_statuses(user: User = Depends(current_user)) -> JobsSchema:
if "can_list_all_queries" in get_user_roles(user):
username_filter = None
jobs = db.get_valid_jobs(username_filter)
return JobsSchema(jobs=jobs) # type: ignore
return JobsSchema(jobs=jobs)


@app.delete(
Expand Down
4 changes: 2 additions & 2 deletions src/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def create_search_task(
datasets_left=0,
total_datasets=0,
taints=taints,
) # type: ignore
)
session.add(obj)
session.commit()

Expand All @@ -287,7 +287,7 @@ def get_job_matches(
query = query.limit(limit)

matches = session.exec(query).all()
return MatchesSchema(job=job, matches=matches) # type: ignore
return MatchesSchema(job=job, matches=matches)

def update_job_files(self, job: JobId, total_files: int) -> int:
"""Add total_files to the specified job, and return a new total."""
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ursadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __execute(self, command: str, recv_timeout: int = 2000) -> Json:
socket.send_string(command)
return json.loads(socket.recv_string())
finally:
socket.close() # type: ignore
socket.close()

def query(
self,
Expand Down
27 changes: 13 additions & 14 deletions src/lib/yaraparse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argparse
import itertools
import re
from typing import Any, Dict, List, Match, Optional
from typing import Any, Dict, List, Match, Optional, cast, Callable

from yaramod import ( # type: ignore
AllExpression,
Expand All @@ -17,7 +17,6 @@
OfExpression,
OrExpression,
ParenthesesExpression,
PlainString,
Regexp,
RegexpConcat,
RegexpGroup,
Expand Down Expand Up @@ -358,7 +357,7 @@ def ursify_plain_string(
return ursa_ascii


def ursify_xor_string(string: PlainString) -> UrsaExpression:
def ursify_xor_string(string: String) -> UrsaExpression:
text_ascii = string.pure_text
xored_strings: List[UrsaExpression] = []

Expand All @@ -377,7 +376,7 @@ def ursify_xor_string(string: PlainString) -> UrsaExpression:

def ursify_string(string: String) -> Optional[UrsaExpression]:
if string.is_xor:
return ursify_xor_string(string) # type: ignore
return ursify_xor_string(string)
elif string.is_plain:
return ursify_plain_string(
string.pure_text,
Expand All @@ -389,14 +388,14 @@ def ursify_string(string: String) -> Optional[UrsaExpression]:
value_safe = string.pure_text.decode()
return ursify_hex(value_safe)
elif string.is_regexp:
return ursify_regex_string(string) # type: ignore
return ursify_regex_string(cast(Regexp, string))

return None


class RuleParseEngine:
def __init__(
self, strings: Dict[str, str], rules: Dict[str, YaraRuleData]
self, strings: Dict[str, String], rules: Dict[str, YaraRuleData]
) -> None:
self.strings = strings
self.rules = rules
Expand Down Expand Up @@ -429,7 +428,7 @@ def pare_expr(
def str_expr(
self, condition: StringExpression
) -> Optional[UrsaExpression]:
return ursify_string(self.strings[condition.id]) # type: ignore
return ursify_string(self.strings[condition.id])

def expand_string_wildcard(
self, condition: StringWildcardExpression
Expand All @@ -440,7 +439,7 @@ def expand_string_wildcard(
v for k, v in self.strings.items() if re.match(condition_regex, k)
]

ursa_strings = [ursify_string(x) for x in filtered_strings] # type: ignore
ursa_strings = [ursify_string(x) for x in filtered_strings]
return [s for s in ursa_strings if s is not None]

def expand_set_expression(
Expand All @@ -463,7 +462,7 @@ def of_expr(self, condition: OfExpression) -> Optional[UrsaExpression]:
if type(children) is SetExpression:
all_elements = self.expand_set_expression(children)
elif type(children) is ThemExpression:
all_elements = [ursify_string(k) for k in self.strings.values()] # type: ignore
all_elements = [ursify_string(k) for k in self.strings.values()]
else:
raise YaraParseError(f"Unsupported of_expr type: {type(children)}")

Expand Down Expand Up @@ -524,7 +523,7 @@ def str_count_expr(
self, condition: StringCountExpression
) -> Optional[UrsaExpression]:
fixed_id = "$" + condition.id[1:]
return ursify_string(self.strings[fixed_id]) # type: ignore
return ursify_string(self.strings[fixed_id])

def int_lit_expr(
self, condition: IntLiteralExpression
Expand All @@ -535,17 +534,17 @@ def int_lit_expr(
def str_at_expr(
self, condition: StringAtExpression
) -> Optional[UrsaExpression]:
return ursify_string(self.strings[condition.id]) # type: ignore
return ursify_string(self.strings[condition.id])

def id_expr(self, condition: IdExpression) -> Optional[UrsaExpression]:
return self.rules[condition.symbol.name].parse()

def str_in_expr(
self, condition: StringInRangeExpression
) -> Optional[UrsaExpression]:
return ursify_string(self.strings[condition.id]) # type: ignore
return ursify_string(self.strings[condition.id])

CONDITION_HANDLERS = {
CONDITION_HANDLERS: Dict[type, Callable] = {
AndExpression: and_expr,
OrExpression: or_expr,
ParenthesesExpression: pare_expr,
Expand All @@ -565,7 +564,7 @@ def str_in_expr(

def traverse(self, condition) -> Optional[UrsaExpression]:
if type(condition) in self.CONDITION_HANDLERS:
return self.CONDITION_HANDLERS[type(condition)](self, condition) # type: ignore
return self.CONDITION_HANDLERS[type(condition)](self, condition)
else:
print(f"unsupported expression: {type(condition)}")
return None
Expand Down
10 changes: 2 additions & 8 deletions src/models/agentgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
from ..models.jobagent import JobAgent


class AgentGroupBase(SQLModel):
class AgentGroupView(SQLModel):
name: str
ursadb_url: str
plugins_spec: Dict[str, Dict[str, str]] = Field(sa_column=Column(JSON))
active_plugins: List[str] = Field(sa_column=Column(ARRAY(String)))


class AgentGroup(AgentGroupBase, table=True):
class AgentGroup(AgentGroupView, table=True):
"""Agent group is a group of processes working on a single
file group, with a shared storage, and a single backing ursadb.
"""

id: Union[int, None] = Field(default=None, primary_key=True)
jobs: List["JobAgent"] = Relationship(back_populates="agent")


class AgentGroupView(AgentGroupBase):
"""Pydantic model used in the public API."""

pass
12 changes: 3 additions & 9 deletions src/models/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from ..models.jobagent import JobAgent


class JobBase(SQLModel):
"""Base class for entities related to mquery jobs."""
class JobView(SQLModel):
"""Public fields of mquery jobs."""

id: str
status: str
Expand All @@ -30,16 +30,10 @@ class JobBase(SQLModel):
agents_left: int


class Job(JobBase, table=True):
class Job(JobView, table=True):
"""Job object in the database. Internal ID is an implementation detail."""

internal_id: Union[int, None] = Field(default=None, primary_key=True)

matches: List["Match"] = Relationship(back_populates="job")
agents: List["JobAgent"] = Relationship(back_populates="job")


class JobView(JobBase):
"""Pydantic model used in the public API."""

pass
4 changes: 2 additions & 2 deletions src/schema.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from enum import Enum
from typing import List, Dict, Optional
from typing import List, Dict, Optional, Sequence
from pydantic import BaseModel, Field # type: ignore
from .models.job import JobView
from .models.agentgroup import AgentGroupView


class JobsSchema(BaseModel):
jobs: List[JobView]
jobs: Sequence[JobView]


class ConfigSchema(BaseModel):
Expand Down

0 comments on commit 4961490

Please sign in to comment.