Skip to content

Require annotations for all nested functions within an annotated function #1324

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 4, 2016 · 4 comments
Open

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 4, 2016

Mypy currently treats an unannotated function within an annotated function as being dynamically typed, similar to all unannotated functions. However, this can be unexpected, and it's easy to miss nested functions when annotating code because they aren't visually prominent. Perhaps mypy should enforce that nested function are annotated within an annotated function?

Once we have strict mode, it could be used to enforce this as well, but it might be useful have a separate check for this.

(This was suggested by Alex Allain.)

@gvanrossum gvanrossum added this to the Undetermined priority milestone Apr 7, 2016
@ddfisher
Copy link
Collaborator

ddfisher commented Apr 7, 2016

This seems like the kind of thing that might be better disallowed with --disallow-untyped-defs to me.

@gvanrossum
Copy link
Member

gvanrossum commented Apr 7, 2016 via email

@gnprice
Copy link
Collaborator

gnprice commented Jun 10, 2016

@danielrh ran into this the other day -- he thought he'd put types on everything in a file, and was surprised when mypy didn't catch a type error he'd made. It turned out that the line of code that had the error was inside a nested function which hadn't been annotated.

One way to prevent that would have been to be able to apply --disallow-untyped-defs to the file (a feature discussed in #1235.) But the same confusion could easily have happened in a context where only some of the functions in the file were annotated (particularly if it's a new or newly-annotated function in a large existing file), or where the user hasn't thought to turn on --disallow-untyped-defs. So I think it would be useful to prevent this especially-subtle scenario in a universal way.

This could be an option at first (perhaps even already on by default?) to facilitate migration, but I suspect people will generally be glad to just always do it and it should ultimately be the only way.

@gvanrossum gvanrossum removed this from the Undetermined priority milestone Mar 29, 2017
@JukkaL
Copy link
Collaborator Author

JukkaL commented Feb 8, 2018

Increasing priority since this is something that I encounter every now and then.

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

No branches or pull requests

4 participants