Commit graph

5431 commits

Author SHA1 Message Date
Dhruv Manilawala
dbd84c947b
Formatter parentheses support for IpyEscapeCommand (#8207)
## Summary

This PR removes the `todo!()` around `IpyEscapeCommand` in the
formatter.

The `NeedsParentheses` trait needs to be implemented which always return
`Never`. The reason being that if an escape command is parenthesized,
then that's not parsed as an escape command. IOW, the parentheses
shouldn't be present around an escape command.

In the similar way, the `CanSkipOptionalParenthesesVisitor` will skip
this node.

## Test Plan

Updated the `unformatted.ipynb` fixture with new cells containing
IPython escape commands and the corresponding snapshot was verified.
Also, tested it out in a few open source repositories containing
notebooks (`openai/openai-cookbook`, `huggingface/notebooks`).

#### New cells in `unformatted.ipynb`

**Cell 2**
```markdown
A markdown cell
```

**Cell 3**
```python
def some_function(foo, bar):
    pass
%matplotlib inline
```

**Cell 4**
```python
foo = %pwd
def some_function(foo,bar,):
	foo = %pwd
    print(foo
	)
```

fixes: #8204
2023-10-25 14:01:50 +00:00
Dhruv Manilawala
c2ec5f0bc9
Use source type to determine parser mode for formatting (#8205)
## Summary

This PR fixes the bug where if a Notebook contained IPython syntax, then
the format command would fail. This was because the correct mode was not
being used while parsing through the formatter code path.

## Test Plan

This PR isn't the only requirement for Notebook formatting to start
working with IPython escape commands. The following PR in the stack is
required as well.
2023-10-25 19:20:02 +05:30
Piotr Dybowski
31032f4f70
Fix skipping formatting examples (#8210) 2023-10-25 11:57:30 +01:00
Otso Velhonoja
f55b724254
Fix misspelled TOML headers in the tutorial (#8209)
## Summary

Fixes misspelled TOML headers in the tutorial regarding the
configuration of the Ruff Linter.
2023-10-25 12:52:42 +02:00
Micha Reiser
fd07a12a52
Refine warning about incompatible isort settings (#8192) 2023-10-25 08:41:17 +01:00
Ran Isenberg
1ee73bdedf
docs: fix name of magic-trailing-comma option in README (#8200)
Co-authored-by: Micha Reiser <micha@reiser.io>
2023-10-25 06:31:43 +00:00
Micha Reiser
23b55aea30
Fix typo in max-doc-length documentation (#8201)
## Summary

Fix typo in `max-doc-length` documentation
2023-10-25 15:26:42 +09:00
Micha Reiser
e36afc3324
Avoid space around pow for None, True and False (#8189) 2023-10-25 07:24:06 +01:00
Spencer Brown
8304c41714
[pylint] Add buffer methods to bad-dunder-method-name (PLW3201) exclusions (#8190)
## Summary

Python 3.12 added the `__buffer__()`/`__release_buffer_()` special
methods, which are incorrectly flagged as invalid dunder methods by
`PLW3201`.

## Test Plan

Added definitions to the test suite, and confirmed they failed without
the fix and are ignored after the fix was done.
2023-10-25 00:03:44 -05:00
Zanie Blue
6f31e9c00e
Match rule prefixes from external codes setting in unused-noqa (#8177)
Supersedes https://github.com/astral-sh/ruff/pull/8176
Closes https://github.com/astral-sh/ruff/pull/8174

## Test plan

Old snapshot contains the new / unmatched `V` code
New snapshot contains no `V` prefixed codes
2023-10-24 22:28:35 +00:00
Luca Mancusi
a6cc56fd98
Fix a wrong setting in configuration.md (#8186)
## Summary

The previous configuration for `ruff` contained an unrecognized field
`magic-trailing-comma` set to "respect". As of version 0.1.2 of `ruff`,
this field was not recognized and resulted in a TOML parse error when
running the `ruff format .` command. This change removes the
`magic-trailing-comma` field and adds the recognized
`skip-magic-trailing-comma` field set to `false`.

## Test Plan

Tested locally with `ruff` 0.1.2.
2023-10-24 17:05:09 -05:00
Charlie Marsh
0236e0751c
Avoid sorting all paths in the format command (#8181)
## Summary

Related to https://github.com/astral-sh/ruff/issues/8135.

If we're not printing a `--diff`, or a summary of `--check` changes, we
can avoid sorting the list of results. Further, when sorting, we only
need to sort a small subset of the entries, in the common case (i.e., in
general, it's much more likely that a file is formatted than not).

## Test Plan

Local benchmarks suggest a 5-10% speedup on the cached behavior:

```
❯ hyperfine --warmup 3 "./target/release/ruff format ../airflow" "./target/release/sort format ../airflow"
Benchmark 1: ./target/release/ruff format ../airflow
  Time (mean ± σ):      70.3 ms ±   5.2 ms    [User: 52.1 ms, System: 59.0 ms]
  Range (min … max):    68.3 ms … 101.7 ms    42 runs

  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet PC without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

Benchmark 2: ./target/release/sort format ../airflow
  Time (mean ± σ):      66.0 ms ±   1.4 ms    [User: 48.3 ms, System: 58.4 ms]
  Range (min … max):    64.7 ms …  71.8 ms    44 runs

  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet PC without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

Summary
  './target/release/sort format ../airflow' ran
    1.07 ± 0.08 times faster than './target/release/ruff format ../airflow'
```
2023-10-24 20:54:06 +00:00
Zanie Blue
2d0769e324
Add external option to unused-noqa documentation (#8171) 2023-10-24 12:38:42 -05:00
Zanie Blue
80473c3f5c
Link to 0.1.2 blog post (#8173) 2023-10-24 12:24:12 -05:00
Zanie Blue
4d7f90e045
Fix link to error supression documentation in unused-noqa (#8172) 2023-10-24 12:23:42 -05:00
Zanie Blue
75bd95e58c
Add note about scope of rule changing in versioning policy (#8169)
Per some previous discussion, the policy is not clear about what happens
if the behavior is similar but the _scope_ in which a rule is applied
changes.
2023-10-24 11:38:31 -05:00
Zanie Blue
3bbdfee69f
Move E721 change to preview section (#8170) 2023-10-24 16:09:37 +00:00
Zanie Blue
3127c79b29
Release 0.1.2 (#8168)
[Rendered
changelog](https://github.com/astral-sh/ruff/blob/release/012/CHANGELOG.md)
2023-10-24 15:21:29 +00:00
Charlie Marsh
c91cc29d6d
Update the formatter README (#8166)
We can decide whether we want to keep this at all, but for now, just
making it consistent with the release.
2023-10-24 14:17:32 +00:00
Micha Reiser
8b665f40c8
Avoid parenthesizing octal/hex or binary literals in object positions (#8160) 2023-10-24 15:12:52 +01:00
Micha Reiser
84979f9673
Rename tab-size to indent-width (#8082)
## Summary

This PR renames the `tab-size` configuration option to `indent-width` to
express that the formatter uses the option to determine the indentation
width AND as tab width.

I first preferred naming the option `tab-width` but then decided to go
with `indent-width` because:

* It aligns with the `indent-style` option
* It would allow us to write a lint rule that asserts that each
indentation uses `indent-width` spaces.

 Closes #7643

## Test Plan

Added integration test
2023-10-24 10:01:24 -04:00
Charlie Marsh
c3dabc1933
Un-hide the ruff format command (#8167) 2023-10-24 09:54:28 -04:00
Dhruv Manilawala
2e81b9c391
Don't move type param opening parenthesis comment (#8163)
## Summary

This PR fixes the issue to avoid collapsing the type param declaration
if
there's a comment after the opening parenthesis. For example,

```python
type foo[  # comment
    A,
    B
] = int
```

Here, we'll preserve the comment on the same line as is being done for
other
similar type of nodes.

## Test Plan

Add a new test case for it, update the snapshots, and validate the
ecosystem
check.

### Formatter ecosystem

#### `main`

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |

#### `dhruv/type-params`

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |

fixes: #8162
2023-10-24 12:02:27 +00:00
Micha Reiser
9feb86caa4
New pycodestyle.max-line-length option (#8039)
## Summary

This PR introduces a new `pycodestyl.max-line-length` option that allows overriding the global `line-length` option for `E501` only.

This is useful when using the formatter and `E501` together, where the formatter uses a lower limit and `E501` is only used to catch extra-long lines. 

Closes #7644

## Considerations

~~Our fix infrastructure asserts in some places that the fix doesn't exceed the configured `line-width`. With this change, the question is whether it should use the `pycodestyle.max-line-width` or `line-width` option to make that decision.
I opted for the global `line-width` for now, considering that it should be the lower limit. However, this constraint isn't enforced and users not using the formatter may only specify `pycodestyle.max-line-width` because they're unaware of the global option (and it solves their need).~~


~~I'm interested to hear your thoughts on whether we should use `pycodestyle.max-line-width` or `line-width` to decide on whether to emit a fix or not.~~

Edit: The linter users `pycodestyle.max-line-width`. The `line-width` option has been removed from the `LinterSettings`

## Test Plan

Added integration test. Built the documentation and verified that the links are correct.
2023-10-24 17:14:05 +09:00
Micha Reiser
2587aef1ea
Add formatter to line-length documentation (#8150) 2023-10-24 07:55:20 +00:00
Charlie Marsh
7f4ea6690d
Remove experimental formatter warning (#8148)
Co-authored-by: Micha Reiser <micha@reiser.io>
2023-10-24 01:26:29 +00:00
Micha Reiser
2f32a57cf4
Remove --line-length option from format command (#8131) 2023-10-24 00:58:01 +01:00
Zanie Blue
802616aac0
Add compatibility test for ruff-lsp to CI (#8016)
Adds a CI job which runs `ruff-lsp` tests against the current Ruff
build.

Avoids rebuilding Ruff at the cost of running _after_ the cargo tests
have finished. Might be worth the rebuild to get earlier feedback but I
don't expect it to fail often?

xref https://github.com/astral-sh/ruff-lsp/pull/286

## Test plan

Verified use of the development version by inspecting version output in
CI; supported by https://github.com/astral-sh/ruff-lsp/pull/289 and
#8034
2023-10-23 15:44:51 -05:00
Weijie Guo
7100e12cc3
add auto-fix for E225,226,227,228 (#8136)
## Summary

Introduce auto fix for `E225`,`E226`,`E227`,`E228`. This partially
address https://github.com/astral-sh/ruff/issues/8121.

## Test Plan

Already covered.
2023-10-23 19:00:42 +00:00
Weijie Guo
5a95b25aa8
add auto-fix for E252 (#8142)
## Summary

Introduce auto fix for `E252`. This partially address #8121.

## Test Plan

Already covered.
2023-10-23 18:57:58 +00:00
Weijie Guo
833814384a
add auto-fix for E275 (#8133)
## Summary

First time contribute to `ruff`, so If there are low-level errors,
please forgive me. 🙇

Introduce auto fix for `E275`, this partially address #8121.

## Test Plan

Already coverd.
2023-10-23 18:57:23 +00:00
Weijie Guo
39e45aa06f
add auto-fix for E273,274 (#8144)
## Summary

Introduce auto fix for `E273` and `E274`. This partially address #8120.

## Test Plan

Already covered.
2023-10-23 16:31:08 +00:00
Weijie Guo
92baa3591d
add auto-fix for E223,224,242 (#8143)
## Summary

Introduce auto fix for `E223`, `E224`, `E242`. This partially address
#8120.

## Test Plan

Already covered.
2023-10-23 16:21:32 +00:00
Simon Boehm
0e96482085
Fix dead link in README (#8146) 2023-10-23 15:56:28 +00:00
Harutaka Kawamura
b9bff6f5d1
[SIM112] Ignore https_proxy, http_proxy, and no_proxy (#8140)
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

Close #8123 

## Test Plan

<!-- How was it tested? -->

New test cases

---------

Signed-off-by: harupy <hkawamura0130@gmail.com>
2023-10-23 08:48:36 -05:00
gouzil
2401e91ab9
[docs] fix extend-unsafe-fixes and extend-safe-fixes example error (#8139) 2023-10-23 11:36:19 +00:00
Micha Reiser
6199590072
Avoid loading files for cached format results (#8134) 2023-10-23 12:29:13 +01:00
Micha Reiser
08519e22e4
Warn about incompatible formatter options (#8088) 2023-10-23 11:04:20 +01:00
dependabot[bot]
c704674190
Bump serde_with from 3.3.0 to 3.4.0 (#8130)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-23 09:05:52 +00:00
dependabot[bot]
2db96067aa
Bump strum_macros from 0.25.2 to 0.25.3 (#8129)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-23 10:04:40 +01:00
dependabot[bot]
8cd09c88d3
Bump codspeed-criterion-compat from 2.2.0 to 2.3.0 (#8128)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-23 10:04:19 +01:00
dependabot[bot]
ce9bd19885
Bump tracing from 0.1.39 to 0.1.40 (#8127)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-23 10:04:08 +01:00
dependabot[bot]
764304faf1
Bump thiserror from 1.0.49 to 1.0.50 (#8126)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-23 10:03:50 +01:00
Micha Reiser
6fc35dd075
Add caching to formatter (#8089) 2023-10-23 09:43:08 +01:00
Harutaka Kawamura
c0710a1dd4
Remove unnecessary mutable variable has_parameters (#8124) 2023-10-23 11:39:33 +05:30
Micha Reiser
2c2ebf952a
Rust 1.73 (#8007) 2023-10-23 02:12:25 +00:00
Charlie Marsh
d6a4283003
Fix range of unparenthesized tuple subject in match statement (#8101)
## Summary

This was just a bug in the parser ranges, probably since it was
initially implemented. Given `match n % 3, n % 5: ...`, the "subject"
(i.e., the tuple of two binary operators) was using the entire range of
the `match` statement.

Closes https://github.com/astral-sh/ruff/issues/8091.

## Test Plan

`cargo test`
2023-10-22 19:58:33 -04:00
Charlie Marsh
95702e408f
Respect parenthesized generators in has_own_parentheses (#8100)
## Summary

When analyzing:

```python
if "root" not in (
    long_tree_name_tree.split("/")[0]
    for long_tree_name_tree in really_really_long_variable_name
):
    msg = "Could not find root. Please try a different forest."
    raise ValueError(msg)
```

We missed that the generator expression is parenthesized, because the
parentheses are _part_ of the generator -- so
`is_expression_parenthesized` returns `False`. We needed to take into
account that generators and tuples may or may not be parenthesized when
determining whether we can omit parentheses while splitting an
expression.

Closes https://github.com/astral-sh/ruff/issues/8090.

## Test Plan

No changes in similarity.

Before:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |

After:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |
2023-10-22 19:58:25 -04:00
Harutaka Kawamura
bcaac9693b
Refactor get_mark_decorators to return a marker name (#8116) 2023-10-22 10:03:36 -04:00
Harutaka Kawamura
d6f59e4131
Fix typo (pytext -> pytest) (#8117) 2023-10-22 10:02:22 -04:00