Skip to content
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

Rule proposal: Auto-add -> None typehint to functions #8213

Closed
intgr opened this issue Oct 25, 2023 · 2 comments · Fixed by #8643
Closed

Rule proposal: Auto-add -> None typehint to functions #8213

intgr opened this issue Oct 25, 2023 · 2 comments · Fixed by #8643
Labels
fixes Related to suggested fixes for violations

Comments

@intgr
Copy link
Contributor

intgr commented Oct 25, 2023

I want to create a new Ruff rule that auto-adds -> None return type hint to functions when no return <value> statements are present.

Clearly there are lots of special cases that need to be handled (yield, raise, @overload, .pyi files, etc).

What do you think?

I pushed a very eary draft #8212, but no point doing thorough review yet.

@intgr
Copy link
Contributor Author

intgr commented Oct 25, 2023

This autofix could potentially be annoying for peope who run Ruff after every save/auto-save in their IDE. Because Ruff might add -> None to a partially written function when the user isn't finished writing a return statement. Is this something I should consider?

Also, maybe it makes sense to allocate a specific range of RUF codes for typehinting-related rules? Or create another rule prefix entirely?

@henryiii
Copy link
Contributor

Also, remember that -> None isn't required on __init__ functions unless there are no typed arguments.

@charliermarsh charliermarsh added the fixes Related to suggested fixes for violations label Oct 26, 2023
charliermarsh added a commit that referenced this issue Nov 14, 2023
## Summary

This PR adds (unsafe) fixes to the flake8-annotations rules that enforce
missing return types, offering to automatically insert type annotations
for functions with literal return values. The logic is smart enough to
generate simplified unions (e.g., `float` instead of `int | float`) and
deal with implicit returns (`return` without a value).

Closes #1640 (though we could
open a separate issue for referring parameter types).

Closes #8213.

## Test Plan

`cargo test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants