Skip to content

Commit

Permalink
Enable flake8-bugbear linting
Browse files Browse the repository at this point in the history
  • Loading branch information
tovrstra committed May 22, 2024
1 parent 9a580a4 commit 2cdf8c6
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 74 deletions.
32 changes: 24 additions & 8 deletions iodata/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ def _document_load(
fmt: str,
guaranteed: list[str],
ifpresent: list[str] = None,
kwdocs: dict[str, str] = {},
kwdocs: dict[str, str] = None,
notes: str = None,
):
if kwdocs is None:
kwdocs = {}
ifpresent = ifpresent or []

def decorator(func):
Expand Down Expand Up @@ -91,7 +93,7 @@ def document_load_one(
fmt: str,
guaranteed: list[str],
ifpresent: list[str] = None,
kwdocs: dict[str, str] = {},
kwdocs: dict[str, str] = None,
notes: str = None,
):
"""Decorate a load_one function to generate a docstring.
Expand All @@ -117,6 +119,8 @@ def document_load_one(
A decorator function.
"""
if kwdocs is None:
kwdocs = {}
return _document_load(LOAD_ONE_DOC_TEMPLATE, fmt, guaranteed, ifpresent, kwdocs, notes)


Expand Down Expand Up @@ -146,7 +150,7 @@ def document_load_many(
fmt: str,
guaranteed: list[str],
ifpresent: list[str] = None,
kwdocs: dict[str, str] = {},
kwdocs: dict[str, str] = None,
notes: str = None,
):
"""Decorate a load_many function to generate a docstring.
Expand All @@ -172,6 +176,8 @@ def document_load_many(
A decorator function.
"""
if kwdocs is None:
kwdocs = {}
return _document_load(LOAD_MANY_DOC_TEMPLATE, fmt, guaranteed, ifpresent, kwdocs, notes)


Expand All @@ -180,9 +186,11 @@ def _document_dump(
fmt: str,
required: list[str],
optional: list[str] = None,
kwdocs: dict[str, str] = {},
kwdocs: dict[str, str] = None,
notes: str = None,
):
if kwdocs is None:
kwdocs = {}
optional = optional or []

def decorator(func):
Expand Down Expand Up @@ -236,7 +244,7 @@ def document_dump_one(
fmt: str,
required: list[str],
optional: list[str] = None,
kwdocs: dict[str, str] = {},
kwdocs: dict[str, str] = None,
notes: str = None,
):
"""Decorate a dump_one function to generate a docstring.
Expand All @@ -262,6 +270,8 @@ def document_dump_one(
A decorator function.
"""
if kwdocs is None:
kwdocs = {}
return _document_dump(DUMP_ONE_DOC_TEMPLATE, fmt, required, optional, kwdocs, notes)


Expand All @@ -288,7 +298,7 @@ def document_dump_many(
fmt: str,
required: list[str],
optional: list[str] = None,
kwdocs: dict[str, str] = {},
kwdocs: dict[str, str] = None,
notes: str = None,
):
"""Decorate a dump_many function to generate a docstring.
Expand All @@ -314,6 +324,8 @@ def document_dump_many(
A decorator function.
"""
if kwdocs is None:
kwdocs = {}
return _document_dump(DUMP_MANY_DOC_TEMPLATE, fmt, required, optional, kwdocs, notes)


Expand All @@ -322,9 +334,11 @@ def _document_write(
fmt: str,
required: list[str],
optional: list[str] = None,
kwdocs: dict[str, str] = {},
kwdocs: dict[str, str] = None,
notes: str = None,
):
if kwdocs is None:
kwdocs = {}
optional = optional or []

def decorator(func):
Expand Down Expand Up @@ -381,7 +395,7 @@ def document_write_input(
fmt: str,
required: list[str],
optional: list[str] = None,
kwdocs: dict[str, str] = {},
kwdocs: dict[str, str] = None,
notes: str = None,
):
"""Decorate a write_input function to generate a docstring.
Expand All @@ -407,4 +421,6 @@ def document_write_input(
A decorator function.
"""
if kwdocs is None:
kwdocs = {}
return _document_write(WRITE_INPUT_DOC_TEMPLATE, fmt, required, optional, kwdocs, notes)
2 changes: 1 addition & 1 deletion iodata/formats/gamess.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _read_data(lit: LineIterator) -> tuple:

