Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5806

Builtins should not be shadowed by local variables

    XMLWordPrintable

    Details

    • Message:
      Rename this variable; it shadows a builtin.
    • Highlighting:
      Hide

      Primary:

      • The first definition of a variable shadowing a builtin in a local scope (function)

      Secondary:

      • Any other assignment to the variable.
      • message: "Variable also assigned here."
      Show
      Primary: The first definition of a variable shadowing a builtin in a local scope (function) Secondary: Any other assignment to the variable. message: "Variable also assigned here."
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Python
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources

      Description

      Shadowing a builtin makes your code more difficult to read and maintain. It may also be a source of bugs as you can reference the builtin by mistake.

      It is sometimes ok to shadow a builtin to improve the readability of a public API or to support multiple versions of a library. In these cases the value is higher than the maintainability cost. Just be careful when you do it.

      It is not ok to shadow builtins with variables which are local to a function or method. These variables are not public and can be easily renamed, thus reducing the confusion and making the code less error-prone.

      This rule raises an issue when the name of a local variable matches the name of a builtin.

      Noncompliant Code Example

      def a_function():
          int = 42  # Noncompliant; int is a builtin
      

      Compliant Solution

      def a_function():
          value = 42
      

      See

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: