-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
error "does not return a value" where method really should not return a value #6549
Comments
Mypy assumes that the return value of methods that return |
a type checker would say "this function returns none, and the function you are calling returns none, therefore, the types match". Is mypy also doing code style? |
@JelleZijlstra Sorry but that's completely bonkers. None is a very useful VALUE, and should not receive any special treatment from the type checker, period. Of course, if one tries to use methods/attributes that None does not have, we should see a mypy error, but if I use it as a return, that's completely congruent with the point of None. I would urge you to re-open this and consider fixing it. |
@SpamapS Please remain courteous. This decision to make this distinction (between |
@gvanrossum understood, and apologies for any offense. I was being true to my feelings, but I don't mean to hurt anyone else's in the process! Can you please point us to anywhere the reasoning for that is written down? It feels like mypy is suggesting a coding style, rather than just doing type checking, and that just seems counter to the reasons folks might want to adopt mypy. |
bump - I'd love to see the reason why? I also just ran into a valid use case for a function that only returns None |
"Mypy assumes that the return value of methods that return None should not be used." ^^^ This makes zero sense at all. |
Agree with this 100%, by suggesting a different return type mypy is making a code style recommendation. |
I would recommend monkey patching your local install of mypy to correct this obvious ridiculousness since the developer has surreptitiously closed the ticket and doesn't want to hear any community logic (from experts). |
Also agreed 100% with the comments here. This assumption that a return value of None should not be used does not make sense and I am happy to point to a number of valid use cases where this create issues. |
The stream of "me too" comments isn't very helpful in changing our minds. Here's what you can do if you want this behavior to change:
|
FWIW, pyright does not ascribe any special meaning to a return type annotation of @JelleZijlstra, you said that the main reasoning behind this special-case handling in mypy is to catch bugs where callers are making use of a |
Just ran into this problem where I was trying to optionally throw an error or return None according to a setting. Short example: class Finder:
def __init__(self, strict: bool = True):
self.strict = strict
def raise_or_none(self, e: Exception) -> None:
if self.strict:
raise e
return None
def find_in_list(self, l: list[str], v: str) -> Optional[str]:
for item in l:
if v in item:
return item
return self.raise_or_none(RuntimeError('could not find item in list')) You could also use this pattern to pass None values up a chain of calls or toss the exceptions around like a football. Throwing a type ignore all over the place to be able to use a single function doesn't seem like a great option. |
Tone deaf |
An example:
gets me:
Funny thing is, when I change
Optional
toNone
- everything works fine.Is this a bug or this should be typed somehow differently?
(mypy version
0.670
)The text was updated successfully, but these errors were encountered: