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

Add implicit -> None type annotation to __init__ methods #389

Closed
ArneBachmann opened this issue Feb 2, 2018 · 10 comments
Closed

Add implicit -> None type annotation to __init__ methods #389

ArneBachmann opened this issue Feb 2, 2018 · 10 comments

Comments

@ArneBachmann
Copy link
Contributor

Should work fine with mypy even if not marked via -> None: (because it's implicit in Python terms)

@eindiran
Copy link
Contributor

eindiran commented Feb 2, 2018

What args are you passing to mypy? I am asking because I just compiled an example without an annotation and ran mypy with my standard set of mypy args, and I did not see a warning.

My standard mypy args: '--ignore-missing-imports', '--python-version', '3.5', '--warn-incomplete-stub', '--warn-redundant-casts', '--warn-return-any', '--warn-unused-ignores'

@evhub evhub added the bug label Feb 2, 2018
@evhub
Copy link
Owner

evhub commented Feb 2, 2018

@ArneBachmann I can't reproduce this; can you give me some example code that replicates this error?

@ArneBachmann
Copy link
Contributor Author

I don't use any of those mypy options, but it seems to have gone away, was probably earlier a thing with Python 2 code compat problems, that made me make it a habit to mark every procedure with -> None. I recently decided to ditch all Python 2 support as it was really hard to achieve, even with Coconut. Just wasn't worth it anymore, especially since any reliable Python console app on Windows requires PEP528/529, supported only from Python 3.6 on (!).

@ArneBachmann
Copy link
Contributor Author

I have it again:

sos/utility.py:65: error: The return type of "__init__" must be None

for

def __init__(_, mapping:Dict[str,Any]): dict.__init__(_, mapping)

Any idea?

@ArneBachmann ArneBachmann reopened this Feb 10, 2018
@auscompgeek
Copy link
Contributor

auscompgeek commented Feb 11, 2018

__init__ should always be annotated with a return type of None, as per PEP 484. Not a bug.

@eindiran
Copy link
Contributor

That's not entirely correct. PEP 484 specifies that __init__ should be annotated with a return type of None if annotated, but that "Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention".

As you can see in this mypy issue, Guido himself complains that "... it'd suck if we'd have to develop the habit of adding explicit '-> None' to all init methods just to make mypy happy". But the issue is still an open bug with mypy, so for the time being, making mypy happy requires that you explicitly add the '-> None'. Hopefully in the near future, __init__ will be assumed to have a default return type of None instead of Any, as mentioned in the final comment.

@evhub evhub added oversight and removed bug labels Feb 14, 2018
@evhub
Copy link
Owner

evhub commented Feb 14, 2018

One possible solution to this is to have Coconut automatically annotate __init__ definitions with a return type of None. Thoughts?

@evhub evhub added this to the v1.3.2 milestone Feb 14, 2018
@ArneBachmann
Copy link
Contributor Author

+1 since also class definitions may leave out the : pass part, similar how -> None could be left out.

@evhub evhub changed the title It's necessary to mark all functions with -> None: to avoid mypy warnings Add implicit -> None type annotation to __init__ methods Mar 26, 2018
@evhub evhub modified the milestones: v1.3.2, v1.4.0 Mar 30, 2018
@baodrate
Copy link

python/mypy#5677

@auscompgeek
Copy link
Contributor

Per the above, this is probably not worth implementing. If someone wants a type-checked no-argument __init__, then they should probably type hint the function signature.

If someone didn't want their no-argument __init__ type-checked, then that should still be possible whilst still running mypy on their code.

@evhub evhub closed this as completed Dec 26, 2018
@evhub evhub removed this from the v1.5.0 milestone Dec 26, 2018
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

5 participants