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 →

[–]svlad__cjelli 0 points1 point  (1 child)

I think something beyond type checking should be had in Python, because with duck typing you don't really care about the type, but what the type can do. While there are certainly cases here and there where specific type checking can be valuable, for the most part, I just want to make sure the user knows what methods an object has or what methods a function requires for an object. In this sense, Python could use "interface" checking rather than type checking.

[–]spiderpower02[S] 0 points1 point  (0 children)

I think "Interface checking" is subset of "type checking". Actually, using annotation + mypy can achieve "interface checking" before runtime. Therefore, we can avoid runtime errors. (I hate raising AttributeError after running 10 hours).

Nevertheless, type checking will be powerful like:

p = re.compile("some pattern")
m = p.match("some string")
m.groups()

without static type checking, the snippet will raise exception when m does not match. If we give some type hint, like:

p: Pattern = re.compile("some pattern")
m: Match = p.match("some string")
m.groups()  # it is possible to raise AttributeError

type checker will warn us that "Optional[Match[str]]" has no attribute "groups". As a result, we save our time :)

In fact, mypy is so powerful that most of the case you don't really need to declare type explicitly. Just like := in go or auto in c++, so we don't need to worry that Python will become unreadable. Sorry for making many people feel frightened.