Skip to content

Commit

Permalink
fix E721 Do not compare types, use isinstance()
Browse files Browse the repository at this point in the history
  • Loading branch information
njzjz authored Aug 18, 2023
1 parent 2aeaa94 commit d7677ad
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions dpgen/auto_test/gen_confs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def gen_ele_std(ele_name, ctype):


def gen_element(ele_name, key):
assert type(ele_name) == str
assert isinstance(ele_name, str)

Check warning on line 53 in dpgen/auto_test/gen_confs.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/gen_confs.py#L53

Added line #L53 was not covered by tests
mpr = MPRester(key)
data = mpr.query(
{"elements": [ele_name], "nelements": 1},
Expand Down Expand Up @@ -87,7 +87,7 @@ def gen_element(ele_name, key):


def gen_element_std(ele_name):
assert type(ele_name) == str
assert isinstance(ele_name, str)

Check warning on line 90 in dpgen/auto_test/gen_confs.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/gen_confs.py#L90

Added line #L90 was not covered by tests
for ii in global_std_crystal.keys():
ss = gen_ele_std(ele_name, ii)

Expand Down
2 changes: 1 addition & 1 deletion dpgen/auto_test/lib/vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def reciprocal_box(box):


def make_kspacing_kpoints(poscar, kspacing, kgamma):
if type(kspacing) is not list:
if not isinstance(kspacing, list):
kspacing = [kspacing, kspacing, kspacing]
with open(poscar) as fp:
lines = fp.read().split("\n")
Expand Down
4 changes: 2 additions & 2 deletions dpgen/data/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def make_abacus_relax(jdata, mdata):
) # a dictionary in which all of the values are strings
if "kspacing" not in standard_incar:
if "gamma_only" in standard_incar:
if type(standard_incar["gamma_only"]) == str:
if isinstance(standard_incar["gamma_only"], str):

Check warning on line 579 in dpgen/data/gen.py

View check run for this annotation

Codecov / codecov/patch

dpgen/data/gen.py#L579

Added line #L579 was not covered by tests
standard_incar["gamma_only"] = int(eval(standard_incar["gamma_only"]))
if standard_incar["gamma_only"] == 0:
if "relax_kpt" not in jdata:
Expand Down Expand Up @@ -917,7 +917,7 @@ def make_abacus_md(jdata, mdata):
# "Cannot find any k-points information."
if "kspacing" not in standard_incar:
if "gamma_only" in standard_incar:
if type(standard_incar["gamma_only"]) == str:
if isinstance(standard_incar["gamma_only"], str):

Check warning on line 920 in dpgen/data/gen.py

View check run for this annotation

Codecov / codecov/patch

dpgen/data/gen.py#L920

