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

implicit None return checks OK against type hint of bool or Iterator[int] #8054

Closed
dckc opened this issue Dec 2, 2019 · 3 comments
Closed

Comments

@dckc
Copy link

dckc commented Dec 2, 2019

Are you reporting a bug, or opening a feature request?

bug

(or at least: where is this documented?)

Please insert below the code you are checking with mypy

import typing as ty

def each():
    # type: () -> ty.Iterator[int]
    pass


for x in each():
    print(x)

or even just:

def gt(a, b):
    # type: (int, int) -> bool
    "blather..."

What is the actual behavior/output?

Success: no issues found in 1 source file

What is the behavior/output you expect?

An error; either "None found where Iterator[int] expected" or "Not all paths return a value" or some such.

Note that python doesn't like this code:

python ty1.py
Traceback (most recent call last):
  File "ty1.py", line 8, in <module>
    for x in each():
TypeError: 'NoneType' object is not iterable

What are the versions of mypy and Python you are using?

mypy 0.730
Python 3.7.3

Do you see the same issue after installing mypy from Git master?

yes. (mypy-0.760+dev.f9955ae1101cd7d1b38c39e8fba77f1d74986a02 , Python 3.8.0)

What are the mypy flags you are using? (For example --strict-optional)

I was originally using:

MYPYPATH=/home/dconnolly/heron/.direnv/python-2.7.15+/lib/python2.7/site-packages
~/opt/miniconda3/envs/py3tr/bin/mypy -2 \
	--follow-imports=skip \
	--disallow-untyped-calls --disallow-untyped-defs \
		ty1.py

but when I tested mypy from git, I used (a) no flags and (b) --strict.

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 3, 2019

Yeah, this kind of unfortunate. The motivation for this is that it's common to declare abstract methods with only pass in the body, and even if the method is declared to return a non-None value. However, we probably should have at least a strictness setting to catch this issue (if we can't make it the default). Once we support enabling/disabling individual error codes, we could perhaps use this mechanism to implement the option. Surprisingly, I couldn't find an existing issue about this.

@ilevkivskyi
Copy link
Member

Duplicate of #2350

@ilevkivskyi ilevkivskyi marked this as a duplicate of #2350 Dec 3, 2019
@dckc
Copy link
Author

dckc commented Dec 3, 2019

Ah... I do get "Missing return statement" for non-trivial function bodies such as:

def gt(a, b):
    #type: (int, int) -> bool
    x = a + b

So this is a much more narrow issue than I thought.

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

3 participants