Optional error code to flag missing return type when an argument has an annotation
It's pretty common to see functions like this:
def foo(x: str):
...
return <something>
Note that the function foo has an implicit Any return type, which is likely not what the programmer intended.
Add a new error code (disabled by default since this is a backward compatibility break) that generates an error about a missing return type if at least one argument has an explicit type annotation. The error could be something like 'Function "foo" has no return type annotation', and perhaps add a note suggesting the use of -> None if the function never returns a value.
We might eventually enable the error code by default as part of a major mypy release, such as 2.0 or 3.0.
Look at the code paths for --disallow-incomplete-defs
Is this issue already been resolved?
Is this issue solved..?
@JukkaL Hi. Isn't this already the way mypy behaves when the flag --disallow-incomplete-defs is used? For example, if I run mypy --disallow-incomplete-defs example.py on this file
# example.py
def foo(x: str):
return 123
I get
example.py:2: error: Function is missing a return type annotation [no-untyped-def]
Found 1 error in 1 file (checked 1 source file)
Is this issue solved? If not, can I work on it?
disallow-incomplete-defs
Example 1: No Annotation --> No error
mypy --disallow-incomplete-defs -c 'def f(x,): pass'
Success: no issues found in 1 source file
Example 2: Atleast 1 arg has annotation, but is missing return type annotation --> mypy throw errors for return type but doesnt suggest the use of -> None if the function never returns a value which would be nice to have
mypy --disallow-incomplete-defs -c 'def f(a:int, b): pass'
<string>:1: error: Function is missing a return type annotation [no-untyped-def]
<string>:1: error: Function is missing a type annotation for one or more arguments [no-untyped-def]
disallow-untyped-defs
Example 1: if we have no annotations, it suggests user the use of -> None
mypy --disallow-untyped-defs -c 'def f(x,): pass'
<string>:1: error: Function is missing a return type annotation [no-untyped-def]
<string>:1: note: Use "-> None" if function does not return a value
Example 2: if we have annotation for atleast 1 argument , it DOES NOT suggests user the use of -> None
mypy --disallow-untyped-defs -c 'def f(a:int, b): pass'
<string>:1: error: Function is missing a return type annotation [no-untyped-def]
<string>:1: error: Function is missing a type annotation for one or more arguments [no-untyped-def]
So, this behavior needs can be made consistent by adding a note suggesting the use of -> None if the function never returns a value for cases when atleast one arg in the function is annotated.
I am working on this as part of PyCon US 2025 Sprints
is this issue resolved?