Added line #L920 was not covered by tests
standard_incar["gamma_only"] = int(eval(standard_incar["gamma_only"]))
if standard_incar["gamma_only"] == 0:
if "md_kpt" not in jdata:
Expand Down
18 changes: 9 additions & 9 deletions dpgen/generator/lib/abacus_scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def make_abacus_scf_input(fp_params, extra_file_path=""):
elif key == "scf_nmax":
fp_params["scf_nmax"] = int(fp_params["scf_nmax"])
assert (
fp_params["scf_nmax"] >= 0 and type(fp_params["scf_nmax"]) == int
fp_params["scf_nmax"] >= 0 and isinstance(fp_params["scf_nmax"], int)
), "'scf_nmax' should be a positive integer."
ret += "scf_nmax %d\n" % fp_params["scf_nmax"]
elif key == "basis_type":
Expand All @@ -73,7 +73,7 @@ def make_abacus_scf_input(fp_params, extra_file_path=""):
elif key == "dft_functional":
ret += "dft_functional %s\n" % fp_params["dft_functional"]
elif key == "gamma_only":
if type(fp_params["gamma_only"]) == str:
if isinstance(fp_params["gamma_only"], str):
fp_params["gamma_only"] = int(eval(fp_params["gamma_only"]))
assert (
fp_params["gamma_only"] == 0 or fp_params["gamma_only"] == 1
Expand All @@ -95,7 +95,7 @@ def make_abacus_scf_input(fp_params, extra_file_path=""):
), "'mixing_beta' should between 0 and 1."
ret += "mixing_beta %f\n" % fp_params["mixing_beta"]
elif key == "symmetry":
if type(fp_params["symmetry"]) == str:
if isinstance(fp_params["symmetry"], str):
fp_params["symmetry"] = int(eval(fp_params["symmetry"]))
assert (
fp_params["symmetry"] == 0 or fp_params["symmetry"] == 1
Expand All @@ -104,7 +104,7 @@ def make_abacus_scf_input(fp_params, extra_file_path=""):
elif key == "nbands":
fp_params["nbands"] = int(fp_params["nbands"])
assert (
fp_params["nbands"] > 0 and type(fp_params["nbands"]) == int
fp_params["nbands"] > 0 and isinstance(fp_params["nbands"], int)
), "'nbands' should be a positive integer."
ret += "nbands %d\n" % fp_params["nbands"]
elif key == "nspin":
Expand Down Expand Up @@ -143,22 +143,22 @@ def make_abacus_scf_input(fp_params, extra_file_path=""):
), "'smearing_sigma' should be non-negative."
ret += "smearing_sigma %f\n" % fp_params["smearing_sigma"]
elif key == "cal_force":
if type(fp_params["cal_force"]) == str:
if isinstance(fp_params["cal_force"], str):
fp_params["cal_force"] = int(eval(fp_params["cal_force"]))
assert (
fp_params["cal_force"] == 0 or fp_params["cal_force"] == 1
), "'cal_force' should be either 0 or 1."
ret += "cal_force %d\n" % fp_params["cal_force"]
elif key == "cal_stress":
if type(fp_params["cal_stress"]) == str:
if isinstance(fp_params["cal_stress"], str):
fp_params["cal_stress"] = int(eval(fp_params["cal_stress"]))
assert (
fp_params["cal_stress"] == 0 or fp_params["cal_stress"] == 1
), "'cal_stress' should be either 0 or 1."
ret += "cal_stress %d\n" % fp_params["cal_stress"]
# paras for deepks
elif key == "deepks_out_labels":
if type(fp_params["deepks_out_labels"]) == str:
if isinstance(fp_params["deepks_out_labels"], str):
fp_params["deepks_out_labels"] = int(
eval(fp_params["deepks_out_labels"])
)
Expand All @@ -176,7 +176,7 @@ def make_abacus_scf_input(fp_params, extra_file_path=""):
), "'deepks_descriptor_lmax' should be a positive integer."
ret += "deepks_descriptor_lmax %d\n" % fp_params["deepks_descriptor_lmax"]
elif key == "deepks_scf":
if type(fp_params["deepks_scf"]) == str:
if isinstance(fp_params["deepks_scf"], str):
fp_params["deepks_scf"] = int(eval(fp_params["deepks_scf"]))
assert (
fp_params["deepks_scf"] == 0 or fp_params["deepks_scf"] == 1
Expand Down Expand Up @@ -447,7 +447,7 @@ def make_supercell_abacus(from_struct, super_cell):

def make_kspacing_kpoints_stru(stru, kspacing):
# adapted from dpgen.autotest.lib.vasp.make_kspacing_kpoints
if type(kspacing) is not list:
if not isinstance(kspacing, list):

Check warning on line 450 in dpgen/generator/lib/abacus_scf.py

View check run for this annotation

Codecov / codecov/patch

dpgen/generator/lib/abacus_scf.py#L450

Added line #L450 was not covered by tests
kspacing = [kspacing, kspacing, kspacing]
box = stru["cells"]
rbox = vasp.reciprocal_box(box)
Expand Down
4 changes: 2 additions & 2 deletions dpgen/generator/lib/ele_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class NBandsEsti:
def __init__(self, test_list):
if type(test_list) is list:
if isinstance(test_list, list):
ele_t = []
vol = []
d_nbd = []
Expand All @@ -28,7 +28,7 @@ def __init__(self, test_list):
self.pref = np.average(alpha)
# print(np.average(alpha), np.std(alpha), self.err/self.pref)
# print((ele_t), vol, d_nbd, nbd, alpha)
elif type(test_list) is str:
elif isinstance(test_list, str):
with open(test_list) as fp:
self.pref = float(fp.readline())
self.err = float(fp.readline())
Expand Down
4 changes: 2 additions & 2 deletions dpgen/generator/lib/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def make_gaussian_input(sys_data, fp_params):
else:
keywords = fp_params["keywords"]

if type(keywords) == str:
if isinstance(keywords, str):
keywords = [keywords]
else:
keywords = keywords.copy()
Expand All @@ -128,7 +128,7 @@ def make_gaussian_input(sys_data, fp_params):

use_fragment_guesses = False
multiplicity = fp_params.get("multiplicity", "auto")
if type(multiplicity) == int:
if isinstance(multiplicity, int):
multiplicity = fp_params["multiplicity"]
mult_auto = False
elif multiplicity == "auto":
Expand Down
12 changes: 6 additions & 6 deletions dpgen/generator/lib/make_calypso.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def make_calypso_input(
assert (
numberofformula is not None
and len(numberofformula) == 2
and type(numberofformula) is list
and isinstance(numberofformula, list)
)
ret += "NumberOfFormula = %s\n" % (" ".join(list(map(str, numberofformula))))
ret += "# The volume per formula unit. Unit is in angstrom^3.\n"
Expand All @@ -66,9 +66,9 @@ def make_calypso_input(
ret += (
"# The population size. Normally, it has a larger number for larger systems.\n"
)
assert popsize is not None and type(popsize) is int
assert popsize is not None and isinstance(popsize, int)
ret += "PopSize = %d\n" % (popsize)
assert maxstep is not None and type(maxstep) is int
assert maxstep is not None and isinstance(maxstep, int)
ret += "# The Max step for iteration\n"
ret += "MaxStep = %d\n" % (maxstep)
ret += "#It determines which method should be adopted in generation the random structure. \n"
Expand All @@ -79,7 +79,7 @@ def make_calypso_input(
ret += "# 0 combination of all method\n"
ret += "# If GenType=3 or 4, it determined the small unit to grow the whole structure\n"
ret += "# It determines which local optimization method should be interfaced in the simulation.\n"
assert icode is not None and type(icode) is int
assert icode is not None and isinstance(icode, int)
ret += "ICode= %d\n" % (icode)
ret += "# ICode= 1 interfaced with VASP\n"
ret += "# ICode= 2 interfaced with SIESTA\n"
Expand All @@ -101,9 +101,9 @@ def make_calypso_input(
ret += "NumberOfParallel = 4\n"
assert split is not None
ret += "Split = %s\n" % (split)
assert pstress is not None and (type(pstress) is int or type(pstress) is float)
assert pstress is not None and (isinstance(pstress, int) or isinstance(pstress, float))
ret += "PSTRESS = %f\n" % (pstress)
assert fmax is not None or type(fmax) is float
assert fmax is not None or isinstance(fmax, float)
ret += "fmax = %f\n" % (fmax)
ret += "################################ End of The Basic Parameters of CALYPSO #######################\n"
if vsc == "T":
Expand Down
2 changes: 1 addition & 1 deletion dpgen/generator/lib/pwmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _update_input_dict(input_dict_, user_dict):
def write_input_dict(input_dict):
lines = []
for key in input_dict:
if type(input_dict[key]) == bool:
if isinstance(input_dict[key], bool):
if input_dict[key]:
rs = "T"
else:
Expand Down
4 changes: 2 additions & 2 deletions dpgen/generator/lib/pwscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
def _convert_dict(idict):
lines = []
for key in idict.keys():
if type(idict[key]) == bool:
if isinstance(idict[key], bool):
if idict[key]:
ws = ".TRUE."
else:
ws = ".FALSE."
elif type(idict[key]) == str:
elif isinstance(idict[key], str):
ws = "'" + idict[key] + "'"
else:
ws = str(idict[key])
Expand Down
2 changes: 1 addition & 1 deletion dpgen/generator/lib/vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _update_incar_dict(incar_dict_, user_dict):
def write_incar_dict(incar_dict):
lines = []
for key in incar_dict:
if type(incar_dict[key]) == bool:
if isinstance(incar_dict[key], bool):
if incar_dict[key]:
rs = "T"
else:
Expand Down
18 changes: 9 additions & 9 deletions dpgen/generator/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ def poscar_shuffle(poscar_in, poscar_out):
def expand_idx(in_list):
ret = []
for ii in in_list:
if type(ii) == int:
if isinstance(ii, int):
ret.append(ii)
elif type(ii) == str:
elif isinstance(ii, str):

Check warning on line 215 in dpgen/generator/run.py

View check run for this annotation

Codecov / codecov/patch

dpgen/generator/run.py#L215

Added line #L215 was not covered by tests
step_str = ii.split(":")
if len(step_str) > 1:
step = int(step_str[1])
Expand Down Expand Up @@ -566,7 +566,7 @@ def make_train(iter_index, jdata, mdata):
mdata["deepmd_version"],
)
assert (
type(model_devi_activation_func) is list
isinstance(model_devi_activation_func, list)
and len(model_devi_activation_func) == numb_models
)
if (
Expand Down Expand Up @@ -601,7 +601,7 @@ def make_train(iter_index, jdata, mdata):
old_model_files = glob.glob(os.path.join(prev_task_path, "model.ckpt*"))
_link_old_models(work_path, old_model_files, ii)
else:
if type(training_iter0_model) == str:
if isinstance(training_iter0_model, str):
training_iter0_model = [training_iter0_model]
iter0_models = []
for ii in training_iter0_model:
Expand Down Expand Up @@ -640,7 +640,7 @@ def _link_old_models(work_path, old_model_files, ii):


def detect_batch_size(batch_size, system=None):
if type(batch_size) == int:
if isinstance(batch_size, int):
return batch_size
elif batch_size == "auto":
# automaticcaly set batch size, batch_size = 32 // atom_numb (>=1, <=fram_numb)
Expand Down Expand Up @@ -1439,7 +1439,7 @@ def _make_model_devi_native(iter_index, jdata, mdata, conf_systems):
for cc in ss:
for tt_ in temps:
if use_ele_temp:
if type(tt_) == list:
if isinstance(tt_, list):

Check warning on line 1442 in dpgen/generator/run.py

View check run for this annotation

Codecov / codecov/patch

dpgen/generator/run.py#L1442

Added line #L1442 was not covered by tests
tt = tt_[0]
if use_ele_temp == 1:
te_f = tt_[1]
Expand All @@ -1448,7 +1448,7 @@ def _make_model_devi_native(iter_index, jdata, mdata, conf_systems):
te_f = None
te_a = tt_[1]
else:
assert type(tt_) == float or type(tt_) == int
assert isinstance(tt_, (float, int))

Check warning on line 1451 in dpgen/generator/run.py

View check run for this annotation

Codecov / codecov/patch

dpgen/generator/run.py#L1451

Added line #L1451 was not covered by tests
tt = float(tt_)
if use_ele_temp == 1:
te_f = tt
Expand Down Expand Up @@ -3258,15 +3258,15 @@ def make_fp_abacus_scf(iter_index, jdata):
if fp_params["basis_type"] == "lcao":
assert (
"fp_orb_files" in jdata
and type(jdata["fp_orb_files"]) == list
and isinstance(jdata["fp_orb_files"], list)
and len(jdata["fp_orb_files"]) == len(fp_pp_files)
)
fp_orb_files = jdata["fp_orb_files"]
dpks_out_labels = fp_params.get("deepks_out_labels", 0)
dpks_scf = fp_params.get("deepks_scf", 0)
if dpks_out_labels or dpks_scf:
assert (
"fp_dpks_descriptor" in jdata and type(jdata["fp_dpks_descriptor"]) == str
"fp_dpks_descriptor" in jdata and isinstance(jdata["fp_dpks_descriptor"], str)
)
fp_dpks_descriptor = jdata["fp_dpks_descriptor"]

Expand Down
2 changes: 1 addition & 1 deletion dpgen/simplify/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def init_model(iter_index, jdata, mdata):
return
iter0_models = []
training_iter0_model = jdata.get("training_iter0_model_path", [])
if type(training_iter0_model) == str:
if isinstance(training_iter0_model, str):

Check warning on line 108 in dpgen/simplify/simplify.py

View check run for this annotation

Codecov / codecov/patch

dpgen/simplify/simplify.py#L108

Added line #L108 was not covered by tests
training_iter0_model = [training_iter0_model]
for ii in training_iter0_model:
model_is = glob.glob(ii)
Expand Down
4 changes: 2 additions & 2 deletions tests/auto_test/test_abacus.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ def test_compuate(self):
def compare_dict(dict1, dict2):
self.assertEqual(dict1.keys(), dict2.keys())
for key in dict1:
if type(dict1[key]) is dict:
if isinstance(dict1[key], dict):
compare_dict(dict1[key], dict2[key])
else:
if type(dict1[key]) is np.ndarray:
if isinstance(dict1[key], np.ndarray):
np.testing.assert_almost_equal(
dict1[key], dict2[key], decimal=5
)
Expand Down
4 changes: 2 additions & 2 deletions tests/auto_test/test_vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ def compare_dict(dict1, dict2):
for key in dict1:
if key == "stress":
self.assertTrue((np.array(dict1[key]["data"]) == dict2[key]).all())
elif type(dict1[key]) is dict:
elif isinstance(dict1[key], dict):
compare_dict(dict1[key], dict2[key])
else:
if type(dict1[key]) is np.ndarray:
if isinstance(dict1[key], np.ndarray):
self.assertTrue((dict1[key] == dict2[key]).all())
else:
self.assertTrue(dict1[key] == dict2[key])
Expand Down

0 comments on commit d7677ad

Please sign in to comment.