Mypy: Don't require `-> None` on `__init__`

Created on 12 May 2017  路  6Comments  路  Source: python/mypy

There's no code where __init__ should return anything except None, so requiring -> None on it is just useless boilerplate.

I think it should be permitted to omit it, and that it should be inferred, as with self parameters not requiring annotation.

Most helpful comment

For others who end up here, it looks like this point is still open for debate in #604 (and perhaps this should be marked as a Duplicate that issue).

This quote from Guido is worth considering: https://github.com/python/mypy/issues/604#issuecomment-83887491

Hm. I think it'd suck if we'd have to develop the habit of adding explicit
"-> None" to all init methods just to make mypy happy.

I'm with him on that.

All 6 comments

This was already considered, see this section of PEP 484:

(Note that the return type of __init__ ought to be annotated with -> None. The reason for this is subtle. If __init__ assumed a return annotation of -> None, would that mean that an argument-less, un-annotated __init__ method should still be type-checked? Rather than leaving this ambiguous or introducing an exception to the exception, we simply say that __init__ ought to have a return annotation; the default behavior is thus the same as for other methods.)

That's reasonable. I guess I can adjust my IDE snippet to just include it

For others who end up here, it looks like this point is still open for debate in #604 (and perhaps this should be marked as a Duplicate that issue).

This quote from Guido is worth considering: https://github.com/python/mypy/issues/604#issuecomment-83887491

Hm. I think it'd suck if we'd have to develop the habit of adding explicit
"-> None" to all init methods just to make mypy happy.

I'm with him on that.

Is there any way to disable this particular warning?

@gmolau

Is there any way to disable this particular warning?

What warning? You no longer need to put a return type on __init__(), unless it has no arguments other than self.

@gvanrossum Ah sorry, I was on an old mypy version. Thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings