Python Analyzer Updates -- January 2021

New Issues:

10 new issues have been added to the Python analyzer. Here are the issues with examples:

Bad async magic method (PTC-W0045):

class BadClass:
    async def __lt__(self, other):

Unit test class with no tests (PTC-W0046):

import unittest

def Tests(unittest.TestCase):
    def my_test(self, arg1, arg2):
        self.assertEquals(arg1, arg2)

Empty block of code (PTC-W0047):

for _ in range(10):

if statements can be merged (PTC-W0048):

if condition1:
    if condition2:

Function/method with an empty body (PTC-W0049):

def my_func(foo="Noncompliant"):

class MyClass:
    def mymethod1(self, foo="Noncompliant"):

Duplicate elements during set declaration (PTC-W0050):

my_set = {"one", "two", "one"}

def myfunc(a, b, c):
    my_set = {a, b, a, c}

Abstract method does not raise NotImplementedError (PTC-W0053):

import abc

class Vehicle:
    __metaclass__ = abc.ABCMeta

    def method_to_implement(self, input):

Special method should return NotImplemented (PTC-W0054):

class A:
    def __init__(self, val): 
        self.val = val 
    def __add__(self, other): 
        raise NotImplementedError
class B: 
    def __init__(self, val): 
        self.val = val 
    def __add__(self, other): 
        return self.val + other.val 
    def __radd__(self, other): 
        return self.val + other.val
A(1) + B(1)  # This will raise `NotImplementedError`

Audit Issue: Unverified server certificate (PTC-W6001):

import ssl

context = ssl.create_default_context()
ctx3.verify_mode = ssl.CERT_NONE  # Setting this won't verify the certificate

Audit Issue: Unverified server hostname (PTC-W6002):

import ssl

context = ssl._create_stdlib_context()  # by default hostname verification is not done

Autofix Additions:

The Python analyzer can now automatically fix 6 more issues.


This fixer removes the unnecessary del statement from the local scope.


This changes the use of len(seq) - 1 to get the last element.
So, an occurrence like seq[len(seq)-1] would be changed to seq[-1].


This fixer converts the async magic method into a regular method.


This fixer removes duplicate elements during a set declaration


Provide blank=True for a field with null=True to Django fields that allow null but not blank.


Remove redundant default=None from Django fields.

1 Like

Adding to a false-positive report about PTC-W0053, that I just made: It’s a false positive only when the class inherits from abc.ABC, i.e.:

import abc

class Vehicle(abc.ABC):

    def method_to_implement(self, input):

In that case Vehicle can’t be initialized, so there is no need to raise NotImplementedError :slight_smile:

Sounds about right.
Looks like we missed this here.

Allow us some time to look into this. We will drop an update here!

1 Like

Update: This case is now handled. Kudos to you for bringing this to our notice. :raised_hands:

1 Like