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

Formatter: Add SourceType to context to enable special formatting for stub files #6331

Merged
merged 5 commits into from Aug 4, 2023

Conversation

konstin
Copy link
Member

@konstin konstin commented Aug 4, 2023

Summary This adds the information whether we're in a .py python source file or in a .pyi stub file to enable people working on #5822 and related issues.

I'm not completely happy with Default for something that depends on the input.

Test Plan None, this is currently unused, i'm leaving this to first implementation of stub file specific formatting.

@konstin
Copy link
Member Author

konstin commented Aug 4, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

Comment on lines 6 to 29
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SourceType {
/// A `.py` file
Py,
/// A `.pyi` file
Pyi,
}

impl SourceType {
pub fn from_path(path: &Path) -> Option<Self> {
match path.extension().and_then(OsStr::to_str) {
Some("py") => Some(Self::Py),
Some("pyi") => Some(Self::Pyi),
_ => None,
}
}
}

impl Default for SourceType {
fn default() -> Self {
Self::Py
}
}
Copy link
Member

Choose a reason for hiding this comment

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

#5552 introduces a new PySourceType. What do you think of moving PySourceType to ruff_python_ast and re-using it here?

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 was searching for this and couldn't find it, thanks

crates/ruff_python_formatter/src/options.rs Show resolved Hide resolved
@MichaReiser MichaReiser added the formatter Related to the formatter label Aug 4, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                                      main                                    pr
-----                                      ----                                    --
formatter/large/dataset.py                 1.00      9.7±0.28ms     4.2 MB/sec     1.03      9.9±0.33ms     4.1 MB/sec
formatter/numpy/ctypeslib.py               1.00  1888.7±56.00µs     8.8 MB/sec     1.03  1951.1±96.49µs     8.5 MB/sec
formatter/numpy/globals.py                 1.00   215.7±11.17µs    13.7 MB/sec     1.03   223.1±13.87µs    13.2 MB/sec
formatter/pydantic/types.py                1.00      4.1±0.14ms     6.2 MB/sec     1.02      4.2±0.14ms     6.1 MB/sec
linter/all-rules/large/dataset.py          1.00     13.7±0.36ms     3.0 MB/sec     1.00     13.7±0.36ms     3.0 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.01      3.4±0.09ms     4.9 MB/sec     1.00      3.4±0.11ms     4.9 MB/sec
linter/all-rules/numpy/globals.py          1.00   483.2±16.57µs     6.1 MB/sec     1.00   481.3±13.50µs     6.1 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.3±0.20ms     4.1 MB/sec     1.00      6.3±0.43ms     4.1 MB/sec
linter/default-rules/large/dataset.py      1.01      6.6±0.18ms     6.2 MB/sec     1.00      6.5±0.18ms     6.2 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.01  1409.8±119.36µs    11.8 MB/sec    1.00  1391.9±39.96µs    12.0 MB/sec
linter/default-rules/numpy/globals.py      1.00    164.3±6.93µs    18.0 MB/sec     1.01    165.4±7.41µs    17.8 MB/sec
linter/default-rules/pydantic/types.py     1.01      2.9±0.09ms     8.7 MB/sec     1.00      2.9±0.08ms     8.8 MB/sec

Windows

group                                      main                                    pr
-----                                      ----                                    --
formatter/large/dataset.py                 1.00     10.4±0.69ms     3.9 MB/sec     1.03     10.7±0.59ms     3.8 MB/sec
formatter/numpy/ctypeslib.py               1.00      2.1±0.13ms     8.0 MB/sec     1.02      2.1±0.15ms     7.9 MB/sec
formatter/numpy/globals.py                 1.00   223.1±14.91µs    13.2 MB/sec     1.07   239.0±16.58µs    12.3 MB/sec
formatter/pydantic/types.py                1.00      4.5±0.31ms     5.7 MB/sec     1.01      4.5±0.28ms     5.6 MB/sec
linter/all-rules/large/dataset.py          1.00     14.4±0.55ms     2.8 MB/sec     1.03     14.8±0.58ms     2.7 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.9±0.18ms     4.3 MB/sec     1.02      4.0±0.19ms     4.2 MB/sec
linter/all-rules/numpy/globals.py          1.00   492.5±30.87µs     6.0 MB/sec     1.03   509.3±28.10µs     5.8 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.8±0.42ms     3.8 MB/sec     1.01      6.9±0.29ms     3.7 MB/sec
linter/default-rules/large/dataset.py      1.00      7.4±0.32ms     5.5 MB/sec     1.03      7.6±0.32ms     5.4 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1577.6±102.55µs    10.6 MB/sec    1.00  1585.4±110.73µs    10.5 MB/sec
linter/default-rules/numpy/globals.py      1.00   188.5±10.98µs    15.7 MB/sec     1.01   189.9±10.64µs    15.5 MB/sec
linter/default-rules/pydantic/types.py     1.02      3.3±0.13ms     7.6 MB/sec     1.00      3.3±0.16ms     7.8 MB/sec

Base automatically changed from Replace_with_ to main August 4, 2023 08:49
@konstin
Copy link
Member Author

konstin commented Aug 4, 2023

@dhruvmanila I've copied most of PySourceType from #5552 to ruff_python_ast, i can rebase #5552 on top of this once merged if you want.

@konstin konstin changed the title Formatter: Add SourceType to context Formatter: Add SourceType to context to enable special formatting for stub files Aug 4, 2023
@konstin konstin enabled auto-merge (squash) August 4, 2023 11:46
@konstin konstin merged commit 1031bb6 into main Aug 4, 2023
16 checks passed
@konstin konstin deleted the add-source-type-option branch August 4, 2023 11:52
@dhruvmanila
Copy link
Member

@dhruvmanila I've copied most of PySourceType from #5552 to ruff_python_ast, i can rebase #5552 on top of this once merged if you want.

Hey, thanks for informing. I'll rebase it, no problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants