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

Implement iter(), len() and is_empty() for all display-literal AST nodes #12807

Merged
merged 1 commit into from
Aug 12, 2024

Conversation

AlexWaygood
Copy link
Member

Summary

This PR adds iter(), len() and is_empty() methods to all the AST nodes representing "display literals":

  • ExprList
  • ExprSet
  • ExprTuple
  • ExprDict

Each of these nodes wraps an inner Vec of elements; it's rare to use these nodes in linter rules without either iterating over the elements or querying the number of elements. Exposing these methods on the outer nodes makes a lot of our code simpler and more elegant.

Test Plan

cargo test

@AlexWaygood AlexWaygood added the internal An internal refactor or improvement label Aug 11, 2024
Copy link

codspeed-hq bot commented Aug 11, 2024

CodSpeed Performance Report

Merging #12807 will not alter performance

Comparing AlexWaygood:alex/display-len (23b601e) with main (a99a458)

Summary

✅ 32 untouched benchmarks

Copy link
Contributor

github-actions bot commented Aug 11, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

ℹ️ ecosystem check encountered format errors. (no format changes; 1 project error)

openai/openai-cookbook (error)

warning: Detected debug build without --no-cache.
error: Failed to parse examples/Chat_finetuning_data_prep.ipynb:6:18:25: Unparenthesized generator expression cannot be used here
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_box.ipynb:13:1:1: Expected an expression
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_confluence.ipynb:15:1:5: Simple statements must be separated by newlines or semicolons
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_gmail.ipynb:15:1:1: Expected an expression
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_jira.ipynb:15:1:1: Expected an expression
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_notion.ipynb:15:1:1: Expected an expression
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_sharepoint_doc.ipynb:28:1:5: Simple statements must be separated by newlines or semicolons
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_sharepoint_text.ipynb:28:1:5: Simple statements must be separated by newlines or semicolons
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_sql_database.ipynb:2:2:5: Simple statements must be separated by newlines or semicolons
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_middleware_azure_function.ipynb:37:1:13: Simple statements must be separated by newlines or semicolons

Formatter (preview)

ℹ️ ecosystem check encountered format errors. (no format changes; 1 project error)

openai/openai-cookbook (error)

ruff format --preview

warning: Detected debug build without --no-cache.
error: Failed to parse examples/Chat_finetuning_data_prep.ipynb:6:18:25: Unparenthesized generator expression cannot be used here
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_box.ipynb:13:1:1: Expected an expression
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_confluence.ipynb:15:1:5: Simple statements must be separated by newlines or semicolons
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_gmail.ipynb:15:1:1: Expected an expression
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_jira.ipynb:15:1:1: Expected an expression
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_notion.ipynb:15:1:1: Expected an expression
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_sharepoint_doc.ipynb:28:1:5: Simple statements must be separated by newlines or semicolons
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_sharepoint_text.ipynb:28:1:5: Simple statements must be separated by newlines or semicolons
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_action_sql_database.ipynb:2:2:5: Simple statements must be separated by newlines or semicolons
error: Failed to parse examples/chatgpt/gpt_actions_library/gpt_middleware_azure_function.ipynb:37:1:13: Simple statements must be separated by newlines or semicolons

@dhruvmanila
Copy link
Member

Each of these nodes wraps an inner Vec of elements; it's rare to use these nodes in linter rules without either iterating over the elements or querying the number of elements. Exposing these methods on the outer nodes makes a lot of our code simpler and more elegant.

If it makes sense, we could just implement Deref for these nodes that returns the inner Vec which would automatically expose all these methods (and other methods).

crates/ruff_linter/src/fix/edits.rs Outdated Show resolved Hide resolved
@AlexWaygood
Copy link
Member Author

If it makes sense, we could just implement Deref for these nodes that returns the inner Vec which would automatically expose all these methods (and other methods).

Yeah, it's an interesting idea. I'm not sure if methods like dict.first() and dict.last() make as much sense as being able to iterate over the ExprDict node and directly query its length, though. I think I prefer to only implement a selection of methods that we know make sense.

I considered creating a DisplayLiteral trait for all of these nodes to implement, but in the end I concluded that it just created more complexity and didn't really simplify anything

@AlexWaygood AlexWaygood enabled auto-merge (squash) August 12, 2024 10:36
@AlexWaygood AlexWaygood merged commit aa0db33 into astral-sh:main Aug 12, 2024
17 checks passed
@AlexWaygood AlexWaygood deleted the alex/display-len branch August 12, 2024 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants