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

Add autotyping-like return type inference for annotation rules #8643

Merged
merged 3 commits into from
Nov 14, 2023

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Nov 13, 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

@charliermarsh charliermarsh added the fixes Related to suggested fixes for violations label Nov 13, 2023
@charliermarsh charliermarsh force-pushed the charlie/autotyping branch 4 times, most recently from ec39700 to 8ae9236 Compare November 13, 2023 04:58
crates/ruff_python_ast/src/helpers.rs Outdated Show resolved Hide resolved
crates/ruff_python_semantic/src/analyze/type_inference.rs Outdated Show resolved Hide resolved
Comment on lines 79 to 81

// Wrap in a bitwise union (e.g., `int | float`).
Some(union(&names))
Copy link
Member

Choose a reason for hiding this comment

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

nit: not critical but maybe we could update union to take an iterator instead to avoid allocating a vector here

// Wrap in a bitwise union (e.g., `int | float`).
Some(union(&names))
}
ResolvedPythonType::Union(_) => None,
Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine but is there any specific reason we're not implementing the typing.Union variant for pre 3.10?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just complexity and/or laziness.

@charliermarsh charliermarsh force-pushed the charlie/autotyping branch 2 times, most recently from f645062 to aa89dd0 Compare November 13, 2023 17:08
Copy link
Contributor

github-actions bot commented Nov 13, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
@charliermarsh charliermarsh merged commit bf2cc3f into main Nov 14, 2023
16 checks passed
@charliermarsh charliermarsh deleted the charlie/autotyping branch November 14, 2023 04:34
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 this pull request may close these issues.

Rule proposal: Auto-add -> None typehint to functions Suggest type annotations similar to autotyping
2 participants