qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v5 19/36] qapi/events.py: add type hint annotations


From: John Snow
Subject: Re: [PATCH v5 19/36] qapi/events.py: add type hint annotations
Date: Wed, 7 Oct 2020 11:39:28 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0

On 10/7/20 7:32 AM, Markus Armbruster wrote:
Ignorant question: what's the difference between -> None (like here) and
nothing (like __init__() above?

This came up in Cleber's review, too.

Mypy supports a gradual typing paradigm; it is designed to facilitate what we are doing here: the gradual adding of types until we are able to enforce static typing everywhere.

To that end, mypy uses a simple heuristic to determine if a function is "typed" or "untyped": did you use any type annotations for it?

Meanwhile, __init__ never returns anything. You do not need to annotate its return type. mypy knows what the return type is and must be. In the case of __init__ with no parameters, it is both untyped and strictly typed!

Annotating the return type for no-parameter init methods convinces mypy that this is a strictly typed method. It doesn't do this automatically, because doing so might enable more checks than you were ready for simply because mypy was able to accurately surmise the typing of just __init__.

So it's just a little flip switch to enable strict typing, really.

--js

https://mypy.readthedocs.io/en/stable/class_basics.html#annotating-init-methods




reply via email to

[Prev in Thread] Current Thread [Next in Thread]