Python logoPython/
PTC-W0031

Use of a method on dictionary's `get` method detectedPTC-W0031

Major severityMajor
Bug Risk categoryBug Risk

Use of a method on the return value of a dictionary's get method without checking if the value exists may give an AttributeError during runtime. By default, the get method returns None if the key is not found in the dictionary.

It is recommended to either provide a default value to be returned by get when key doesn't exist, or check that the default value is not None before using any other method on it.

Bad practice

def generate_username(name):
    return student_record.get(name).lower()
def generate_username(name):
    official_name = student_record.get(name)
    return official_name.lower() if official_name else None