Skip to content

Return value expected on def f() -> Optional[Type]: with plain return #1003

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

Closed
darjus opened this issue Nov 25, 2015 · 4 comments
Closed

Return value expected on def f() -> Optional[Type]: with plain return #1003

darjus opened this issue Nov 25, 2015 · 4 comments

Comments

@darjus
Copy link
Contributor

darjus commented Nov 25, 2015

mypy is expecting me to explicitly return None instead of return, but I'd expect that to be inferred as there's little point in me telling it i return None

def f(i: int) -> Optional[int]:
    if i > 3:
        return  # fails but return None works
    return i * 3
@gvanrossum
Copy link
Member

Actually this is intentional -- it's a code smell and even called out in PEP 8 (https://www.python.org/dev/peps/pep-0008/#programming-recommendations, "Be consistent in return statements.")

@darjus
Copy link
Contributor Author

darjus commented Nov 25, 2015

Thanks for explaining this @gvanrossum . I've read pep8 several times before but didn't quite follow this specific recommendation, moreover pep8 command does not complain (at least my version) about this.
The actual message is error: Return value expected which threw me off.
Should we invest in a more descriptive message and or documentation (I'm willing to submit a pr for this), or you think this is sufficient?

@gvanrossum
Copy link
Member

gvanrossum commented Nov 25, 2015 via email

@mlissner
Copy link

I'm all for eliminating code smells, but I just struggled with this error a bit because I'm not in the state of mind to fix pep8 stuff when I run mypy.

I'd suggest removing this warning since it isn't about typing. If the function returns None and that's what the signature calls for, then mypy shouldn't complain otherwise.

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

No branches or pull requests

3 participants