Satisfy flake8 using the following example

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"]

>Solution :

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

Leave a Reply