S100/S116 rules is not triggered for method/field names in class inheritance

  • Operating system: Ubuntu 22.04.4 LTS (WSL 2)
  • SonarQube for VS Code plugin version: 4.19.0
  • Programming language you’re coding in: Python
  • Is connected mode used: No

The S100/S116 rules is not applied to method/field names in Python classes that inherit from other classes.

Expected behavior: All method/field names should be validated against the naming convention regex, regardless of whether they are defined in base classes or subclasses.

Actual behavior: Methods/fields defined in subclasses using camelCase are not reported as violations.

This makes it easy to miss inconsistent naming across the codebase when using inheritance.

Steps to reproduce:

  1. Define a class with camelCaseMethod/camelCaseField that inherits from a base class.
  2. Enable rules S100/S116 with a regex enforcing snake_case.
  3. No issue is reported by Sonar.

Example:

class A:
    pass


class B(A):
    myVar: str   # Noncompliant - should trigger S116, but it doesn't

    def myTest(self) -> None:   # Noncompliant - should trigger S100, but it doesn't
        return


class C:
    myVar: str   # Noncompliant - correctly triggers S116

    def myTest(self) -> None:   # Noncompliant - correctly triggers S100
        return

Hello @kiber.io !

Thanks for posting, it is always appreciated!
I’ll have a look as soon as possible to the rule and let you know.

Cheers,

1 Like

Hey again @kiber.io,

Thanks for the reproducer and your detailed post.

I took a look at the rule and this is the expected behavior. The rationale is that you might want to inherit from a class defined in another library. If the mother class has a method that is written in CamelCase, and you want to override it, you will raise this issue and you won’t be able to fix it. So this choice was made to reduce the noise, i.e. raising only issues that you can fix.

As you suggest, this can lead to false negatives. I guess we can make the rule more precise by detecting if a method written in camelCase is overriding an existing method rather than only checking that the method’s class is inheriting from another class.

I dig up this old ticket suggesting to do so. It could be an opportunity for us to reprioritize it.
I will consider pushing this ticket in an incoming sprint if we have one related to core Python language.

Thanks again,

1 Like