-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Conversation
@@ -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__'): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
Can you also add a test for overloading? |
I'm on it, but I can say that this change works with overloading too. |
@gvanrossum It may be good to add tests for explicit |
Yes, please. |
I think this PR is ready to merge. |
Sorry, I got busy with other stuff. Will review later today.
|
Done (sorry for the delay). Thanks for fixing this long-standing issue! |
…_subclass__ (python#5677) Implements python#604 (comment).
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".
They are noisy and not needed in mypy any more (python/mypy#5677).
This has to be done, because ubuntus mypy is too old (still v0.560). See also python/mypy#5677.
Implements #604 (comment).