I'm currently working through "Python Crash Course" from No Starch and I'm trying to get used to pep8, by writing the code examples with flake8.
from typing import Any
def build_person(
first_name: str,
last_name: str,
age: int = None) -> Any:
#Expression of type "None" cannot be assigned to parameter of type "int"
#Type "None" cannot be assigned to type "int"
"""Return a dictionary of information about a person."""
person = {"first": first_name, "last": last_name}
if age:
person["age"] = age
#Argument of type "int" cannot be assigned to parameter "__v" of type "str"
#in function "__setitem__"
"int" is incompatible with "str"
return person
musician = build_person("Jimi", "Hendrix", 25)
print(musician)
In this piece of code I'm not really sure how I should do the type annotation for the age
argument. I want it to be an int
when used, but simply a None
when not used, but None
is obviously not an int
. Naturally flake8 complains. Also I'm not sure how to annotate the dynamically changing return dict.
Is this simply a case where adherence to pep8 does not make sense, or is there an easy solution?
Secondly, flake8 seems to be unhappy about my way of adding a key-value-pair to the dictionary. I'm not sure what the corresponding message means though.