Skip to content
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

Unused imports #507

Closed
elcorto opened this issue Mar 26, 2024 · 2 comments
Closed

Unused imports #507

elcorto opened this issue Mar 26, 2024 · 2 comments

Comments

@elcorto
Copy link
Member

elcorto commented Mar 26, 2024

There are many unused imports. While not critical, some may have import speed implications. Even if not, it tends to make maintainability a bit more difficult.

Here is the result of a check with ruff 0.3.4 (rule F401):

$ ruff check --select F401 $(find mala -name "*.py") | grep -v __init__
mala/datahandling/data_handler.py:5:29: F401 `horovod.torch` imported but unused; consider using `importlib.util.find_spec` to test for availability
mala/datahandling/data_shuffler.py:6:8: F401 [*] `mala` imported but unused
mala/datahandling/data_shuffler.py:7:36: F401 [*] `mala.common.parameters.ParametersData` imported but unused
mala/datahandling/lazy_load_dataset.py:11:30: F401 [*] `torch.utils.data.Dataset` imported but unused
mala/datahandling/lazy_load_dataset_single.py:7:30: F401 [*] `torch.utils.data.Dataset` imported but unused
mala/datahandling/lazy_load_dataset_single.py:7:39: F401 [*] `torch.utils.data.DataLoader` imported but unused
mala/datahandling/snapshot.py:2:21: F401 [*] `os.path.join` imported but unused
mala/datahandling/snapshot.py:4:17: F401 [*] `numpy` imported but unused
mala/descriptors/atomic_density.py:7:24: F401 `lammps.lammps` imported but unused; consider using `importlib.util.find_spec` to test for availability
mala/descriptors/atomic_density.py:18:43: F401 [*] `mala.descriptors.lammps_utils.set_cmdlinevars` imported but unused
mala/descriptors/bispectrum.py:7:24: F401 `lammps.lammps` imported but unused; consider using `importlib.util.find_spec` to test for availability
mala/descriptors/bispectrum.py:17:43: F401 [*] `mala.descriptors.lammps_utils.set_cmdlinevars` imported but unused
mala/descriptors/minterpy_descriptors.py:7:24: F401 `lammps.lammps` imported but unused; consider using `importlib.util.find_spec` to test for availability
mala/descriptors/minterpy_descriptors.py:18:43: F401 [*] `mala.descriptors.lammps_utils.set_cmdlinevars` imported but unused
mala/descriptors/minterpy_descriptors.py:97:28: F401 [*] `lammps.lammps` imported but unused
mala/interfaces/ase_calculator.py:4:17: F401 [*] `numpy` imported but unused
mala/interfaces/ase_calculator.py:6:69: F401 [*] `mala.Density` imported but unused
mala/interfaces/ase_calculator.py:7:18: F401 [*] `mala.DOS` imported but unused
mala/interfaces/ase_calculator.py:8:38: F401 [*] `mala.common.parallelizer.get_rank` imported but unused
mala/interfaces/ase_calculator.py:8:48: F401 [*] `mala.common.parallelizer.get_comm` imported but unused
mala/interfaces/ase_calculator_simple_ensemble.py:6:30: F401 [*] `mala.Network` imported but unused
mala/interfaces/ase_calculator_simple_ensemble.py:7:38: F401 [*] `mala.common.parallelizer.get_rank` imported but unused
mala/interfaces/ase_calculator_simple_ensemble.py:7:48: F401 [*] `mala.common.parallelizer.get_comm` imported but unused
mala/network/hyper_opt_oat.py:4:8: F401 [*] `os` imported but unused
mala/network/hyperparameter_acsd.py:2:26: F401 [*] `optuna.trial.Trial` imported but unused
mala/network/predictor.py:2:8: F401 [*] `ase.io` imported but unused
mala/network/predictor.py:4:29: F401 `horovod.torch` imported but unused; consider using `importlib.util.find_spec` to test for availability
mala/network/simple_ensemble_predictor.py:3:30: F401 [*] `zipfile.ZIP_STORED` imported but unused
mala/network/simple_ensemble_predictor.py:5:8: F401 [*] `ase.io` imported but unused
mala/network/simple_ensemble_predictor.py:7:29: F401 `horovod.torch` imported but unused; consider using `importlib.util.find_spec` to test for availability
mala/network/simple_ensemble_predictor.py:11:17: F401 [*] `numpy` imported but unused
mala/network/simple_ensemble_predictor.py:12:8: F401 [*] `torch` imported but unused
mala/network/simple_ensemble_predictor.py:14:38: F401 [*] `mala.common.parallelizer.printout` imported but unused
mala/network/simple_ensemble_predictor.py:14:48: F401 [*] `mala.common.parallelizer.get_rank` imported but unused
mala/network/simple_ensemble_predictor.py:14:58: F401 [*] `mala.common.parallelizer.barrier` imported but unused
mala/network/tester.py:3:29: F401 `horovod.torch` imported but unused; consider using `importlib.util.find_spec` to test for availability
mala/network/trainer.py:19:38: F401 [*] `mala.common.parallelizer.parallel_warn` imported but unused
mala/network/trainer.py:21:34: F401 [*] `mala.network.network.Network` imported but unused
mala/targets/calculation_helpers.py:6:8: F401 [*] `sys` imported but unused
mala/targets/density.py:2:8: F401 [*] `os` imported but unused
mala/targets/density.py:5:8: F401 [*] `ase.io` imported but unused
mala/targets/target.py:2:17: F401 [*] `abc.ABC` imported but unused
mala/targets/target.py:7:30: F401 [*] `ase.neighborlist.NeighborList` imported but unused
Found 124 errors.
[*] 36 fixable with the `--fix` option.

I'm excluding __init__.py since imports in there are "not used" but needed for namespace reasons. There may be other false positives, but most of them are valid.

An example from mala/network/predictor.py (note that also the module doc string is the one from mala/network/tester.py, but that's a separate issue):

"""Tester class for testing a network."""
import ase.io
try:
    import horovod.torch as hvd
except ModuleNotFoundError:
    # Warning is thrown by Parameters class
    pass

ase.io is not used. I'm no expert in horovod so I'm not sure if importing it is needed even if not used in the code.

@RandomDefaultUser
Copy link
Member

Hi @elcorto thanks for bringing this to my attention, I will have a look! :)
I think many of these are just from checking whether or not a particular library is available. Many of these can probably be rewritten using the importlib library or be completely deleted, like the horovod import.

@elcorto
Copy link
Member Author

elcorto commented Apr 18, 2024

Closed by #511

@elcorto elcorto closed this as completed Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants