Skip to content

Commit 687fb41

Browse files
Clean up unused imports and improve type hints (#324)
This pull request primarily removes unused imports and cleans up type annotations across several modules in the `dpgen2` codebase. These changes help streamline the code, reduce unnecessary dependencies, and improve readability. Additionally, some minor logic and formatting updates are included to modernize the code and ensure consistency. ### Code cleanup and import removal * Removed unused imports such as `os`, `json`, `logging`, `glob`, `pickle`, and several type hints (`Set`, `Union`, etc.) from multiple files including `main.py`, `showkey.py`, `status.py`, `submit.py`, `watch.py`, `workflow.py`, `prep_fp.py`, `run_fp.py`, and `vasp.py`. This reduces clutter and potential confusion for future maintenance. [[1]](diffhunk://#diff-566f49ecd2afbc4c7a22cd2f576a706a0fea95bed20f560240e328541830494dL4-L19) [[2]](diffhunk://#diff-dc5e3ffd275993a4866b65927e0972ad7c65aac2e195b3ca9fdb43254278f265L1-L8) [[3]](diffhunk://#diff-70638d2beeba19acd9b8de0bdf0bf976ba14b6f5e5f8dd24b1f84aaf23840425L4-L6) [[4]](diffhunk://#diff-518b16955e40ff2d3d6308e0dff9a6800306adecead3255ccf8c71a146723588L2-L7) [[5]](diffhunk://#diff-518b16955e40ff2d3d6308e0dff9a6800306adecead3255ccf8c71a146723588L18-L23) [[6]](diffhunk://#diff-b80b08ddd1a5fe0cc9352de549f54b44ca1fbd63fd6e1300c756818bf2ef2dfdL7) [[7]](diffhunk://#diff-74c1d2a47b53b85b573096163113600a7dd410f2a410bc9699e90c26af23d226L2-L4) [[8]](diffhunk://#diff-8d2a04f59606da4f76ec76bc363f743a27ee018f3be91607b428165a5b56cba8L1-L2) [[9]](diffhunk://#diff-8d2a04f59606da4f76ec76bc363f743a27ee018f3be91607b428165a5b56cba8L12-L16) [[10]](diffhunk://#diff-c7fc0e85df33ae92aa2222a509449350292360031e74b03b169a3c7ba84dec29L1) [[11]](diffhunk://#diff-c7fc0e85df33ae92aa2222a509449350292360031e74b03b169a3c7ba84dec29L13-L18) [[12]](diffhunk://#diff-2f01a3587ee0b6fde430a0c71d0542ec7bd74bf9e57da500338b11afbf0df1c1L9-L14) [[13]](diffhunk://#diff-34aa3497602bd93c83e17bd39502560ca7c9b6d09bacba3af33be0244a784477L13) ### Type annotation improvements * Updated function signatures to use modern type hinting, such as changing `Optional[Dict] = {}` to `Optional[Dict] | None = None` and ensuring default values are properly handled with `wf_config = wf_config or {}`. This makes the code safer and more in line with current Python standards. [[1]](diffhunk://#diff-b80b08ddd1a5fe0cc9352de549f54b44ca1fbd63fd6e1300c756818bf2ef2dfdL46-R67) [[2]](diffhunk://#diff-74c1d2a47b53b85b573096163113600a7dd410f2a410bc9699e90c26af23d226L41-R40) ### Minor logic and formatting updates * Improved string formatting in logging statements and conditional checks for better readability and consistency. For example, switched to f-strings with consistent quotation marks and improved assertion error messages. [[1]](diffhunk://#diff-b80b08ddd1a5fe0cc9352de549f54b44ca1fbd63fd6e1300c756818bf2ef2dfdL46-R67) [[2]](diffhunk://#diff-518b16955e40ff2d3d6308e0dff9a6800306adecead3255ccf8c71a146723588L591-R584) These changes collectively enhance code maintainability and readability without altering core functionality.This pull request primarily cleans up unused imports and improves code readability across several entrypoint modules in the `dpgen2` package. It also introduces minor enhancements to type hints and default argument handling for better code clarity and robustness. **Codebase cleanup and readability:** * Removed unused imports (such as `os`, `glob`, `pickle`, `dpdata`, etc.) from multiple files including `main.py`, `showkey.py`, `status.py`, `submit.py`, and `workflow.py` to streamline the codebase. [[1]](diffhunk://#diff-566f49ecd2afbc4c7a22cd2f576a706a0fea95bed20f560240e328541830494dL4-L19) [[2]](diffhunk://#diff-dc5e3ffd275993a4866b65927e0972ad7c65aac2e195b3ca9fdb43254278f265L1-L8) [[3]](diffhunk://#diff-70638d2beeba19acd9b8de0bdf0bf976ba14b6f5e5f8dd24b1f84aaf23840425L4-L6) [[4]](diffhunk://#diff-518b16955e40ff2d3d6308e0dff9a6800306adecead3255ccf8c71a146723588L2-L7) [[5]](diffhunk://#diff-74c1d2a47b53b85b573096163113600a7dd410f2a410bc9699e90c26af23d226L2-R2) * Simplified assertion error messages and improved string formatting for logging statements in `submit.py` and `watch.py`. [[1]](diffhunk://#diff-518b16955e40ff2d3d6308e0dff9a6800306adecead3255ccf8c71a146723588L591-R583) [[2]](diffhunk://#diff-b80b08ddd1a5fe0cc9352de549f54b44ca1fbd63fd6e1300c756818bf2ef2dfdL46-R67) **Type hint and argument improvements:** * Updated type hints for optional dictionary arguments in `watch.py` and `workflow.py` to use `Optional[Dict] | None`, and ensured proper default initialization using `wf_config = wf_config or {}`. [[1]](diffhunk://#diff-b80b08ddd1a5fe0cc9352de549f54b44ca1fbd63fd6e1300c756818bf2ef2dfdL46-R67) [[2]](diffhunk://#diff-74c1d2a47b53b85b573096163113600a7dd410f2a410bc9699e90c26af23d226L41-R38) * Minor formatting improvements for argument parsing and function calls for better readability in `main.py`. [[1]](diffhunk://#diff-566f49ecd2afbc4c7a22cd2f576a706a0fea95bed20f560240e328541830494dL87-R77) [[2]](diffhunk://#diff-566f49ecd2afbc4c7a22cd2f576a706a0fea95bed20f560240e328541830494dL384-R372) **General code style enhancements:** * Removed unnecessary tuple and union type imports, and cleaned up type hints in `submit.py` and `status.py`. [[1]](diffhunk://#diff-518b16955e40ff2d3d6308e0dff9a6800306adecead3255ccf8c71a146723588L18-L23) [[2]](diffhunk://#diff-70638d2beeba19acd9b8de0bdf0bf976ba14b6f5e5f8dd24b1f84aaf23840425L4-L6) [[3]](diffhunk://#diff-b80b08ddd1a5fe0cc9352de549f54b44ca1fbd63fd6e1300c756818bf2ef2dfdL7) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Removed numerous unused imports across entry points and FP modules to reduce dependency surface and trim code. * Standardized logging/string formatting for consistency. * **Bug Fixes** * Changed workflow-config default from a mutable empty dict to None with internal initialization to avoid shared-state issues. * Corrected membership check used when computing finished-step diffs to fix status reporting. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9b6f382 commit 687fb41

10 files changed

Lines changed: 8 additions & 50 deletions

File tree

dpgen2/entrypoint/main.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
import argparse
22
import json
33
import logging
4-
import os
54
import textwrap
65
from typing import (
76
List,
87
Optional,
98
)
109

11-
import dflow
12-
from dflow import (
13-
Step,
14-
Steps,
15-
Workflow,
16-
download_artifact,
17-
upload_artifact,
18-
)
19-
2010
from dpgen2 import (
2111
__version__,
2212
)

dpgen2/entrypoint/showkey.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
import glob
2-
import os
3-
import pickle
4-
from pathlib import (
5-
Path,
6-
)
7-
8-
import dpdata
91
from dflow import (
102
Workflow,
113
)

dpgen2/entrypoint/status.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import logging
22
from typing import (
33
Dict,
4-
List,
54
Optional,
6-
Union,
75
)
86

97
from dflow import (

dpgen2/entrypoint/submit.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import copy
2-
import glob
32
import json
43
import logging
54
import os
6-
import pickle
7-
import re
85
from copy import (
96
deepcopy,
107
)
@@ -15,12 +12,8 @@
1512
Dict,
1613
List,
1714
Optional,
18-
Tuple,
19-
Type,
20-
Union,
2115
)
2216

23-
import dpdata
2417
from dflow import (
2518
ArgoStep,
2619
InputArtifact,
@@ -588,7 +581,7 @@ def workflow_concurrent_learning(
588581
if fp_style == "deepmd":
589582
assert (
590583
"teacher_model_path" in fp_config["run"]
591-
), f"Cannot find 'teacher_model_path' in config['fp']['run_config'] when fp_style == 'deepmd'"
584+
), "Cannot find 'teacher_model_path' in config['fp']['run_config'] when fp_style == 'deepmd'"
592585
assert os.path.exists(
593586
fp_config["run"]["teacher_model_path"]
594587
), f"No such file: {fp_config['run']['teacher_model_path']}"

dpgen2/entrypoint/watch.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
Dict,
55
List,
66
Optional,
7-
Union,
87
)
98

109
from dflow import (
@@ -43,28 +42,29 @@ def update_finished_steps(
4342
if finished_keys is not None:
4443
diff_keys = []
4544
for kk in wf_keys:
46-
if not (kk in finished_keys):
45+
if kk not in finished_keys:
4746
diff_keys.append(kk)
4847
else:
4948
diff_keys = wf_keys
5049
for kk in diff_keys:
51-
logging.info(f'steps {kk.ljust(50,"-")} finished')
50+
logging.info(f"steps {kk.ljust(50, '-')} finished")
5251
if download:
5352
download_dpgen2_artifacts(wf, kk, prefix=prefix, chk_pnt=chk_pnt)
54-
logging.info(f'steps {kk.ljust(50,"-")} downloaded')
53+
logging.info(f"steps {kk.ljust(50, '-')} downloaded")
5554
finished_keys = wf_keys
5655
return finished_keys
5756

5857

5958
def watch(
6059
workflow_id,
61-
wf_config: Optional[Dict] = {},
60+
wf_config: Optional[dict] = None,
6261
watching_keys: Optional[List] = default_watching_keys,
6362
frequency: float = 600.0,
6463
download: bool = False,
6564
prefix: Optional[str] = None,
6665
chk_pnt: bool = False,
6766
):
67+
wf_config = wf_config or {}
6868
wf_config = normalize_args(wf_config)
6969

7070
global_config_workflow(wf_config)

dpgen2/entrypoint/workflow.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import argparse
2-
import json
3-
import logging
4-
import os
52
from typing import (
63
Optional,
74
)
@@ -38,8 +35,9 @@ def add_subparser_workflow_subcommand(subparsers, command: str):
3835
def execute_workflow_subcommand(
3936
command: str,
4037
wfid: str,
41-
wf_config: Optional[dict] = {},
38+
wf_config: Optional[dict] = None,
4239
):
40+
wf_config = wf_config or {}
4341
wf_config = normalize_args(wf_config)
4442
global_config_workflow(wf_config)
4543
wf = Workflow(id=wfid)

dpgen2/fp/prep_fp.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import json
2-
import os
31
from abc import (
42
ABC,
53
abstractmethod,
@@ -9,11 +7,8 @@
97
)
108
from typing import (
119
Any,
12-
Dict,
1310
List,
14-
Set,
1511
Tuple,
16-
Union,
1712
)
1813

1914
import dpdata

dpgen2/fp/run_fp.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import os
32
from abc import (
43
ABC,
@@ -10,12 +9,10 @@
109
from typing import (
1110
Dict,
1211
List,
13-
Set,
1412
Tuple,
1513
)
1614

1715
import dargs
18-
import dpdata
1916
from dflow.python import (
2017
OP,
2118
OPIO,

dpgen2/fp/vasp.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
Path,
77
)
88
from typing import (
9-
Dict,
109
List,
11-
Optional,
12-
Set,
1310
Tuple,
14-
Union,
1511
)
1612

1713
import dpdata

dpgen2/fp/vasp_input.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
Union,
1111
)
1212

13-
import dpdata
1413
import numpy as np
1514
from dargs import (
1615
Argument,

0 commit comments

Comments
 (0)