def _read_coordinates(lit: LineIterator, result: dict) -> tuple:
"""Extract ``numbers`` and ``coordinates`` from the punch file."""
for i in range(2):
for _ in range(2):
next(lit)
natom = len(result["symbols"])
# if the data are already read before, just overwrite them
Expand Down
2 changes: 1 addition & 1 deletion iodata/formats/gaussianlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _load_fourindex_g09(lit: LineIterator, nbasis: int) -> np.ndarray:
"""
result = np.zeros((nbasis, nbasis, nbasis, nbasis))
# Skip first six lines
for i in range(6):
for _i in range(6):
next(lit)
# Start reading elements until a line is encountered that does not start
# with ' I='
Expand Down
37 changes: 22 additions & 15 deletions iodata/formats/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def _parse_json(json_in: dict, lit: LineIterator) -> dict:
f"{lit.filename}: QCSchema files should have a `schema_name` key."
"Attempting to determine schema type...",
FileFormatWarning,
2,
stacklevel=2,
)
# Geometry is required in any molecule schema
if "geometry" in result:
Expand All @@ -673,7 +673,7 @@ def _parse_json(json_in: dict, lit: LineIterator) -> dict:
f"{lit.filename}: QCSchema files should have a `schema_version` key."
"Attempting to load without version number.",
FileFormatWarning,
2,
stacklevel=2,
)

if schema_name == "qcschema_molecule":
Expand Down Expand Up @@ -763,7 +763,7 @@ def _parse_topology_keys(mol: dict, lit: LineIterator) -> dict:
warn(
f"{lit.filename}: QCSchema files should have a '{key}' key.",
FileFormatWarning,
2,
stacklevel=2,
)
for key in topology_keys:
if key not in mol:
Expand All @@ -789,7 +789,7 @@ def _parse_topology_keys(mol: dict, lit: LineIterator) -> dict:
"Some QCSchema writers omit this key for default value 0.0,"
"Ensure this value is correct.",
FileFormatWarning,
2,
stacklevel=2,
)
formal_charge = 0.0
else:
Expand All @@ -804,7 +804,7 @@ def _parse_topology_keys(mol: dict, lit: LineIterator) -> dict:
"Some QCSchema writers omit this key for default value 1,"
"Ensure this value is correct.",
FileFormatWarning,
2,
stacklevel=2,
)
topology_dict["spinpol"] = 0
else:
Expand All @@ -827,7 +827,7 @@ def _parse_topology_keys(mol: dict, lit: LineIterator) -> dict:
"{}: Both `masses` and `mass_numbers` given. "
"Both values will be written to `extra` dict.",
FileFormatWarning,
2,
stacklevel=2,
)
extra_dict["mass_numbers"] = np.array(mol["mass_numbers"])
extra_dict["masses"] = np.array(mol["masses"])
Expand Down Expand Up @@ -940,7 +940,7 @@ def _version_check(result: dict, max_version: float, schema_name: str, lit: Line
f"{lit.filename}: Unknown {schema_name} version {version}, "
"loading may produce invalid results",
FileFormatWarning,
2,
stacklevel=2,
)
return version

Expand Down Expand Up @@ -1083,7 +1083,7 @@ def _parse_input_keys(result: dict, lit: LineIterator) -> dict:
warn(
f"{lit.filename}: QCSchema files should have a '{key}' key.",
FileFormatWarning,
2,
stacklevel=2,
)
for key in input_keys:
if key not in result:
Expand Down Expand Up @@ -1215,14 +1215,17 @@ def _parse_model(model: dict, lit: LineIterator) -> dict:
model_dict["lot"] = model["method"]
# QCEngineRecords doesn't give an empty string for basis-free methods, omits req'd key instead
if "basis" not in model:
warn(f"{lit.filename}: Model `basis` key should be given. Assuming basis-free method.")
warn(
f"{lit.filename}: Model `basis` key should be given. Assuming basis-free method.",
stacklevel=2,
)
elif isinstance(model["basis"], str):
if model["basis"] == "":
warn(
f"{lit.filename}: QCSchema `basis` could not be read and will be omitted."
"Unless model is for a basis-free method, check input file.",
FileFormatWarning,
2,
stacklevel=2,
)
else:
model_dict["obasis_name"] = model["basis"]
Expand Down Expand Up @@ -1256,13 +1259,17 @@ def _parse_protocols(protocols: dict, lit: LineIterator) -> dict:
warn(
"{}: Protocols `wavefunction` key not specified, no properties will be kept.",
FileFormatWarning,
2,
stacklevel=2,
)
wavefunction = "none"
else:
wavefunction = protocols["wavefunction"]
if "stdout" not in protocols:
warn("{}: Protocols `stdout` key not specified, stdout will be kept.", FileFormatWarning, 2)
warn(
"{}: Protocols `stdout` key not specified, stdout will be kept.",
FileFormatWarning,
stacklevel=2,
)
keep_stdout = True
else:
keep_stdout = protocols["stdout"]
Expand Down Expand Up @@ -1338,7 +1345,7 @@ def _parse_output_keys(result: dict, lit: LineIterator) -> dict:
warn(
f"{lit.filename}: QCSchema files should have a '{key}' key.",
FileFormatWarning,
2,
stacklevel=2,
)
for key in output_keys:
if key not in result:
Expand Down Expand Up @@ -1494,7 +1501,7 @@ def _dump_qcschema_molecule(data: IOData) -> dict:
"`charge` and `spinpol` should be given to write qcschema_molecule file:"
"QCSchema defaults to charge = 0 and multiplicity = 1 if no values given.",
FileFormatWarning,
2,
stacklevel=2,
)
if data.charge is not None:
molecule_dict["molecular_charge"] = data.charge
Expand Down Expand Up @@ -1691,7 +1698,7 @@ def _dump_qcschema_output(data: IOData) -> dict:
"No basis name given. QCSchema assumes this signifies a basis-free method; to"
"avoid this warning, specify `obasis_name` as an empty string.",
FileFormatWarning,
2,
stacklevel=2,
)
if "basis" in data.extra["input"]["model"]:
raise NotImplementedError("qcschema_basis is not yet supported in IOData.")
Expand Down
4 changes: 2 additions & 2 deletions iodata/formats/wfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def load_data_wfx(lit: LineIterator) -> dict:
elif key in lbs_other:
result[lbs_other[key]] = value
else:
warnings.warn(f"Not recognized section label, skip {key}")
warnings.warn(f"Not recognized section label, skip {key}", stacklevel=2)

# reshape some arrays
result["atcoords"] = result["atcoords"].reshape(-1, 3)
Expand Down Expand Up @@ -372,7 +372,7 @@ def dump_one(f: TextIO, data: IOData):
for angmom, kind in zip(shell.angmoms, shell.kinds):
n = len(data.obasis.conventions[angmom, kind])
c = raw_coeffs[index_mo_old : index_mo_old + n]
for j in range(shell.nprim):
for _j in range(shell.nprim):
mo_coeffs[index_mo_new : index_mo_new + n] = c
index_mo_new += n
index_mo_old += n
Expand Down
Loading

0 comments on commit 2cdf8c6

Please sign in to comment.