Skip to content

Deal better with "def __init__(self)" return type #7291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JukkaL opened this issue Aug 6, 2019 · 3 comments
Open

Deal better with "def __init__(self)" return type #7291

JukkaL opened this issue Aug 6, 2019 · 3 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 6, 2019

If untyped defs are disallowed, mypy requires that def __init__(self): has an explicit -> None return type, even though normally the return type is optional for __init__. This can be surprising, so it would be better for mypy to produce a special note in this case about why the return type is required even for __init__.

Another idea would be to drop the error message if using --check-untyped-defs, since the explicit return type arguably doesn't add much value.

See #5943 for context.

@raph-amiard
Copy link

Seconding this. It's not a big bother to annotate, but I was really convinced it was a mypy bug and lost some time searching for it on the internets.

@alvis
Copy link

alvis commented Mar 17, 2021

What's more strange is that mypy doesn't complain if there are arguments passed to __init__. At least this inconsistency shouldn't happen. Either you require the return type hint to __init__ in all situation, or none at all (preferred), but not the current form.

@JohanWendelDevies
Copy link

Bit of necromancy on this thread but since it is still open, I agree with previous posters as well, it is not intuitive as it is now with two different behaviors. It leads me as a dev to first expect that there was some other strange bug either with my own project or with mypy.

Since the behavior of requiring return type when no arguments are given has been put in on purpose I think adding a custom error message would be the best solution to this confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants