mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Optional error code to flag missing return type when an argument has an annotation

Open JukkaL opened this issue 3 years ago • 8 comments

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.

JukkaL avatar Apr 25 '23 15:04 JukkaL

Look at the code paths for --disallow-incomplete-defs

hauntsaninja avatar Apr 25 '23 16:04 hauntsaninja

Is this issue already been resolved?

Aditya-Sakpal avatar May 08 '23 20:05 Aditya-Sakpal

Is this issue solved..?

AVUKU-PRAGATHESWARI avatar May 16 '23 14:05 AVUKU-PRAGATHESWARI

@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)

Cnoor0171 avatar Dec 05 '23 18:12 Cnoor0171

Is this issue solved? If not, can I work on it?

timothy-choi avatar May 03 '25 19:05 timothy-choi

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.

charulatalodha avatar May 20 '25 13:05 charulatalodha

I am working on this as part of PyCon US 2025 Sprints

charulatalodha avatar May 20 '25 14:05 charulatalodha

is this issue resolved?

alansajith avatar Feb 03 '26 11:02 alansajith