Skip to content

Add a rule to detect string members in runtime-evaluated unions#9143

Merged
charliermarsh merged 1 commit intomainfrom
charlie/type
Dec 16, 2023
Merged

Add a rule to detect string members in runtime-evaluated unions#9143
charliermarsh merged 1 commit intomainfrom
charlie/type

Conversation

@charliermarsh
Copy link
Copy Markdown
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
@github-actions
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes!

Comment thread crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006.py Outdated
@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
@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