Skip to content

Add typing to worker module#38

Draft
JuanTecedor wants to merge 2 commits into
TheHive-Project:masterfrom
JuanTecedor:add_typing_to_worker_module
Draft

Add typing to worker module#38
JuanTecedor wants to merge 2 commits into
TheHive-Project:masterfrom
JuanTecedor:add_typing_to_worker_module

Conversation

@JuanTecedor

@JuanTecedor JuanTecedor commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

This PR addresses #37.

I've tried to keep types wide for now.

TODO:

  • Check if all typing used is Python 3.10 compatible.
  • Review design decisions in comments

Comment thread cortexutils/worker.py
name: str | list[str],
default: Any = None,
message: str | None = None,
) -> Any:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without validation I'm not convinced it is worth it to narrow the return type beyond Any, but we could narrow it down to a JSON subset dict | list | str | int | float | bool | None (https://docs.python.org/3/library/json.html#json.JSONEncoder).

Other option is to break compatibility and add a function with a expected_type param that errors if an isinstance check fails, for example.

Comment thread cortexutils/worker.py
else:
job_directory = "/job"
self.job_directory = job_directory
self.job_directory: str | None = job_directory

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May look unnecessary but without this explicit annotation it clashes with L40 self.job_directory = None

Comment thread cortexutils/worker.py


class Worker:
READ_TIMEOUT = 3 # seconds

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unused. Again, chance to break compatibility

Comment thread cortexutils/worker.py
return default

def error(self, message, ensure_ascii=False):
def error(self, message: str, ensure_ascii: bool = False) -> NoReturn:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the issue states, I think here is 80% of the benefit of typing this class

Comment thread cortexutils/worker.py
self.__write_output(output, ensure_ascii=ensure_ascii)

def run(self):
def run(self) -> None:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return -> None might be opinionated here. We may want users to have more freedom if run should ever return something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant