Unnecessary "else" after "raise", remove the "else" and de-indent the code inside it
51 )
52
53 if user_auth:
54 if auth.new_password != auth.new_password_confirm: 55 raise HTTPException(
56 status_code=400,
57 detail="Passwords don't match.",
Unnecessary "elif" after "raise", remove the leading "el" from "elif"
8 raise ValueError("Username should be more than 2 characters")
9 elif len(username) >= 255:
10 raise ValueError("Username should be less than 255 characters")
11 if re.match(r"^[a-z0-9.-_]+$", username) is None:12 raise ValueError("Username must be alphanumeric, underscore and dots only")
13 elif username.find("[deleted]") > 0:
14 raise ValueError("Username must be alphanumeric, underscore and dots only")
Unnecessary "elif" after "raise", remove the leading "el" from "elif"
4
5def validate_user(username: str) -> str:
6 # TODO: https://github.com/shouldbee/reserved-usernames/blob/master/reserved-usernames.txt
7 if len(username) <= 2: 8 raise ValueError("Username should be more than 2 characters")
9 elif len(username) >= 255:
10 raise ValueError("Username should be less than 255 characters")
Description
raise
causes control flow to be disrupted, as it will exit the block.
It is recommended to check other conditions using another if
statement, and get rid of else
statements as they are unnecessary.
Bad practice
def check_magic_generator(attribute):
magic = get_magic_number(attribute)
if magic < 0:
raise ValueError(f"{attribute} looks invalid")
elif magic > 100
raise ValueError(f"{attribute} is very magical")
else:
return magic
Recommended
def check_magic_generator(attribute):
magic = get_magic_number(attribute)
if magic < 0:
raise ValueError(f"{attribute} looks invalid")
if magic > 100
raise ValueError(f"{attribute} is very magical")
return magic