Python type hints - descriptive variable names


Python Clean Code Tip:

Avoid using the variable/parameter type inside your variable/parameter name. Use type hints instead.

# BAD: user_list

# GOOD: users: list[User]

Full example👇

from dataclasses import dataclass


@dataclass
class User:
    username: str


# bad
def print_users(user_list):
    for user in user_list:
        print(user.username)


print_users([User(username="johndoe")])
# => johndoe


# good
def print_users(users: list[User]):
    for user in users:
        print(user.username)


print_users([User(username="johndoe")])
# => johndoe