rspec/rules/S1722/python/rule.adoc

47 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The new style of class creation, with the declaration of a parent class, created a unified object model in Python, so that the type of an instantiated class is equal to its class. In Python 2.2-2.7, this is not the case for old-style classes. In Python 3+ all classes are new-style classes. However, since the behavior can differ from 2.2+ to 3+, explicitly inheriting from ``++object++`` (if there is no better candidate) is recommended.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
class MyClass():
pass
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
class MyClass(object):
pass
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add inheritance from "object" or some other new-style class.
'''
== Comments And Links
(visible only on this page)
=== on 15 May 2014, 13:55:35 Pierre-Yves Nicolas wrote:
In Python 2.x, a class is an old-style class if it does not subclass a new-style class. If a class subclasses another class, we should check recursively whether the superclass is a new-style class. If a class does not subclass any class, we know for sure that it is an old-style class.
See \https://docs.python.org/2/whatsnew/2.2.html#old-and-new-classes
endif::env-github,rspecator-view[]