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: Thu, 8 Oct 2020 11:35:42 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0

On 10/8/20 3:41 AM, Markus Armbruster wrote:
John Snow <jsnow@redhat.com> writes:

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

I now understand we can omit -> None, except when it's the only type
hint, because omitting it then would make it untyped, which is not what
we want.

Is this just for __init__(), or is it for any function that doesn't
return anything?


*just* __init__. Some other dunders have types that could be assumed, but aren't. I asked about this for some other well-known but hard-to-type dunders like __exit__ on the Typing-SIG list. Not a strong response yet, I might push again later.

--js




reply via email to

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