3 ms·
It doesn't affect runtime at all (except when it does :P, but that's a bug). So there is a difference between what's valid in mypy, and what's valid in "real" P
by squeaky-clean 10y ago
It doesn't affect runtime at all (except when it does :P, but that's a bug). So there is a difference between what's valid in mypy, and what's valid in "real" Python.
Typing does associate type with the names as well, which isn't necessary for legal Python (but is often cleaner). For example, mypy will yell at you if you do something like this:
def add_week_to_day(d: str) -> str:
# d arrives as an ISO string, like '2016-12-23'
d = datetime.strptime(d, '%Y-%m-%d')
d = d + timedelta(days=7)
return d.strftime('%Y-%m-%d')
Because d was original defined as a str, and now I'm re-using it as a datetime. This is of course perfectly valid Python, and will work at runtime. But mypy prefers you use a different name for the different types.
- fithisux 10y agoIf language permits it, then #$%^& mypy. My bet is on types.
- amyjess 10y agoIt's not like mypy is the only linter that warns you about stuff that's technically legal. Like, I make it a point to run all my stuff through pylint, and it typically catches a few things that will technically run but would cause problems.
- squeaky-clean 10y agoReally there's many more examples where re-using a name causes more harm than where it's idiomatic like the one above. And you always add an ignore comment to that specific line.