Private nested class declared but not usedPTC-W0064
A private class defined in the class should be only accessible inside the class.
The names of nested classes which are supposed to be public should not start with _
or __
.
This issue concerns that one or more private classes have been declared but are nowhere used in the class.
It is recommended either to remove the unused private classes or remove the __
prefix if it is intended to be accessed from outside the class.
Note:
A private attribute can not be accessed like other attributes.
Consider a class MyClass
with a nested private class: __PrivateClass
.
Now, doing this will raise an AttributeError
:
c = MyClass()
c.__PrivateClass()
But you can access the private class like this (which is not recommended, just for the record):
c = MyClass()
c._MyClass__PrivateClass()
So, if you want to fix this issue:
- Make sure the nested private class is being used in the class it is declared.
- Rename the class to comply with the conventions (Check References).
- If the nested class is unnecessary, remove it.
At the moment, DeepSource can automatically fix the issue by removing the unused private classes.
Bad practice
class AS:
class __NonPublicClass(): # Noncompliant
pass
Recommended
class Compliant:
class __MyNonPublicClass():
pass
def process(self):
return Compliant.__MyNonPublicClass()