Commit graph

5987 commits

Author SHA1 Message Date
Dimitri Papadopoulos Orfanos
d04d49cc0e
Fix typos found by codespell (#9346)
## Summary

Fix typos found by
[codespell](https://github.com/codespell-project/codespell).

## Test Plan

CI tests.
2024-01-02 02:08:15 +00:00
dependabot[bot]
cd0493db2a
Bump result-like from 0.4.6 to 0.5.0 (#9343) 2024-01-02 02:06:17 +00:00
Mikael Arguedas
d0a1e201a3
[S507] fix doc recommended fix (#9347)
paramiko `set_missing_host_key_policy` has mandatory positional arg.
The [current
documentation](https://docs.astral.sh/ruff/rules/ssh-no-host-key-verification/#example)
leads to non-running code

```
>>> from paramiko import client
>>> ssh_client = client.SSHClient()
>>> ssh_client.set_missing_host_key_policy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: SSHClient.set_missing_host_key_policy() missing 1 required positional argument: 'policy'
```

This PR modifies the documentation to set the policy to the [default
`RejectPolicy`](https://docs.paramiko.org/en/latest/api/client.html#paramiko.client.SSHClient.set_missing_host_key_policy)

Signed-off-by: Mikael Arguedas <mikael.arguedas@gmail.com>
2024-01-01 20:59:11 -05:00
Dimitri Papadopoulos Orfanos
0a0020583f
Short rule messages should not end with a period (#9345)
## Summary

Remove the period from a couple short messages, for consistency with all
other short messages.

All other short rule messages lack such a period, except for long
messages made of multiple sentences.

## Test Plan

Tests modified accordingly.

Not sure if this would qualify as a breaking change because user-visible
messages are modified.
2024-01-01 20:58:48 -05:00
dependabot[bot]
6821b81d06
Bump proc-macro2 from 1.0.71 to 1.0.73 (#9344) 2024-01-01 20:57:49 -05:00
dependabot[bot]
b3087849ee
Bump clap from 4.4.7 to 4.4.12 (#9342) 2024-01-01 20:57:41 -05:00
dependabot[bot]
0c52710da5
Bump is-macro from 0.3.1 to 0.3.4 (#9341) 2024-01-01 20:57:36 -05:00
dependabot[bot]
a96c8db95c
Bump serde_json from 1.0.108 to 1.0.109 (#9340) 2024-01-01 20:57:28 -05:00
Alex Waygood
15f6213cb0
[flake8-pyi] Implement PYI058 (#9313)
## Summary

This PR implements Y058 from flake8-pyi -- this is a new flake8-pyi rule
that was released as part of `flake8-pyi 23.11.0`. I've followed the
Python implementation as closely as possible (see
858c0918a8),
except that for the Ruff version, the rule also applies to `.py` files
as well as for `.pyi` files. (For `.py` files, we only emit the
diagnostic in very specific situations, however, as there's a much
higher likelihood of emitting false positives when applying this rule to
a `.py` file.)

## Test Plan

`cargo test`/`cargo insta review`
2024-01-01 07:28:35 -04:00
Charlie Marsh
1f9353fed3
Respect __str__ definitions from super classes (#9338)
Closes https://github.com/astral-sh/ruff/issues/9242.
2023-12-31 22:25:08 +00:00
Charlie Marsh
cea2ec8dd0
Use a sorted vector for block comments (#9337)
## Summary

I realized that we can avoid allocating a hash map here.
2023-12-31 19:52:40 +00:00
Charlie Marsh
686abbc97a
Use match_typing_call_path in more sites (#9336)
Preferable as it avoids multiple `resolve_call_path` calls internally.
2023-12-31 19:47:01 +00:00
Charlie Marsh
195f7c097a
Treat all typing_extensions members as typing aliases (#9335)
## Summary

Historically, we encoded this list by extracting the `__all__`. I went
to update it, but... is there really any value in it? Seems easier to
just treat `typing_extensions` as an alias for `typing`.

Closes https://github.com/astral-sh/ruff/issues/9334.
2023-12-31 14:23:33 -04:00
Charlie Marsh
772e5d587d
Remove flake8-to-ruff (#9329)
## Summary

We stopped releasing this a while ago and no longer advertise it
anywhere. IMO, we should remove it so that we stop paying the cost of
maintaining it. If we want to revive it, we can always do so from Git.
2023-12-31 11:09:56 -05:00
Charlie Marsh
da8a3af524
Make DisplayParseError an error type (#9325)
## Summary

This is a non-behavior-changing refactor to follow-up
https://github.com/astral-sh/ruff/pull/9321 by modifying
`DisplayParseError` to use owned data and make it useable as a
standalone error type (rather than using references and implementing
`Display`). I don't feel very strongly either way. I thought it was
awkward that the `FormatCommandError` had two branches in the display
path, and wanted to represent the `Parse` vs. other cases as a separate
enum, so here we are.
2023-12-31 15:46:29 +00:00
Charlie Marsh
b3789cd9e9
Fix continuation detection following multi-line strings (#9332)
## Summary

The logic that detects continuations assumed that tokens themselves
cannot span multiple lines. However, strings _can_ -- even single-quoted
strings.

Closes https://github.com/astral-sh/ruff/issues/9323.
2023-12-31 10:43:35 -05:00
Charlie Marsh
003851c41d
Avoid panic when comment is preceded by Unicode (#9331)
Closes https://github.com/astral-sh/ruff/issues/9328.
2023-12-31 12:54:31 +00:00
Charlie Marsh
1c9268d2ff
Remove some unused dependencies (#9330) 2023-12-31 07:38:16 -05:00
Ofek Lev
158367bf91
Improve responsiveness when invoked via Python (#9315)
This saves a handful of milliseconds on Windows and even more on other
platforms when running `python -m ruff`. On non-Windows systems the
process is replaced directly (impossible on Windows unfortunately).

```
❯ docker run --rm python:3.11 bash -c "for i in {1..15}; do python -m timeit -n 1 -r 1 'from pathlib import Path'; done"
1 loop, best of 1: 25.7 msec per loop
1 loop, best of 1: 3.07 msec per loop
1 loop, best of 1: 3.16 msec per loop
1 loop, best of 1: 3.06 msec per loop
1 loop, best of 1: 3.32 msec per loop
1 loop, best of 1: 3.93 msec per loop
1 loop, best of 1: 3.26 msec per loop
1 loop, best of 1: 3.73 msec per loop
1 loop, best of 1: 3.1 msec per loop
1 loop, best of 1: 3.29 msec per loop
1 loop, best of 1: 3.12 msec per loop
1 loop, best of 1: 3.05 msec per loop
1 loop, best of 1: 3.04 msec per loop
1 loop, best of 1: 3.19 msec per loop
1 loop, best of 1: 3.04 msec per loop

❯ docker run --rm python:3.11 bash -c "for i in {1..15}; do python -m timeit -n 1 -r 1 'import subprocess'; done"
1 loop, best of 1: 31.2 msec per loop
1 loop, best of 1: 3.75 msec per loop
1 loop, best of 1: 4.71 msec per loop
1 loop, best of 1: 3.88 msec per loop
1 loop, best of 1: 4.08 msec per loop
1 loop, best of 1: 4.35 msec per loop
1 loop, best of 1: 3.94 msec per loop
1 loop, best of 1: 4.06 msec per loop
1 loop, best of 1: 3.88 msec per loop
1 loop, best of 1: 3.85 msec per loop
1 loop, best of 1: 3.84 msec per loop
1 loop, best of 1: 4.01 msec per loop
1 loop, best of 1: 4.21 msec per loop
1 loop, best of 1: 4.07 msec per loop
1 loop, best of 1: 4.11 msec per loop

❯ python -m timeit -n 1 -r 1 "from pathlib import Path"
1 loop, best of 1: 5.25 msec per loop

❯ python -m timeit -n 1 -r 1 "import subprocess"
1 loop, best of 1: 7.61 msec per loop
```
2023-12-31 07:11:26 -05:00
Charlie Marsh
48e04cc2c8
Add row and column numbers to formatted parse errors (#9321)
## Summary

We now render parse errors in the formatter identically to those in the
linter, e.g.:

```
❯ cargo run -p ruff_cli -- format foo.py
error: Failed to parse foo.py:1:17: Unexpected token '='
```

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

Closes https://github.com/astral-sh/ruff/issues/9311.
2023-12-31 07:10:45 -05:00
Charlie Marsh
e80260a3c5
Remove source path from parser errors (#9322)
## Summary

I always found it odd that we had to pass this in, since it's really
higher-level context for the error. The awkwardness is further evidenced
by the fact that we pass in fake values everywhere (even outside of
tests). The source path isn't actually used to display the error; it's
only accessed elsewhere to _re-display_ the error in certain cases. This
PR modifies to instead pass the path directly in those cases.
2023-12-30 20:33:05 +00:00
Charlie Marsh
eb9a1bc5f1
Use consistent re-export from ruff_source_file (#9320)
Right now, we both re-export (via `pub use`) and mark the modules
themselves a `pub`, so they can be imported through two different paths.
2023-12-30 14:48:45 -05:00
Zanie Blue
c01bb0d485
Report errors when repositories cannot be cloned during ecosystem checks (#9314)
Otherwise, the directory is just missing later and you need to dig
through logs to understand why.
2023-12-29 21:40:22 -06:00
Zanie Blue
fca9dc5ce2
Fix typo in truncation comment (#9318) 2023-12-29 21:40:12 -06:00
Charlie Marsh
94727996e8
Respect runtime-required decorators on functions (#9317)
## Summary

This PR modifies the semantics of `runtime-evaluated-decorators` to
respect decorators on both classes _and_ functions. Historically, this
only respected classes, since the common use-case is (e.g.)
`pydantic.BaseModel` -- but functions are equally valid.

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

## Test Plan

`cargo test`
2023-12-29 22:14:53 -04:00
Charlie Marsh
97e9d3c54f
Use Display for formatter parse errors (#9316)
## Summary

This helps a bit with (but does not close) the issues described in
https://github.com/astral-sh/ruff/issues/9311. E.g., now, we at least
see: `error: Failed to format main.py: source contains syntax errors:
invalid syntax. Got unexpected token '=' at byte offset 20`.
2023-12-29 22:26:57 +00:00
Charlie Marsh
2895e7d126
Respect mixed return and raise cases in return-type analysis (#9310)
## Summary

Given:

```python
from somewhere import get_cfg

def lookup_cfg(cfg_description):
    cfg = get_cfg(cfg_description)
    if cfg is not None:
        return cfg
    raise AttributeError(f"No cfg found matching {cfg_description}")
```

We were analyzing the method from last-to-first statement. So we saw the
`raise`, then assumed the method _always_ raised. In reality, though, it
_might_ return. This PR improves the branch analysis to respect these
mixed cases.

Closes https://github.com/astral-sh/ruff/issues/9269.
Closes https://github.com/astral-sh/ruff/issues/9304.
2023-12-29 16:46:37 +00:00
Charlie Marsh
00f3c7d1d5
Respect attribute chains when resolving builtin call paths (#9309)
## Summary

When resolving `dict.__dict__`, we were discarding the `.__dict__`
segment when computing the call path.

## Test Plan

`cargo test`
2023-12-29 15:13:24 +00:00
Shantanu
ec88acc291
[pyupgrade] Document more fix unsafety for UP007 (#9306)
```
from typing import Optional
x = "asdf"
def foo(a: Optional[x]):
    pass
```
2023-12-29 08:41:29 -04:00
Mikael Arguedas
d86d3bd0b6
Change PLR0917 error message to match other PLR09XX messages (#9308)
## Summary

Remove `:` for PLR0917 to make all PLR09XX message look the same

```
PLR0904	Too many public methods (21 > 20)
PLR0911	Too many return statements (16 > 6)
PLR0912	Too many branches (13 > 12)
PLR0913	Too many arguments in function definition (10 > 5)
PLR0915	Too many statements (118 > 50)
PLR0917	Too many positional arguments: (15/5)
```
## Test Plan

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

---------

Signed-off-by: Mikael Arguedas <mikael.arguedas@gmail.com>
2023-12-29 08:40:51 -04:00
Mike Bernard
375c175d53
[pylint] Implement empty-comment (PLR2044) (#9174)
## Summary

Part of #970.

This adds Pylint's [R0244
empty_comment](https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/empty-comment.html)
lint as well as an always-safe fix.

## Test Plan

The included snapshot verifies the following:

- A line containing only a non-empty comment is not changed
- A line containing leading whitespace before a non-empty comment is not
changed
- A line containing only an empty comment has its content deleted
- A line containing only leading whitespace before an empty comment has
its content deleted
- A line containing only leading and trailing whitespace on an empty
comment has its content deleted
- A line containing trailing whitespace after a non-empty comment is not
changed
- A line containing only a single newline character (i.e. a blank line)
is not changed
- A line containing code followed by a non-empty comment is not changed
- A line containing code followed by an empty comment has its content
deleted after the last non-whitespace character
- Lines containing code and no comments are not changed
- Empty comment lines within block comments are ignored
- Empty comments within triple-quoted sections are ignored

## Comparison to Pylint

Running Ruff and Pylint 3.0.3 with Python 3.12.0 against the
`empty_comment.py` file added in this PR, we see the following:

* Identical behavior:
* empty_comment.py:3:0: R2044: Line with empty comment (empty-comment)
* empty_comment.py:4:0: R2044: Line with empty comment (empty-comment)
* empty_comment.py:5:0: R2044: Line with empty comment (empty-comment)
* empty_comment.py:18:0: R2044: Line with empty comment (empty-comment)

* Differing behavior:
* Pylint doesn't ignore empty comments in block comments commonly used
for visual spacing; I decided these were fine in this implementation
since many projects use these and likely do not want them removed.
* empty_comment.py:28:0: R2044: Line with empty comment (empty-comment)
* Pylint detects "empty comments" within the triple-quoted section at
the bottom of the file, which is arguably a bug in the Pylint
implementation since these are not truly comments. These are ignored by
this implementation.
* empty_comment.py:37:0: R2044: Line with empty comment (empty-comment)
* empty_comment.py:38:0: R2044: Line with empty comment (empty-comment)
* empty_comment.py:39:0: R2044: Line with empty comment (empty-comment)
2023-12-29 02:53:56 +00:00
Zanie Blue
57b6a8cedd
Allow config-file overrides in ecosystem checks (#9286)
Adds the ability to override `ruff.toml` or `pyproject.toml` settings
per-project during ecosystem checks.

Exploring this as a fix for the `setuptools` project error. 

Also useful for including Jupyter Notebooks in the ecosystem checks, see
#9293

Note the remaining `sphinx` project error is resolved in #9294
2023-12-28 10:44:50 -06:00
Zanie Blue
f8fc309855
Exclude failing sphinx file in ecosytem checks (#9294)
Failing due to

> error: Failed to read tests/roots/test-pycode/cp_1251_coded.py: stream
did not contain valid UTF-8

Unclear to me if ignoring is the correct response.
2023-12-28 10:42:06 -06:00
Charlie Marsh
5d5f56d563
Expand target name for better rule documentation (#9302) 2023-12-28 14:58:26 +00:00
Charlie Marsh
465f835cf9
Escape trailing placeholders in rule documentation (#9301)
## Summary

If a rule ends with a trailing placeholder (like "Use {target}"), that
gets interpreted as an HTML attribute adding, `target="target"` to the
node. This PR escapes such cases. In reality, they're rare, since we
almost always wrap placeholders in backticks, which avoids this problem
-- but in some cases, they are in fact correct to be un-backticked.

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

## Test Plan

<img width="673" alt="Screen Shot 2023-12-28 at 9 33 40 AM"
src="14aaa168-c802-4258-b82d-217a85b42ebf">
2023-12-28 09:44:51 -05:00
Mikael Arguedas
edfad461a8
[flake8-bandit/S506] Dont report violation when SafeLoader is imported from yaml.loader (#9299)
## Summary

Hey there 👋 thanks for this great project!

On python code looking like the following
```
import yaml
from yaml.loader import SafeLoader

with MY_FILE_PATH.open("r") as my_file:
    my_data = yaml.load(my_file, Loader=SafeLoader)
```

ruff reports this error:
```
S506 Probable use of unsafe loader `SafeLoader` with `yaml.load`. Allows instantiation of arbitrary objects. Consider `yaml.safe_load`.
```

This PR is an attempt to support SafeLoader being imported for either
`yaml` or `yaml.loader`

Disclaimer:
I am not familiar with Rust so this is likely not the better way of
doing it. Interested in hearing how to adapt this PR to provide similar
behavior in a better way
 

## Test Plan

The S506.py file was updated accordingly to cover the use cases and test
were confirmed to pass with this change.
2023-12-28 14:30:46 +00:00
Tetsuo Koyama
87086195c8
Add PyVista into the Who's Using Ruff? section (#9296)
Our project PyVista added ruff as a linter at
https://github.com/pyvista/pyvista/pull/5304. Please add `Who's Using
Ruff?` section if you want.
2023-12-28 09:16:46 -05:00
Charlie Marsh
e178d938cc
Respect unused-noqa via per-file-ignores (#9300)
## Summary

If `RUF100` is ignored via `per-file-ignores`, we need to avoid raising
it. `RUF100` has special "self-ignore" logic, since the rule itself
deals with `# noqa` directives. This PR wires up `per-file-ignores` to
that "self-ignore" logic.

Closes https://github.com/astral-sh/ruff/issues/9297.
2023-12-28 14:15:23 +00:00
Micha Reiser
5d4825b60f
Normalise Hex and unicode escape sequences in string (#9280) 2023-12-28 09:06:58 +08:00
Steve C
c716acc7a6
[refurb] Implement bit-count (FURB161) (#9265)
## Summary

Implements
[`FURB161`/`use-bit-count`](https://github.com/dosisod/refurb/blob/master/refurb/checks/builtin/use_bit_count.py)

See: #1348 

## Test Plan

`cargo test`
2023-12-27 15:32:51 +00:00
Shantanu
50187016cb
[refurb] Avoid false positives for math-constant (FURB152) (#9290)
Fixes #9281
2023-12-27 09:45:37 -05:00
dependabot[bot]
29513398d2
Bump env_logger from 0.10.0 to 0.10.1 (#9275) 2023-12-25 09:03:35 -05:00
Charlie Marsh
fa78d2d97c
Avoid adding return types to stub methods (#9277)
We should avoid adding `-> None` to stubs in `.pyi` files, along with a
few other cases. (We already ignore abstract methods.)

Closes https://github.com/astral-sh/ruff/issues/9270.
2023-12-25 09:03:24 -05:00
dependabot[bot]
8b70240fa2
Bump anyhow from 1.0.75 to 1.0.76 (#9274) 2023-12-25 08:12:40 -05:00
dependabot[bot]
e85e0d45f3
Bump ignore from 0.4.20 to 0.4.21 (#9273) 2023-12-25 08:12:36 -05:00
dependabot[bot]
9b43203575
Bump colored from 2.0.4 to 2.1.0 (#9271) 2023-12-25 08:12:28 -05:00
dependabot[bot]
bae3fa435d
Bump pep440_rs from 0.3.12 to 0.4.0 (#9272) 2023-12-25 08:12:20 -05:00
Zanie Blue
6e65601055
Update some references to the old repo org (#9233)
Need https://github.com/pkgxdev/pantry/issues/4531 before we can update
at
af88ffc57e/docs/installation.md (L30-L31)
2023-12-24 20:02:49 +00:00
Charlie Marsh
9d6444138b
Remove lexing and parsing from the linter benchmark (#9264)
## Summary

This PR adds some helper structs to the linter paths to enable passing
in the pre-computed tokens and parsed source code during benchmarking,
to remove lexing and parsing from the overall linter benchmark
measurement. We already remove parsing for the formatter, and we have
separate benchmarks for the lexer and the parser, so this should make it
much easier to measure linter performance changes.
2023-12-23 16:43:11 -05:00
Charlie Marsh
6d0c9c4e95
Avoid asyncio-dangling-task for nonlocal and global bindings (#9263)
Closes https://github.com/astral-sh/ruff/issues/9262.
2023-12-23 21:33:50 +00:00