PYL-C0205: Use of simple string as __slots__
value
Fixes this code:
class Example:
__slots__ = 'my_attr'
To this:
class Example:
__slots__ = ['my_attr']
PYL-C0113: Use of an unneeded not
in comparisons
def categorize_number(num):
if not num % 2 == 0:
print("The number is odd.")
else:
print("The number is even.")
Here, the condition not num % 2 == 0
contains an unneeded not
and can be changed into num % 2 != 0
.
The autofix will change it to:
def categorize_number(num):
if num % 2 != 0:
print("The number is odd.")
else:
print("The number is even.")
PTC-W0032: Explicit exception raised in assert
message
assert isinstance(num_channels, int), ValueError("Field is not an integer")
Running this will give AssertionError: Field is not an integer
and not the expected ValueError
.
A better way is to check the condition using an If
statement and raise
the exception if the condition is not satisfied.
The autofix will change it to:
if not isinstance(nem_channels, int):
raise ValueError("Field is not an integer")
PYL-R1721 - Unnecessary use of comprehension
The following pattern:
states = [
('AL', 'Alabama'),
('AK', 'Alaska'),
('AZ', 'Arizona'),
('AR', 'Arkansas'),
('CA', 'California'),
# ...
]
abbreviations_to_names = {
abbreviation: name
for abbreviation, name in states
}
Will be changed to this:
states = [
('AL', 'Alabama'),
('AK', 'Alaska'),
('AZ', 'Arizona'),
('AR', 'Arkansas'),
('CA', 'California'),
# ...
]
abbreviations_to_names = dict(states)
PYL-R1722: Use of exit()
or quit()
This autofix will replace the occurrences of exit
and / or quit
with sys.exit
.
If the sys
module is not imported in the module, the autofix will detect and import it.
So, the following code:
try:
patch_diffs()
except Exception as exc:
print(exc)
exit(1)
will be replaced with:
import sys
try:
patch_diffs()
except Exception as exc:
print(exc)
sys.exit(1)
PYL-W1201: Logging is not lazy
logger.info("This is a log message %s %d %d" % (msg, foo, bar))
Will be fixed as:
logger.info("This is a log message %s %d %d", msg, foo, bar)
PTC-W0037: Private attribute declared but not used
class Example:
def __init__(self, attr=10):
self.__private = "Placeholder"
self.attr = attr
def init_val(self):
print(f"class was initialised with the value: {self.attr}")
Here, the attribute self.__private
is unused and would not be accessible outside of the class. This would be removed during autofix.
PTC-W0038: Private method declared but not used
class Example:
def example_method(self):
print("I am accessible from outside the class")
def __private(self):
print("I am an unused Private method")
Here, the method __private
is unused and would not be accessible outside of the class. This method would be removed during autofix.
PYL-E0236:
__slots__
must contain only non empty string objects otherwise it will raise a TypeError
during runtime.
Not correct:
class ExampleClass:
__slots__ = ("a", "b", "", 34)
Preferred:
class ExampleClass:
__slots__ = ("a", "b")
On autofix, DeepSource will remove the invalid objects present in __slots__
.
PYL-R0202: No @classmethod
decorator
class MyClass:
def cmethod(self):
'''class method-to-be'''
# Convert cmethod to `classmethod`
cmethod = classmethod(cmethod)
Will be changed into:
class MyClass:
@classmethod
def smethod(cls):
'''class method-to-be'''
PYL-R0203: No @staticmethod
decorator
class MyClass:
def smethod():
'''static method-to-be'''
# Convert smethod to `staticmethod`
smethod = staticmethod(smethod)
Will be changed into:
class MyClass:
@staticmethod
def smethod():
'''static method-to-be'''
PYL-W0109: Duplicate dictionary keys
Here, the key random
is provided twice.
DICT = {
"some": "some",
"random": "random",
"dict": "dict",
"values": "values",
"random": "different",
}
The autofix
will remove the first occurrence of the duplicate key.
DICT = {
"some": "some",
"dict": "dict",
"values": "values",
"random": "different",
}