Skip to content
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

Make return type implicitly None for type checked __init__ and __init_subclass__ #5677

Merged
merged 8 commits into from
Oct 1, 2018

Conversation

onlined
Copy link
Contributor

@onlined onlined commented Sep 26, 2018

Implements #604 (comment).

@@ -407,6 +406,10 @@ def _visit_func_def(self, defn: FuncDef) -> None:
add_symbol = False
if add_symbol:
self.type.names[defn.name()] = SymbolTableNode(MDEF, defn)
if defn.type is not None and defn.name() in ('__init__', '__init_subclass__'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this works with overloads and decorated methods? Please add tests to verify that.

Also perhaps it should only work inside a class definition?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one needs similar check at least in visit_overloaded_func_def.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gvanrossum It works only inside a class definition.
@gvanrossum @ilevkivskyi You are right, I will find a way to extend behavior.

mypy/semanal.py Outdated Show resolved Hide resolved
@gvanrossum
Copy link
Member

Can you also add a test for overloading?

@onlined
Copy link
Contributor Author

onlined commented Sep 26, 2018

I'm on it, but I can say that this change works with overloading too.

@onlined
Copy link
Contributor Author

onlined commented Sep 27, 2018

@gvanrossum It may be good to add tests for explicit Any return type, what do you think about it?

@gvanrossum
Copy link
Member

@gvanrossum It may be good to add tests for explicit Any return type, what do you think about it?

Yes, please.

@onlined
Copy link
Contributor Author

onlined commented Sep 28, 2018

I think this PR is ready to merge.

@gvanrossum
Copy link
Member

gvanrossum commented Sep 28, 2018 via email

@gvanrossum gvanrossum merged commit 9cd3b0f into python:master Oct 1, 2018
@gvanrossum
Copy link
Member

Done (sorry for the delay). Thanks for fixing this long-standing issue!

TV4Fun pushed a commit to TV4Fun/mypy that referenced this pull request Oct 4, 2018
ilevkivskyi pushed a commit that referenced this pull request Jan 25, 2020
This is the implication of #5677 where the return type of `__init__` is inferred given that at least one argument is typed.

However, if no argument is present in `__init__`, `-> None` becomes compulsory if we are to enable type-checking on that method without using `--check-untyped-defs` flag. I believe it is worth mentioning in the section of "No errors reported for obviously wrong code".
Cito added a commit to graphql-python/graphql-core that referenced this pull request Jan 25, 2020
They are noisy and not needed in mypy any more (python/mypy#5677).
marph91 added a commit to marph91/pocket-cnn that referenced this pull request Mar 15, 2020
This has to be done, because ubuntus mypy is too old (still v0.560).
See also python/mypy#5677.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants