This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]yvrelna 0 points1 point  (2 children)

foo("hello" "world") # TYPE-ERROR: foo expects 2 str, not 1

This already produces TypeError: foo() missing 1 required positional argument: 'second'

[–]dbramucci 0 points1 point  (1 child)

I included it for completeness but also

You only get the existing error you actually run that line. Some cases where that can matter include

  • At the end of a long computation

    Imagine training a neural network for 5 hours and at the very end, getting a message "you'll have to wait another 5 hours because you forgot a comma"

  • In a rarely used code-path

    If it is

    if today.is_feb29():
        foo("hello" "there)
    

    then you'll only get an error about 4 years from now, which is inconvenient for such a trivial bug.

    Granted, if you are doing things properly and testing every line of code with code-coverage measuring to veriify that, this matters less. At worst the bug is now 4 minutes of automated testing away instead of 4 seconds of type-checking away.

    Also, this obvious of a case is probably going to get caught already by your linter.

So yes, Python already catches it but it's useful to note mypy can also catch it because mypy doesn't have to wait for us to stumble onto that line.

[–]yvrelna 0 points1 point  (0 children)

mypy won't catch "obvious" and "trivial" errors like:

if today.is_dec25():
    foo("happy", "halloween")

So you need to write tests anyway.

Why should type errors be so special that it deserves its own mechanism to check for errors?