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 a rule to detect string members in runtime-evaluated unions #9143

Merged
merged 1 commit into from
Dec 16, 2023

Conversation

charliermarsh
Copy link
Member

Summary

A common mistake is to add quotes around one member in an X | Y-style type union, as in:

contract_versions_list: list[ContractVersion] | 'QuerySet[ContractVersion]' | None = None

However, doing so will lead to a runtime error if the annotation is runtime-evaluated. This PR lints against such patterns.

Closes #9139.

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule preview Related to preview mode features labels Dec 15, 2023
Copy link
Contributor

github-actions bot commented Dec 15, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Comment on lines +29 to +32
/// Use instead:
/// ```python
/// var: str | int
/// ```
Copy link
Member

Choose a reason for hiding this comment

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

Would var: "str | int" be valid too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes!

@charliermarsh charliermarsh force-pushed the charlie/type branch 2 times, most recently from 4a9499c to 4eeaec2 Compare December 16, 2023 21:16
@charliermarsh charliermarsh enabled auto-merge (squash) December 16, 2023 21:16
@charliermarsh charliermarsh merged commit a336c1b into main Dec 16, 2023
16 checks passed
@charliermarsh charliermarsh deleted the charlie/type branch December 16, 2023 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint when | annotations contain string segments
3 participants