Skip to content

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

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

Open
JukkaL opened this issue Apr 25, 2023 · 7 comments · May be fixed by #15160
Open

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

JukkaL opened this issue Apr 25, 2023 · 7 comments · May be fixed by #15160

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 25, 2023

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.

@hauntsaninja
Copy link
Collaborator

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

@Aditya-Sakpal
Copy link

Is this issue already been resolved?

@AVUKU-PRAGATHESWARI
Copy link

Is this issue solved..?

joaopedro-s added a commit to joaopedro-s/mypy that referenced this issue Jul 1, 2023
Adding optional error flag for missing return type, when a function has at least one typed argument. This will address issue python#15127

Co-Authored-By: Leonardo Abreu Santos <[email protected]>
@Cnoor0171
Copy link

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

@timothy-choi
Copy link

timothy-choi commented May 3, 2025

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

@charulatalodha
Copy link
Contributor

charulatalodha commented May 20, 2025

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants