-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add flag to disallow partially annotated defs #3744
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
Add flag to disallow partially annotated defs #3744
Conversation
This flag helps user remember to annotate the entire function instead of just parts of it. Fixes python#3742
Perhaps, this flag needs to be enabled by default? That being said a lot of our tests have partially annotated functions, so some changes need to be done there. |
I think there are also a lot of partially annotated functions in typeshed. I'd be open to disallowing them in typeshed though. |
Let's start with the flag off by default. |
docs/source/command_line.rst
Outdated
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 |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, done!
test-data/unit/check-flags.test
Outdated
return m | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testDisallowPartiallyAnnotatedDefs] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This flag helps user remember to annotate the entire function instead of just
parts of it.
Fixes #3742