Skip to content

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
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
@github-actions
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.

@charliermarsh charliermarsh merged commit bf2cc3f into main Nov 14, 2023
@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

3 participants