Write Clean Machine Learning code with PyTorch

Hello Pythonistas,

Sonar can now help you write clean Python code with PyTorch, one of the most widely adopted deep-learning frameworks. These rules help you to avoid pitfalls where you spend a lot of your time such as in model training and evaluation.

  • S6978: Subclasses of “torch.nn.Module” should call the initializer
  • S6979: “torch.tensor” should be used instead of “torch.autograd.Variable”
  • S6982: “model.eval()” or “model.train()” should be called after loading the state of a PyTorch model
  • S6983: The “num_workers” parameter should be specified for “torch.utils.data.DataLoader”
  • S6984: Einops pattern should be valid
  • S6985: Usage of “torch.load” can lead to untrusted code execution

Additionally, S6973 has been extended to cover the PyTorch framework.

These rules are now available in SonarCloud, from SonarQube 10.7, and the next SonarLint release in your favorite IDE.

As always, let us know your feedback below. Take a glimpse of what’s coming up for Python in SonarLint, SonarQube and SonarCloud .

Jean

1 Like