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

Abstract stylist to libcst style conversion #4749

Merged
merged 4 commits into from
Jun 5, 2023
Merged

Abstract stylist to libcst style conversion #4749

merged 4 commits into from
Jun 5, 2023

Conversation

konstin
Copy link
Member

@konstin konstin commented May 31, 2023

Replace all duplicate invocations of

let mut state = CodegenState {
    default_newline: &stylist.line_ending(),
    default_indent: stylist.indentation(),
    ..CodegenState::default()
}
tree.codegen(&mut state);
state.to_string()

with

tree.codegen_stylist(&stylist)

No functional changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Looks good to me. I don't know if it was intentional that the ast crate doesn't depend on the cst crate.

@github-actions
Copy link
Contributor

github-actions bot commented May 31, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
linter/all-rules/large/dataset.py          1.00     14.6±0.17ms     2.8 MB/sec    1.01     14.7±0.12ms     2.8 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.6±0.03ms     4.7 MB/sec    1.00      3.6±0.02ms     4.7 MB/sec
linter/all-rules/numpy/globals.py          1.00    360.2±0.85µs     8.2 MB/sec    1.01    363.2±3.40µs     8.1 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.1±0.10ms     4.2 MB/sec    1.02      6.2±0.09ms     4.1 MB/sec
linter/default-rules/large/dataset.py      1.00      7.3±0.04ms     5.6 MB/sec    1.01      7.4±0.02ms     5.5 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1530.0±1.85µs    10.9 MB/sec    1.00   1534.9±3.45µs    10.8 MB/sec
linter/default-rules/numpy/globals.py      1.00    164.5±0.59µs    17.9 MB/sec    1.00    164.9±1.70µs    17.9 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.3±0.01ms     7.8 MB/sec    1.01      3.3±0.00ms     7.7 MB/sec
parser/large/dataset.py                    1.06      6.2±0.01ms     6.6 MB/sec    1.00      5.8±0.01ms     7.0 MB/sec
parser/numpy/ctypeslib.py                  1.05  1196.8±26.64µs    13.9 MB/sec    1.00   1134.7±0.72µs    14.7 MB/sec
parser/numpy/globals.py                    1.04    120.9±0.26µs    24.4 MB/sec    1.00    116.3±0.53µs    25.4 MB/sec
parser/pydantic/types.py                   1.05      2.6±0.00ms     9.7 MB/sec    1.00      2.5±0.00ms    10.2 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
linter/all-rules/large/dataset.py          1.00     17.1±0.33ms     2.4 MB/sec    1.00     17.0±0.28ms     2.4 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.01      4.2±0.06ms     3.9 MB/sec    1.00      4.2±0.05ms     4.0 MB/sec
linter/all-rules/numpy/globals.py          1.01   495.9±14.60µs     6.0 MB/sec    1.00    489.7±6.74µs     6.0 MB/sec
linter/all-rules/pydantic/types.py         1.01      7.1±0.10ms     3.6 MB/sec    1.00      7.1±0.11ms     3.6 MB/sec
linter/default-rules/large/dataset.py      1.01      8.4±0.12ms     4.8 MB/sec    1.00      8.3±0.11ms     4.9 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1760.5±27.14µs     9.5 MB/sec    1.00  1767.2±20.64µs     9.4 MB/sec
linter/default-rules/numpy/globals.py      1.00    198.4±3.84µs    14.9 MB/sec    1.00    198.5±5.82µs    14.9 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.8±0.05ms     6.7 MB/sec    1.00      3.8±0.20ms     6.8 MB/sec
parser/large/dataset.py                    1.00      6.5±0.05ms     6.2 MB/sec    1.16      7.6±0.11ms     5.4 MB/sec
parser/numpy/ctypeslib.py                  1.00  1235.6±13.08µs    13.5 MB/sec    1.13  1399.6±23.15µs    11.9 MB/sec
parser/numpy/globals.py                    1.00    126.5±2.13µs    23.3 MB/sec    1.11    139.9±6.79µs    21.1 MB/sec
parser/pydantic/types.py                   1.00      2.8±0.03ms     9.1 MB/sec    1.13      3.2±0.04ms     8.1 MB/sec

@konstin
Copy link
Member Author

konstin commented May 31, 2023

I don't know if it was intentional that the ast crate doesn't depend on the cst crate.

i already though about making this a free standing function in the ruff crate otherwise, @charliermarsh any preferences?

@konstin konstin requested a review from charliermarsh May 31, 2023 09:57
@charliermarsh
Copy link
Member

It wasn't intentional but it is a heavy dependency. Should we make it a feature?

konstin added a commit that referenced this pull request Jun 1, 2023
Follow-up/alternative to #4749 we can either merge into it or discard.
@konstin
Copy link
Member Author

konstin commented Jun 1, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@konstin
Copy link
Member Author

konstin commented Jun 1, 2023

To make this less hypothetical, this would be the alternative without the libcst dep: #4777. If you want that version, you can merge it into this PR, if you don't want it you can close #4777 and merge this PR instead.

/// Given an import statement, remove any imports that are specified in the `imports` iterator.
///
/// Returns `Ok(None)` if the statement is empty after removing the imports.
pub(crate) fn remove_imports<'a>(
Copy link
Member

Choose a reason for hiding this comment

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

Does this just need a rebase? Why was this deleted?

Copy link
Member Author

Choose a reason for hiding this comment

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

i think the rebase went wrong, i have to fix that

@konstin
Copy link
Member Author

konstin commented Jun 2, 2023

Found a better solution that abstracts away more code

@konstin konstin requested a review from MichaReiser June 2, 2023 14:48
@charliermarsh
Copy link
Member

Looks reasonable to me.

@@ -11,6 +11,23 @@ use ruff_python_ast::source_code::{Locator, Stylist};
use crate::cst::helpers::compose_module_path;
use crate::cst::matchers::match_statement;

/// Glue code to make libcst codegen work with ruff's Stylist
pub(crate) trait CodegenStylist<'a>: Codegen<'a> {
fn codegen_stylist(&self, stylist: &'a Stylist<'a>) -> String;
Copy link
Member

@MichaReiser MichaReiser Jun 5, 2023

Choose a reason for hiding this comment

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

This seems to be sufficient

Suggested change
fn codegen_stylist(&self, stylist: &'a Stylist<'a>) -> String;
fn codegen_stylist(&self, stylist: &'a Stylist) -> String;

konstin added 4 commits June 5, 2023 09:11
Replace all duplicate invocations of

```rust
let mut state = CodegenState {
    default_newline: &stylist.line_ending(),
    default_indent: stylist.indentation(),
    ..CodegenState::default()
}
tree.codegen(&mut state);
state.to_string()
```

with

```rust
tree.codegen_stylist(&stylist);
```

No functional changes.
@konstin konstin enabled auto-merge (squash) June 5, 2023 07:17
@konstin konstin merged commit 576e0c7 into main Jun 5, 2023
@konstin konstin deleted the codegen_state branch June 5, 2023 07:22
konstin added a commit that referenced this pull request Jun 13, 2023
* Abstract codegen with stylist into a CodegenStylist trait

Replace all duplicate invocations of

```rust
let mut state = CodegenState {
    default_newline: &stylist.line_ending(),
    default_indent: stylist.indentation(),
    ..CodegenState::default()
}
tree.codegen(&mut state);
state.to_string()
```

with

```rust
tree.codegen_stylist(&stylist);
```

No functional changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants