Satisfy flake8 using the following example

Question:

I have a very simple expression below. I am checking each character of a password to ensure it has at least one of the below special characters. However, Flake8 registers the example as bad. How can I address this within Flake8?

W605 invalid escape sequence ‘!’

W605 invalid escape sequence ‘$’

W605 invalid escape sequence ‘^’

W605 invalid escape sequence ‘*’

W605 invalid escape sequence ‘(‘

W605 invalid escape sequence ‘)’

W605 invalid escape sequence ‘+’

W605 invalid escape sequence ‘[‘

W605 invalid escape sequence ‘]’

def clean_password(self):
    special_characters = "[~!@#$%^&*()_+{}":;'[]]"
    if len(self.data["password"]) < 8:
        raise ValidationError("Password length must be greater than 8 characters.")
    if not any(char.isdigit() for char in self.data["password"]):
        raise ValidationError("Password must contain at least 1 digit.")
    if not any(char.isalpha() for char in self.data["password"]):
        raise ValidationError("Password must contain at least 1 letter.")
    if not any(char in special_characters for char in self.data["password"]):
        raise ValidationError("Password must contain at least 1 special character.")
    return self.data["password"]
Asked By: bigredthelogger

||

Answers:

Flake8 is complaining because in your string of special_characters you are escaping some characters that do not need to be escaped.

In your list the only character that needs to be escaped is the double quotes ("), so you can just do:

special_characters = "~!@#$%^&*()_+{}":;'[]"

NOTE: I also removed the first and last squared brakets as they were duplicates

Answered By: Matteo Zanoni

I solved as follows:

def password_check(password):
    """
    Verify the strength of 'password'
    Returns a dict indicating the wrong criteria
    A password is considered strong if:
        8 characters length or more
        1 digit or more
        1 symbol or more
        1 uppercase letter or more
        1 lowercase letter or more
    """

    # calculating the length
    length_error = len(password) < 8

    # searching for digits
    digit_error = re.search(r"d", password) is None

    # searching for uppercase
    uppercase_error = re.search(r"[A-Z]", password) is None

    # searching for lowercase
    lowercase_error = re.search(r"[a-z]", password) is None

    # searching for symbols
    symbol_error = re.search(r"[ !#$%&'()*+,-./[\]^_`{|}~"+r'"]', password) is None

    # overall result
    password_ok = not ( length_error or digit_error or uppercase_error or lowercase_error or symbol_error )

    return {
        'password_ok' : password_ok,
        'length_error' : length_error,
        'digit_error' : digit_error,
        'uppercase_error' : uppercase_error,
        'lowercase_error' : lowercase_error,
        'symbol_error' : symbol_error,
    }


    def clean_password(self):

        userpassword = password_check(self.data["password"])
        
        if (userpassword['length_error']):
            raise ValidationError("Password length must be at least 8 characters.")
        elif(userpassword['digit_error']):
            raise ValidationError("Password must contain at least 1 digit.")
        elif(userpassword['uppercase_error']):
            raise ValidationError("Password must contain at least 1 uppercase character.")
        elif(userpassword['lowercase_error']):
            raise ValidationError("Password must contain at least 1 lowercase character.")
        elif(userpassword['symbol_error']):
            raise ValidationError("Password must contain at least 1 special character.")
        else:
            return self.data["password"]

Answered By: bigredthelogger
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.