Uploaded image for project: 'SonarPython'
  1. SonarPython
  2. SONARPY-690

Update the default regular expression of rule S101 to not raise on valid PEP-8 class names

    Details

    • Type: False-Positive
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Rules
    • Labels:
      None

      Description

      Python default styling convention is de facto PEP-8. Anything accepted by PEP-8 should be accepted by the few styling rules we have.

      The current regular expression used in RSPEC-101 raises issues on class names starting with two underscores, which is accepted by PEP-8 and can be used to mark a nested class as class-private.

      The current implementation also raises on classes which follow function name convention, i.e. snake-case in lowercase. This is allowed by PEP8:

      Class names should normally use the CapWords convention.
      The naming convention for functions may be used instead in cases where the interface is documented and used primarily as a callable.

      Many projects name their class using function convention when they create decorators, context managers, or simply classes with a __call__ method.

      We could try to make a difference between callable and non-callable classes, but we wouldn't be able to detect every case as we often miss part of the hierarchy.

      Thus the simplest solution is to allow both styles and describe in the RSPEC when each style should be used. If a project needs a stricter naming its developers can change the regular expression. We will avoid making noise by default.

      Here are example of False Positives

      class __FooBar:  # False Positive
          pass
      
      class my_decorator:  # False Positive
          def __enter__(self):
              pass
          def __exit__(self, type, value, traceback):
              pass
      

       
      The new default regular expression should be ^_?([A-Z_][a-zA-Z0-9]*|[a-z_][a-z0-9_]*)$
      Once done the RSPEC should be updated as follow:

      ==============================

      Shared coding conventions allow teams to collaborate effectively. This rule allows to check that all class names match a provided regular expression.

      The default regular expression is based on PEP-8 standard. It allows "CapWords" convention and "snake-case" in lowercase. The "snake-case" convention is accepted by PEP-8 when the class is primarily used as a callable (ex: decorator, context manager, etc...). However the "CapWords" convention is recommended in every case.

      Noncompliant Code Example

      With default provided regular expression ^_?([A-Z_][a-zA-Z0-9]*|[a-z_][a-z0-9_]*)$:

      class myClass:  # Noncompliant
         ...
      
      class my_CONTEXT_manager:  # Noncompliant
          def __enter__(self):
              pass
          def __exit__(self, type, value, traceback):
              pass
      

      Compliant Solution

      class MyClass:
         ...
      
      class my_context_manager:  # Noncompliant
          def __enter__(self):
              pass
          def __exit__(self, type, value, traceback):
              pass
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                nicolas.harraudeau Nicolas Harraudeau
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: