You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S8494.html
+29-5Lines changed: 29 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,33 @@
1
-
<p>This rule raises an issue when code assigns a value to an attribute that is not listed in the class's <code>__slots__</code> declaration.</p>
1
+
<p>This rule raises an issue when code assigns a value to an attribute that is not listed in the class’s <code>__slots__</code> declaration.</p>
2
2
<h2>Why is this an issue?</h2>
3
-
<p>In Python, the <code>__slots__</code> class attribute is used to explicitly declare which instance attributes a class can have. When a class defines
4
-
<code>__slots__</code>, Python restricts instances to only those attributes, preventing the creation of new attributes dynamically.</p>
5
-
<p>Assigning to an attribute that is not in <code>__slots__</code> causes Python to raise an <code>AttributeError</code> at runtime.</p>
3
+
<p>In Python, the <code>__slots__</code> class attribute is used to explicitly declare which instance attributes a class can have. When a class
4
+
defines <code>__slots__</code>, Python restricts instances to only those attributes, preventing the creation of new attributes dynamically.</p>
5
+
<p>This restriction serves several purposes:</p>
6
+
<ul>
7
+
<li><strong>memory efficiency</strong>: instances use less memory because Python does not create a <code>__dict__</code> for each instance</li>
8
+
<li><strong>faster attribute access</strong>: attribute lookups are faster with <code>__slots__</code></li>
9
+
<li><strong>explicit interface</strong>: the class clearly declares which attributes it supports</li>
10
+
</ul>
11
+
<p>Assigning to an attribute that is not in <code>__slots__</code> causes Python to raise an <code>AttributeError</code> at runtime:</p>
12
+
<pre>
13
+
class Point:
14
+
__slots__ = ['x', 'y']
15
+
16
+
p = Point()
17
+
p.x = 10 # OK
18
+
p.z = 20 # Raises AttributeError: 'Point' object has no attribute 'z'
19
+
</pre>
20
+
<h3>What is the potential impact?</h3>
21
+
<p>When code attempts to assign to an attribute not in <code>__slots__</code>, the application will crash with an <code>AttributeError</code> at
22
+
runtime. This can lead to:</p>
23
+
<ul>
24
+
<li><strong>application crashes</strong>: the error will terminate the current operation unless properly handled</li>
25
+
<li><strong>data loss</strong>: if the assignment happens during a data processing operation, partial results may be lost</li>
26
+
<li><strong>poor user experience</strong>: users may encounter unexpected errors during normal operations</li>
27
+
<li><strong>difficult debugging</strong>: the error may only occur in specific scenarios, making it hard to reproduce and fix</li>
28
+
</ul>
6
29
<h2>How to fix it</h2>
7
-
<p>Add the missing attribute to the <code>__slots__</code> declaration.</p>
30
+
<p>If the attribute should be part of the class’s interface, add it to the <code>__slots__</code> declaration.</p>
0 commit comments