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

Rule S5781: Expressions creating sets should not have duplicate values

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.11
    • Component/s: Rules
    • Labels:

      Description

      Expected Issues:

      # string
      {"one", "two"}
      {"one", "two", "one"}  # Noncompliant
      {"one", "two", 'one'}  # Noncompliant. Simple and double quotes are equivalent
      {"""multi
      line""",
      "two",
      """multi
      line"""}  # Noncompliant
      
      # int
      {1, 2, 1}  # Noncompliant
      
      # float
      {1.0, 2.0, 1.0}  # Noncompliant
      
      # bool
      {True, False, True}  # Noncompliant
      
      # int in other bases
      {0o1, 0o2, 0o1}  # Noncompliant
      {0x1, 0x3, 0x1}  # Noncompliant
      
      # None
      {None, None}  # Noncompliant
      
      # equality between int/float/bool types
      {1, 2, 1.0}  # Noncompliant
      {1, 2, True}  # Noncompliant. True == 1
      {0, 2, False}  # Noncompliant. False == 0
      
      # equality between string/raw string/unicode string
      {"one", "two", u"one"}  # Noncompliant
      {"one", "two", r"one"}  # Noncompliant
      
      
      # No issue as there are no duplicates in python 3. In Python 2, bytes and string would be duplicates.
      {"one", "two", b"one"}
      
      #
      # Tuple
      #
      {(1, "2"), 2, (1, "2")}  # Noncompliant
      {(1, "2"), 2, ("2", 1)}  # Ok
      
      
      #
      # Repeated variable or parameter as key
      #
      def func(a1, a2, a3):
          {a1, a2, a1}  # Noncompliant. Not in pylint but pyflakes detects this (F602).
      
          {func, a2, func}  # Noncompliant
      
          [{a, a} for a in range(10)]  # Noncompliant
      
          class MyClass:
              pass
      
          {MyClass, a2, MyClass}  # Noncompliant
      
          {MyClass.__doc__, a2, MyClass.__doc__}  # Noncompliant
      
          {MyClass(), a2, MyClass()}  # Ok
          {a1(), a2, a1()}  # Ok
          {func(1, 2, 3), a2, func(1, 2, 3)}  # Ok
      
      
      #
      # Nice to have but optional as probably rare
      #
      
      {complex(1, 0), 2, complex(1, 0)}  # Noncompliant
      {1, 2, complex(1, 0)}  # Noncompliant
      {frozenset([1]), frozenset([1])}
      {range(1), range(1)}
      
      # Same number with different bases
      {1, 2, 0o1}  # Noncompliant
      {1, 2, 0x1}  # Noncompliant
      
      #
      # Out of scope
      #
      
      # Out of scope: formatted strings
      p = 1
      {f"one{p}", "two", f"one{p}"}  # Bug but out of scope
      
      # Out of scope: Custom classes
      class A:
          def __init__(self, value):
              self.value = value
      
          def __eq__(self, other):
              if not isinstance(other, A):
                  return False
              return other.value == self.value
      
          def __hash__(self):
              return hash(self.value)
      
      {A(1), A(1.0)}  # Bug but out of scope
      
      # Out of scope: python 2 unicode builtin
      # {"one", "two", unicode("one")}  # Noncompliant
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                guillaume.dequenne Guillaume Dequenne
                Reporter:
                nicolas.harraudeau Nicolas Harraudeau
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: