You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stub functions should only typecheck for None #4066
mypy --py2 --strict demo.py does not show any errors. If a function body does not contain a return statement or a yield statement it should only have a type of None I believe.
This is a special case in mypy. If a function have an empty body then it can have arbitrary return type. This is useful for testing, and is necessary for stubs. We have lots of tests that use this, so we can't disable this in non-stub files. Alternatively we can somehow detect whether we are running tests, and display an error otherwise.
Mypy should probably eventually enforce this outside stubs. One way would be to make this an option (which doesn't need to be shown in help, perhaps) and turn it on by default, but we'd turn it off for most tests. Detecting stubs is easy, so it won't be a problem. We don't need to actually detect tests if all test runners just explicitly toggle the option.
Aha, I didn't realise that mypy treated stubs specially. My intent was to minimise my example, but I initially encountered this issue on a function that calls exit:
I propose to change the title of this issue to "Get rid of exception for functions with just pass as body" -- or something better (but I can't think of something better). The current title is too wide and in general incorrect.
changed the title [-]A function without return or yield should only typecheck for None[/-][+]Stub functions should only typecheck for None[/+]on Oct 10, 2017
@JelleZijlstra the NoReturn behaviour was surprising to me. Could I open an issue for a --strict option that disables this behaviour, or is this working as intended?
@WilfredNoReturn is the bottom type, i.e. a subtype of every type, so I would put this vice versa, what's the benefit of complicating the type system by not doing that?
Activity
ilevkivskyi commentedon Oct 6, 2017
This is a special case in mypy. If a function have an empty body then it can have arbitrary return type. This is useful for testing, and is necessary for stubs. We have lots of tests that use this, so we can't disable this in non-stub files. Alternatively we can somehow detect whether we are running tests, and display an error otherwise.
JukkaL commentedon Oct 6, 2017
Mypy should probably eventually enforce this outside stubs. One way would be to make this an option (which doesn't need to be shown in help, perhaps) and turn it on by default, but we'd turn it off for most tests. Detecting stubs is easy, so it won't be a problem. We don't need to actually detect tests if all test runners just explicitly toggle the option.
ilevkivskyi commentedon Oct 6, 2017
@JukkaL
Yes, I was thinking about the same idea.
ilevkivskyi commentedon Oct 7, 2017
See also #2350
Wilfred commentedon Oct 9, 2017
Aha, I didn't realise that mypy treated stubs specially. My intent was to minimise my example, but I initially encountered this issue on a function that calls exit:
I was expecting mypy to give a type error here.
JelleZijlstra commentedon Oct 9, 2017
That's expected: that function basically returns NoReturn, and NoReturn is a subtype of every type.
gvanrossum commentedon Oct 9, 2017
I propose to change the title of this issue to "Get rid of exception for functions with just pass as body" -- or something better (but I can't think of something better). The current title is too wide and in general incorrect.
[-]A function without return or yield should only typecheck for None[/-][+]Stub functions should only typecheck for None[/+]Wilfred commentedon Oct 10, 2017
I've updated the issue title.
Wilfred commentedon Oct 10, 2017
@JelleZijlstra the NoReturn behaviour was surprising to me. Could I open an issue for a
--strictoption that disables this behaviour, or is this working as intended?ilevkivskyi commentedon Oct 10, 2017
This is intended. It is OK for a function to declare a return type wider than actual (
NoReturnin this case).Wilfred commentedon Oct 12, 2017
@JelleZijlstra out of interest, what's the benefit of that? When is
NoReturnbeing a subtype of everything useful?ilevkivskyi commentedon Oct 12, 2017
@Wilfred
NoReturnis the bottom type, i.e. a subtype of every type, so I would put this vice versa, what's the benefit of complicating the type system by not doing that?ilevkivskyi commentedon Oct 12, 2017
@Wilfred To clarify, this was a rhetorical question, this discussion is off-topic for this issue, if you want we can continue it elsewhere.
ilevkivskyi commentedon Dec 2, 2018
I think this is just a duplicate of #2350