Python analysis upgraded to detect access to "None" values

Hello Python developers,

The Python analysis has been upgraded to detect a bug we all faced at least once:

AttributeError: 'NoneType' object has no attribute 'xyz'

That is basically triggered when a variable/field is assigned the None value and a call is made on that variable:

my_variable = None
[...]
print(my_variable.upper())

Now there is a rule available to detect that before you run your Python code:

  • S2259: Attributes should not be accessed on “None” values

This rule relies on technology for advanced bug detection that was already used for Java and that was made compatible with Python. Even more than for other rules, we are looking for feedback on the results. If you are facing an AttributeError that was not detected by our products, don’t hesitate to share a reproducer.

This is available now on SonarCloud.io and will be part of SonarQube 9.6.

Alex

3 Likes