Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ‘$’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading