-
Notifications
You must be signed in to change notification settings - Fork 4
Add type checking with mypy #535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||
name: MyPy | ||||||
|
||||||
on: | ||||||
push: | ||||||
branches: [ main ] | ||||||
pull_request: | ||||||
|
||||||
jobs: | ||||||
mypy: | ||||||
runs-on: ubuntu-latest | ||||||
steps: | ||||||
- name: Setup Python | ||||||
uses: actions/setup-python@v5 | ||||||
with: | ||||||
python-version: "3.13" | ||||||
architecture: x64 | ||||||
- name: Checkout | ||||||
uses: actions/checkout@v4 | ||||||
- name: Install mypy | ||||||
run: pip install mypy | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Pin MyPy version for consistent CI results Installing MyPy without version pinning could lead to inconsistent results as new versions are released. - run: pip install mypy
+ run: pip install 'mypy==1.8.0' 📝 Committable suggestion
Suggested change
|
||||||
- name: Test | ||||||
run: mypy --ignore-missing-imports ${{ github.event.repository.name }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import pickle | ||
import sys | ||
import time | ||
from typing import Any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused import The Apply this diff to remove the unused import: -from typing import Any 🧰 Tools🪛 Ruff (0.8.2)4-4: Remove unused import: (F401) |
||
|
||
import cloudpickle | ||
|
||
|
@@ -24,7 +25,7 @@ def main() -> None: | |
""" | ||
from mpi4py import MPI | ||
|
||
MPI.pickle.__init__( | ||
MPI.pickle.__init__( # type: ignore | ||
cloudpickle.dumps, | ||
cloudpickle.loads, | ||
pickle.HIGHEST_PROTOCOL, | ||
|
@@ -34,10 +35,9 @@ def main() -> None: | |
file_name = sys.argv[1] | ||
|
||
time_start = time.time() | ||
apply_dict = {} | ||
if mpi_rank_zero: | ||
apply_dict = backend_load_file(file_name=file_name) | ||
else: | ||
apply_dict = None | ||
apply_dict = MPI.COMM_WORLD.bcast(apply_dict, root=0) | ||
output = apply_dict["fn"].__call__(*apply_dict["args"], **apply_dict["kwargs"]) | ||
if mpi_size_larger_one: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
from concurrent.futures import ( | ||
Future, | ||
) | ||
from typing import Optional | ||
from typing import Callable, List, Optional, Union | ||
|
||
from executorlib.standalone.inputcheck import check_resource_dict | ||
from executorlib.standalone.queue import cancel_items_in_queue | ||
|
@@ -27,8 +27,8 @@ def __init__(self, max_cores: Optional[int] = None): | |
""" | ||
cloudpickle_register(ind=3) | ||
self._max_cores = max_cores | ||
self._future_queue: queue.Queue = queue.Queue() | ||
self._process: Optional[RaisingThread] = None | ||
self._future_queue: Optional[queue.Queue] = queue.Queue() | ||
self._process: Optional[Union[RaisingThread, List[RaisingThread]]] = None | ||
|
||
@property | ||
def info(self) -> Optional[dict]: | ||
|
@@ -39,21 +39,21 @@ def info(self) -> Optional[dict]: | |
Optional[dict]: Information about the executor. | ||
""" | ||
if self._process is not None and isinstance(self._process, list): | ||
meta_data_dict = self._process[0]._kwargs.copy() | ||
meta_data_dict = self._process[0].get_kwargs().copy() | ||
if "future_queue" in meta_data_dict.keys(): | ||
del meta_data_dict["future_queue"] | ||
meta_data_dict["max_workers"] = len(self._process) | ||
return meta_data_dict | ||
elif self._process is not None: | ||
meta_data_dict = self._process._kwargs.copy() | ||
meta_data_dict = self._process.get_kwargs().copy() | ||
if "future_queue" in meta_data_dict.keys(): | ||
del meta_data_dict["future_queue"] | ||
return meta_data_dict | ||
else: | ||
return None | ||
|
||
@property | ||
def future_queue(self) -> queue.Queue: | ||
def future_queue(self) -> Optional[queue.Queue]: | ||
""" | ||
Get the future queue. | ||
|
||
|
@@ -62,7 +62,7 @@ def future_queue(self) -> queue.Queue: | |
""" | ||
return self._future_queue | ||
|
||
def submit(self, fn: callable, *args, resource_dict: dict = {}, **kwargs) -> Future: | ||
def submit(self, fn: Callable, *args, resource_dict: dict = {}, **kwargs) -> Future: # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid mutable defaults for -def submit(self, fn: Callable, *args, resource_dict: dict = {}, **kwargs) -> Future:
+def submit(self, fn: Callable, *args, resource_dict: Optional[dict] = None, **kwargs) -> Future:
...
+ if resource_dict is None:
+ resource_dict = {}
🧰 Tools🪛 Ruff (0.8.2)65-65: Do not use mutable data structures for argument defaults Replace with (B006) |
||
""" | ||
Submits a callable to be executed with the given arguments. | ||
|
||
|
@@ -97,16 +97,17 @@ def submit(self, fn: callable, *args, resource_dict: dict = {}, **kwargs) -> Fut | |
"The specified number of cores is larger than the available number of cores." | ||
) | ||
check_resource_dict(function=fn) | ||
f = Future() | ||
self._future_queue.put( | ||
{ | ||
"fn": fn, | ||
"args": args, | ||
"kwargs": kwargs, | ||
"future": f, | ||
"resource_dict": resource_dict, | ||
} | ||
) | ||
f: Future = Future() | ||
if self._future_queue is not None: | ||
self._future_queue.put( | ||
{ | ||
"fn": fn, | ||
"args": args, | ||
"kwargs": kwargs, | ||
"future": f, | ||
"resource_dict": resource_dict, | ||
} | ||
) | ||
return f | ||
|
||
def shutdown(self, wait: bool = True, *, cancel_futures: bool = False): | ||
|
@@ -124,11 +125,11 @@ def shutdown(self, wait: bool = True, *, cancel_futures: bool = False): | |
futures. Futures that are completed or running will not be | ||
cancelled. | ||
""" | ||
if cancel_futures: | ||
if cancel_futures and self._future_queue is not None: | ||
cancel_items_in_queue(que=self._future_queue) | ||
if self._process is not None: | ||
if self._process is not None and self._future_queue is not None: | ||
self._future_queue.put({"shutdown": True, "wait": wait}) | ||
if wait: | ||
if wait and isinstance(self._process, RaisingThread): | ||
self._process.join() | ||
self._future_queue.join() | ||
self._process = None | ||
|
@@ -151,7 +152,10 @@ def __len__(self) -> int: | |
Returns: | ||
int: The length of the executor. | ||
""" | ||
return self._future_queue.qsize() | ||
queue_size = 0 | ||
if self._future_queue is not None: | ||
queue_size = self._future_queue.qsize() | ||
return queue_size | ||
|
||
def __del__(self): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a stable Python version
Python 3.13 is currently in development/alpha and not recommended for CI environments. Consider using the latest stable version (3.12) instead.
📝 Committable suggestion