KEMBAR78
Add flag to disallow partially annotated defs by ilinum · Pull Request #3744 · python/mypy · GitHub
Skip to content

Conversation

ilinum
Copy link
Collaborator

@ilinum ilinum commented Jul 20, 2017

This flag helps user remember to annotate the entire function instead of just
parts of it.
Fixes #3742

This flag helps user remember to annotate the entire function instead of just
parts of it.
Fixes python#3742
@ilinum
Copy link
Collaborator Author

ilinum commented Jul 20, 2017

Perhaps, this flag needs to be enabled by default?
No functions in mypy/mypy are partially annotated and running mypy with the new flag on Dropbox internal codebases only produced a few extra errors.

That being said a lot of our tests have partially annotated functions, so some changes need to be done there.

@JelleZijlstra
Copy link
Member

I think there are also a lot of partially annotated functions in typeshed. I'd be open to disallowing them in typeshed though.

@ddfisher
Copy link
Collaborator

Let's start with the flag off by default.

annotations are not type checked.) It will assume all arguments
have type ``Any`` and always infer ``Any`` as the return type.

- ``--disallow-incompletely-annotated-defs`` reports an error whenever it
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit verbose. What about something like --disallow-incomplete-defs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I agree! Flag is --disallow-incomplete-defs now.
I opted for the longer version originally because I wanted to avoid slight ambiguity in flag name.

It's a tradeoff but I think shorter name is better.

mypy/checker.py Outdated
return isinstance(t, AnyType) and t.implicit

if isinstance(fdef.type, CallableType):
has_an_explicit_annotation = any(not is_implicit_any(t)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The an here is unnecessary.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you could simplify this if statement away into a clearer one(ish)-liner like this:

has_explicit_annotation = isinstance(fdef.type, CallableType) and any(not is_implicit_any(t) for t in fdef.type.arg_types + [fdef.type.ret_type])

(but with better linewrapping and indentation)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, done!

return m
[builtins fixtures/dict.pyi]

[case testDisallowPartiallyAnnotatedDefs]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a test case for a function with one annotated argument, one unannotated argument, and no return type annotation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Collaborator

@ddfisher ddfisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ddfisher ddfisher merged commit 1dc3f73 into python:master Aug 2, 2017
@ilinum ilinum deleted the disallow-partially-annotated-defs branch August 8, 2017 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants