rspec/rules/S2822/python/rule.adoc

54 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
A class without an explicit extension of object (``++class ClassName(object)++``) is considered an old-style class, and ``++__slots__++`` declarations are ignored in old-style classes. Having such a declaration in an old-style class could be confusing for maintainers and lead them to make false assumptions about the class.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
class A:
__slots__ = ["id"] # Noncompliant; this is ignored
def __init__(self):
self.id = id
self.name = "name" # name wasn't declared in __slots__ but there's no error
a = A()
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
class A(object): # Converting to a new-style class is the preferred method of addressing this issue
__slots__ = ["id"]
def __init__(self):
self.id = id
self.name = "name" # "name" is not listed in __slots__, so as expected there is an error in this line
a = A()
----
or
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
class A:
def __init__(self):
self.id = id
self.name = "name"
a = A()
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]