Compare commits

..

29 Commits

Author SHA1 Message Date
Charlie Marsh
3937885f37 Bump version to 0.0.40 2022-09-16 04:57:21 -04:00
Charlie Marsh
24de97d951 Create cache directory prior to writing .gitignore 2022-09-16 04:56:58 -04:00
Charlie Marsh
06e5b3e457 Bump version to 0.0.39 2022-09-15 21:41:14 -04:00
Charlie Marsh
68a0e6dc19 Remove erroneous test dir 2022-09-15 21:41:00 -04:00
Charlie Marsh
9d4a4478f7 Improve exclusion syntax to match exact files (#209) 2022-09-15 21:40:49 -04:00
Charlie Marsh
6bbf3f46c4 Add .gitignore to .ruff_cache (#208) 2022-09-15 20:40:06 -04:00
Charlie Marsh
4ac4e8c991 Exclude .ruff_cache by default (#207) 2022-09-15 20:39:39 -04:00
Dmitry Dygalo
0091a3ae5f chore: Do not read the same file twice (#206) 2022-09-15 16:05:29 -04:00
Patrick Haller
17b3109a8b Update docs with --format flag (#205) 2022-09-15 16:04:07 -04:00
Charlie Marsh
71520213c1 Allow __path__ in __init__.py (#201) 2022-09-15 09:44:03 -04:00
Charlie Marsh
f24e7a0052 Add trailing period to help message 2022-09-15 09:43:51 -04:00
Patrick Haller
507e9f7ec3 Fix: Structured output Issue Fix (#186) 2022-09-15 09:43:10 -04:00
Charlie Marsh
592c53c8bf Use binding location when reporting F821 errors (#200) 2022-09-14 22:51:07 -04:00
Charlie Marsh
a2df89dedd Bump version to 0.0.38 2022-09-14 22:38:42 -04:00
Charlie Marsh
b8f12d2e79 Raise error when failing to parse (#199) 2022-09-14 22:37:55 -04:00
Charlie Marsh
67b1d0463a Pull in pycodestyle tests for E checks (#195) 2022-09-14 22:22:53 -04:00
Charlie Marsh
d008a181ec Improve default exclusions and support extend-exclude (#188) 2022-09-14 22:21:17 -04:00
Charlie Marsh
6d612a428a Migrate linter tests to insta (#194) 2022-09-14 21:52:44 -04:00
Charlie Marsh
c0cb73ab16 Implement E721 (#193) 2022-09-14 21:10:29 -04:00
Charlie Marsh
2e1eb84cbf Implement F632 (#190) 2022-09-14 18:22:35 -04:00
Charlie Marsh
b03a8728b5 Add support for from __future__ import annotations (#189) 2022-09-14 18:22:19 -04:00
Charlie Marsh
1dd3350a30 Revert "Adding flag and logic for different output format" (#187) 2022-09-14 14:20:02 -04:00
Patrick Haller
bda34945a5 Adding flag and logic for different output format (#185) 2022-09-14 10:43:32 -04:00
Dmitry Dygalo
85dcaa8d3c chore: Avoid collect in inner_main (#184) 2022-09-14 08:16:04 -04:00
Charlie Marsh
4ac74ed0ad Revert erroneous pyproject.toml changes 2022-09-14 08:14:26 -04:00
Dmitry Dygalo
b7e2a4b9a9 feat: Implement InvalidPrintSyntax (F633) (#182) 2022-09-13 21:10:20 -04:00
Dmitry Dygalo
53a7758248 Avoid some allocations (#179) 2022-09-13 10:07:22 -04:00
Dmitry Dygalo
2ba767957d refactor: Use while let Some instead of calling is_empty (#180) 2022-09-13 10:06:49 -04:00
Dmitry Dygalo
08152787e1 chore: Use once_cell instead of lazy_static (#178) 2022-09-13 10:06:21 -04:00
75 changed files with 2656 additions and 1303 deletions

View File

@@ -1,5 +1,5 @@
repos:
- repo: https://github.com/charliermarsh/ruff
rev: v0.0.37
rev: v0.0.40
hooks:
- id: lint

87
Cargo.lock generated
View File

@@ -458,6 +458,19 @@ dependencies = [
"cache-padded",
]
[[package]]
name = "console"
version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89eab4d20ce20cea182308bca13088fecea9c05f6776cf287205d41a0ed3c847"
dependencies = [
"encode_unicode",
"libc",
"once_cell",
"terminal_size",
"winapi 0.3.9",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
@@ -652,6 +665,12 @@ dependencies = [
"log",
]
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "event-listener"
version = "2.5.3"
@@ -1011,6 +1030,20 @@ dependencies = [
"libc",
]
[[package]]
name = "insta"
version = "1.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc61e98be01e89296f3343a878e9f8ca75a494cb5aaf29df65ef55734aeb85f5"
dependencies = [
"console",
"linked-hash-map",
"once_cell",
"serde",
"similar",
"yaml-rust",
]
[[package]]
name = "instant"
version = "0.1.12"
@@ -1122,6 +1155,12 @@ version = "0.2.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "505e71a4706fa491e9b1b55f51b95d4037d0821ee40131190475f692b35b009b"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "lock_api"
version = "0.4.7"
@@ -1393,6 +1432,24 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "path-absolutize"
version = "3.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3de4b40bd9736640f14c438304c09538159802388febb02c8abaae0846c1f13"
dependencies = [
"path-dedot",
]
[[package]]
name = "path-dedot"
version = "3.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d611d5291372b3738a34ebf0d1f849e58b1dcc1101032f76a346eaa1f8ddbb5b"
dependencies = [
"once_cell",
]
[[package]]
name = "percent-encoding"
version = "2.1.0"
@@ -1744,7 +1801,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.37"
version = "0.0.40"
dependencies = [
"anyhow",
"bincode",
@@ -1758,11 +1815,12 @@ dependencies = [
"fern",
"filetime",
"glob",
"insta",
"itertools",
"lazy_static",
"log",
"notify",
"once_cell",
"path-absolutize",
"rayon",
"regex",
"rustpython-parser",
@@ -1978,6 +2036,12 @@ dependencies = [
"libc",
]
[[package]]
name = "similar"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62ac7f900db32bf3fd12e0117dd3dc4da74bc52ebaac97f39668446d89694803"
[[package]]
name = "siphasher"
version = "0.3.10"
@@ -2100,6 +2164,16 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "terminal_size"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]]
name = "terminfo"
version = "0.7.3"
@@ -2595,3 +2669,12 @@ dependencies = [
"winapi 0.2.8",
"winapi-build",
]
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff"
version = "0.0.37"
version = "0.0.40"
edition = "2021"
[lib]
@@ -18,12 +18,12 @@ common-path = { version = "1.0.0" }
dirs = { version = "4.0.0" }
fern = { version = "0.6.1" }
filetime = { version = "0.2.17" }
glob = { version = "0.3.0" }
glob = "0.3.0"
itertools = "0.10.3"
lazy_static = "1.4.0"
log = { version = "0.4.17" }
notify = { version = "4.0.17" }
once_cell = { version = "1.13.1" }
path-absolutize = "3.0.13"
rayon = { version = "1.5.3" }
regex = { version = "1.6.0" }
rustpython-parser = { features = ["lalrpop"], git = "https://github.com/charliermarsh/RustPython.git", rev = "7d21c6923a506e79cc041708d83cef925efd33f4" }
@@ -33,6 +33,9 @@ toml = { version = "0.5.9" }
update-informer = { version = "0.5.0", default_features = false, features = ["pypi"], optional = true }
walkdir = { version = "2.3.2" }
[dev-dependencies]
insta = { version = "1.19.1", features = ["yaml"] }
[features]
default = ["update-informer"]
update-informer = ["dep:update-informer"]
@@ -42,3 +45,9 @@ panic = "abort"
lto = "thin"
codegen-units = 1
opt-level = 3
[profile.dev.package.insta]
opt-level = 3
[profile.dev.package.similar]
opt-level = 3

103
README.md
View File

@@ -57,7 +57,7 @@ ruff also works with [Pre-Commit](https://pre-commit.com) (requires Cargo on sys
```yaml
repos:
- repo: https://github.com/charliermarsh/ruff
rev: v0.0.37
rev: v0.0.40
hooks:
- id: lint
```
@@ -86,7 +86,7 @@ ruff path/to/code/ --select F401 F403
See `ruff --help` for more:
```shell
ruff (v0.0.37)
ruff (v0.0.40)
An extremely fast Python linter.
USAGE:
@@ -96,19 +96,42 @@ ARGS:
<FILES>...
OPTIONS:
-e, --exit-zero Exit with status code "0", even upon detecting errors
--exclude <EXCLUDE>... List of file and/or directory patterns to exclude from checks
-f, --fix Attempt to automatically fix lint errors
-h, --help Print help information
--ignore <IGNORE>... List of error codes to ignore
-n, --no-cache Disable cache reads
-q, --quiet Disable all logging (but still exit with status code "1" upon
detecting errors)
--select <SELECT>... List of error codes to enable
-v, --verbose Enable verbose logging
-w, --watch Run in watch mode by re-running whenever files change
-e, --exit-zero
Exit with status code "0", even upon detecting errors
--exclude <EXCLUDE>...
List of paths, used to exclude files and/or directories from checks
--extend-exclude <EXTEND_EXCLUDE>...
Like --exclude, but adds additional files and directories on top of the excluded ones
-f, --fix
Attempt to automatically fix lint errors
--format <FORMAT>
Output serialization format for error messages [default: text] [possible values: text,
json]
-h, --help
Print help information
--ignore <IGNORE>...
List of error codes to ignore
-n, --no-cache
Disable cache reads
-q, --quiet
Disable all logging (but still exit with status code "1" upon detecting errors)
--select <SELECT>...
List of error codes to enable
-v, --verbose
Enable verbose logging
-w, --watch
Run in watch mode by re-running whenever files change
```
Exclusions are based on globs, and can be either:
- Single-path patterns, like `.mypy_cache` (to exclude any directory named `.mypy_cache` in the
tree), `foo.py` (to exclude any file named `foo.py`), or `foo_*.py` (to exclude any file matching
`foo_*.py` ).
- Relative patterns, like `./directory/foo.py` (to exclude that specific file) or `./directory/*.py`
(to exclude any Python files in `./directory`). Note that these paths are relative to the
directory from which you execute `ruff`, and _not_ the directory of the `pyproject.toml`.
### Compatibility with Black
ruff is intended to be compatible with [Black](https://github.com/psf/black), and should be
@@ -124,14 +147,14 @@ ruff's goal is to achieve feature-parity with Flake8 when used (1) without any p
stylistic checks; limiting to Python 3 obviates the need for certain compatibility checks.)
Under those conditions, Flake8 implements about 60 rules, give or take. At time of writing, ruff
implements 38 rules. (Note that these 38 rules likely cover a disproportionate share of errors:
implements 42 rules. (Note that these 42 rules likely cover a disproportionate share of errors:
unused imports, undefined variables, etc.)
The unimplemented rules are tracked in #170, and include:
- 14 rules related to string `.format` calls.
- 1 rule related to parsing and syntax.
- 6 logical rules.
- 4 logical rules.
- 1 rule related to parsing.
Beyond rule-set parity, ruff suffers from the following limitations vis-à-vis Flake8:
@@ -149,12 +172,14 @@ Beyond rule-set parity, ruff suffers from the following limitations vis-à-vis F
| E712 | TrueFalseComparison | Comparison to `True` should be `cond is True` |
| E713 | NotInTest | Test for membership should be `not in` |
| E714 | NotIsTest | Test for object identity should be `is not` |
| E721 | TypeComparison | do not compare types, use `isinstance()` |
| E722 | DoNotUseBareExcept | Do not use bare `except` |
| E731 | DoNotAssignLambda | Do not assign a lambda expression, use a def |
| E741 | AmbiguousVariableName | ambiguous variable name '...' |
| E742 | AmbiguousClassName | ambiguous class name '...' |
| E743 | AmbiguousFunctionName | ambiguous function name '...' |
| E902 | IOError | No such file or directory: `...` |
| E999 | SyntaxError | SyntaxError: ... |
| F401 | UnusedImport | `...` imported but unused |
| F403 | ImportStarUsage | `from ... import *` used; unable to detect undefined names |
| F404 | LateFutureImport | from __future__ imports must occur at the beginning of the file |
@@ -166,6 +191,8 @@ Beyond rule-set parity, ruff suffers from the following limitations vis-à-vis F
| F621 | TooManyExpressionsInStarredAssignment | too many expressions in star-unpacking assignment |
| F622 | TwoStarredExpressions | two starred expressions in assignment |
| F631 | AssertTuple | Assert test is a non-empty tuple, which is always `True` |
| F632 | IsLiteral | use ==/!= to compare constant literals |
| F633 | InvalidPrintSyntax | use of >> is invalid with print function |
| F634 | IfTuple | If test is a tuple, which is always `True` |
| F701 | BreakOutsideLoop | `break` outside loop |
| F702 | ContinueOutsideLoop | `continue` not properly in loop |
@@ -217,28 +244,28 @@ Add this `pyproject.toml` to the CPython directory:
[tool.ruff]
line-length = 88
exclude = [
"Lib/lib2to3/tests/data/bom.py",
"Lib/lib2to3/tests/data/crlf.py",
"Lib/lib2to3/tests/data/different_encoding.py",
"Lib/lib2to3/tests/data/false_encoding.py",
"Lib/lib2to3/tests/data/py2_test_grammar.py",
"Lib/test/bad_coding2.py",
"Lib/test/badsyntax_3131.py",
"Lib/test/badsyntax_pep3120.py",
"Lib/test/encoded_modules/module_iso_8859_1.py",
"Lib/test/encoded_modules/module_koi8_r.py",
"Lib/test/test_fstring.py",
"Lib/test/test_grammar.py",
"Lib/test/test_importlib/test_util.py",
"Lib/test/test_named_expressions.py",
"Lib/test/test_patma.py",
"Lib/test/test_source_encoding.py",
"Tools/c-analyzer/c_parser/parser/_delim.py",
"Tools/i18n/pygettext.py",
"Tools/test2to3/maintest.py",
"Tools/test2to3/setup.py",
"Tools/test2to3/test/test_foo.py",
"Tools/test2to3/test2to3/hello.py",
"./resources/test/cpython/Lib/lib2to3/tests/data/bom.py",
"./resources/test/cpython/Lib/lib2to3/tests/data/crlf.py",
"./resources/test/cpython/Lib/lib2to3/tests/data/different_encoding.py",
"./resources/test/cpython/Lib/lib2to3/tests/data/false_encoding.py",
"./resources/test/cpython/Lib/lib2to3/tests/data/py2_test_grammar.py",
"./resources/test/cpython/Lib/test/bad_coding2.py",
"./resources/test/cpython/Lib/test/badsyntax_3131.py",
"./resources/test/cpython/Lib/test/badsyntax_pep3120.py",
"./resources/test/cpython/Lib/test/encoded_modules/module_iso_8859_1.py",
"./resources/test/cpython/Lib/test/encoded_modules/module_koi8_r.py",
"./resources/test/cpython/Lib/test/test_fstring.py",
"./resources/test/cpython/Lib/test/test_grammar.py",
"./resources/test/cpython/Lib/test/test_importlib/test_util.py",
"./resources/test/cpython/Lib/test/test_named_expressions.py",
"./resources/test/cpython/Lib/test/test_patma.py",
"./resources/test/cpython/Lib/test/test_source_encoding.py",
"./resources/test/cpython/Tools/c-analyzer/c_parser/parser/_delim.py",
"./resources/test/cpython/Tools/i18n/pygettext.py",
"./resources/test/cpython/Tools/test2to3/maintest.py",
"./resources/test/cpython/Tools/test2to3/setup.py",
"./resources/test/cpython/Tools/test2to3/test/test_foo.py",
"./resources/test/cpython/Tools/test2to3/test2to3/hello.py",
]
```

View File

@@ -13,13 +13,15 @@ fn main() {
CheckKind::DoNotAssignLambda,
CheckKind::DoNotUseBareExcept,
CheckKind::DuplicateArgumentName,
CheckKind::ForwardAnnotationSyntaxError("...".to_string()),
CheckKind::FStringMissingPlaceholders,
CheckKind::ForwardAnnotationSyntaxError("...".to_string()),
CheckKind::FutureFeatureNotDefined("...".to_string()),
CheckKind::IOError("...".to_string()),
CheckKind::IfTuple,
CheckKind::ImportStarNotPermitted("...".to_string()),
CheckKind::ImportStarUsage("...".to_string()),
CheckKind::InvalidPrintSyntax,
CheckKind::IsLiteral,
CheckKind::LateFutureImport,
CheckKind::LineTooLong(89, 88),
CheckKind::ModuleImportNotAtTopOfFile,
@@ -31,9 +33,11 @@ fn main() {
CheckKind::NotIsTest,
CheckKind::RaiseNotImplemented,
CheckKind::ReturnOutsideFunction,
CheckKind::SyntaxError("...".to_string()),
CheckKind::TooManyExpressionsInStarredAssignment,
CheckKind::TrueFalseComparison(true, RejectedCmpop::Eq),
CheckKind::TwoStarredExpressions,
CheckKind::TypeComparison,
CheckKind::UndefinedExport("...".to_string()),
CheckKind::UndefinedLocal("...".to_string()),
CheckKind::UndefinedName("...".to_string()),

View File

@@ -1,11 +1,50 @@
if var == None:
#: E711
if res == None:
pass
#: E711
if res != None:
pass
#: E711
if None == res:
pass
#: E711
if None != res:
pass
#: E711
if res[1] == None:
pass
#: E711
if res[1] != None:
pass
#: E711
if None != res[1]:
pass
#: E711
if None == res[1]:
pass
if None != var:
#: Okay
if x not in y:
pass
if var is None:
if not (X in Y or X is Z):
pass
if None is not var:
if not (X in Y):
pass
if x is not y:
pass
if X is not Y is not Z:
pass
if TrueElement.get_element(True) == TrueElement.get_element(False):
pass
if (True) == TrueElement or x == TrueElement:
pass
assert (not foo) in bar
assert {"x": not foo} in bar
assert [42, not foo] in bar

View File

@@ -1,14 +1,46 @@
if var == True:
#: E712
if res == True:
pass
#: E712
if res != False:
pass
#: E712
if True != res:
pass
#: E712
if False == res:
pass
#: E712
if res[1] == True:
pass
#: E712
if res[1] != False:
pass
#: E712
var = 1 if cond == True else -1 if cond == False else cond
#: E712
if (True) == TrueElement or x == TrueElement:
pass
if False != var:
#: Okay
if x not in y:
pass
if var != False != True:
if not (X in Y or X is Z):
pass
if var is True:
if not (X in Y):
pass
if False is not var:
if x is not y:
pass
if X is not Y is not Z:
pass
if TrueElement.get_element(True) == TrueElement.get_element(False):
pass
assert (not foo) in bar
assert {"x": not foo} in bar
assert [42, not foo] in bar

View File

@@ -1,7 +1,38 @@
my_list = [1, 2, 3]
if not num in my_list:
print(num)
#: E713
if not X in Y:
pass
#: E713
if not X.B in Y:
pass
#: E713
if not X in Y and Z == "zero":
pass
#: E713
if X == "zero" or not Y in Z:
pass
#: E713
if not (X in Y):
pass
my_list = [1, 2, 3]
if num not in my_list:
print(num)
#: Okay
if x not in y:
pass
if not (X in Y or X is Z):
pass
if x is not y:
pass
if X is not Y is not Z:
pass
if TrueElement.get_element(True) == TrueElement.get_element(False):
pass
if (True) == TrueElement or x == TrueElement:
pass
assert (not foo) in bar
assert {"x": not foo} in bar
assert [42, not foo] in bar

View File

@@ -1,5 +1,38 @@
if not user is None:
print(user.name)
#: E714
if not X is Y:
pass
#: E714
if not X.B is Y:
pass
#: E714
if not X is Y is not Z:
pass
if user is not None:
print(user.name)
#: Okay
if not X is not Y:
pass
if x not in y:
pass
if not (X in Y or X is Z):
pass
if not (X in Y):
pass
if x is not y:
pass
if X is not Y is not Z:
pass
if TrueElement.get_element(True) == TrueElement.get_element(False):
pass
if (True) == TrueElement or x == TrueElement:
pass
assert (not foo) in bar
assert {"x": not foo} in bar
assert [42, not foo] in bar

54
resources/test/fixtures/E721.py vendored Normal file
View File

@@ -0,0 +1,54 @@
#: E721
if type(res) == type(42):
pass
#: E721
if type(res) != type(""):
pass
#: E721
import types
if res == types.IntType:
pass
#: E721
import types
if type(res) is not types.ListType:
pass
#: E721
assert type(res) == type(False) or type(res) == type(None)
#: E721
assert type(res) == type([])
#: E721
assert type(res) == type(())
#: E721
assert type(res) == type((0,))
#: E721
assert type(res) == type((0))
#: E721
assert type(res) != type((1,))
#: E721
assert type(res) is type((1,))
#: E721
assert type(res) is not type((1,))
#: E211 E721
assert type(res) == type(
[
2,
]
)
#: E201 E201 E202 E721
assert type(res) == type(())
#: E201 E202 E721
assert type(res) == type((0,))
#: Okay
import types
if isinstance(res, int):
pass
if isinstance(res, str):
pass
if isinstance(res, types.MethodType):
pass
if type(a) != type(b) or type(a) == type(ccc):
pass

View File

@@ -1,9 +1,33 @@
#: E722
try:
pass
except:
pass
#: E722
try:
pass
except Exception:
pass
except:
pass
#: E722
try:
pass
except:
pass
#: Okay
fake_code = """"
try:
do_something()
except:
pass
"""
try:
pass
except Exception:
pass
#: Okay
from . import compute_type
if compute_type(foo) == 5:
pass

View File

@@ -1,6 +1,21 @@
from typing import Callable, Iterable
#: E731
f = lambda x: 2 * x
#: E731
f = lambda x: 2 * x
#: E731
while False:
this = lambda y, z: 2 * x
a = lambda x: x**2
b = map(lambda x: 2 * x, range(3))
c: Callable = lambda x: x**2
d: Iterable = map(lambda x: 2 * x, range(3))
f = object()
#: E731
f.method = lambda: "Method"
f = {}
#: E731
f["a"] = lambda x: x ** 2
f = []
f.append(lambda x: x ** 2)
lambda: "no-op"

2
resources/test/fixtures/E999.py vendored Normal file
View File

@@ -0,0 +1,2 @@
def x():

5
resources/test/fixtures/F632.py vendored Normal file
View File

@@ -0,0 +1,5 @@
if x is "abc":
pass
if 123 is not y:
pass

4
resources/test/fixtures/F633.py vendored Normal file
View File

@@ -0,0 +1,4 @@
from __future__ import print_function
import sys
print >> sys.stderr, "Hello"

View File

@@ -77,3 +77,8 @@ class Ticket:
def set_status(self, status: Status):
self.status = status
def update_tomato():
print(TOMATO)
TOMATO = "cherry tomato"

View File

@@ -0,0 +1,3 @@
print(__path__)
__all__ = ["a", "b", "c"]

View File

@@ -0,0 +1,9 @@
a = "abc"
b = f"ghi{'jkl'}"
c = f"def"
d = f"def" + "ghi"
e = (
f"def" +
"ghi"
)

View File

@@ -0,0 +1,27 @@
from __future__ import annotations
from dataclasses import dataclass
from models import Fruit, Nut
@dataclass
class Foo:
x: int
y: int
@classmethod
def a(cls) -> Foo:
return cls(x=0, y=0)
@classmethod
def b(cls) -> "Foo":
return cls(x=0, y=0)
@classmethod
def c(cls) -> Bar:
return cls(x=0, y=0)
@classmethod
def d(cls) -> Fruit:
return cls(x=0, y=0)

View File

@@ -1,6 +1,10 @@
[tool.ruff]
line-length = 88
exclude = ["excluded.py", "**/migrations"]
extend-exclude = [
"excluded.py",
"migrations",
"./resources/test/fixtures/directory/also_excluded.py",
]
select = [
"E402",
"E501",
@@ -8,12 +12,14 @@ select = [
"E712",
"E713",
"E714",
"E721",
"E722",
"E731",
"E741",
"E742",
"E743",
"E902",
"E999",
"F401",
"F403",
"F404",
@@ -25,6 +31,8 @@ select = [
"F621",
"F622",
"F631",
"F632",
"F633",
"F634",
"F701",
"F702",

View File

@@ -42,18 +42,20 @@ pub fn check_not_tests(
if matches!(op, Unaryop::Not) {
if let ExprKind::Compare { ops, .. } = &operand.node {
match ops[..] {
[Cmpop::In] => {
if check_not_in {
checks.push(Check::new(CheckKind::NotInTest, operand.location));
for op in ops {
match op {
Cmpop::In => {
if check_not_in {
checks.push(Check::new(CheckKind::NotInTest, operand.location));
}
}
}
[Cmpop::Is] => {
if check_not_is {
checks.push(Check::new(CheckKind::NotIsTest, operand.location));
Cmpop::Is => {
if check_not_is {
checks.push(Check::new(CheckKind::NotIsTest, operand.location));
}
}
_ => {}
}
_ => {}
}
}
}
@@ -156,9 +158,7 @@ pub fn check_useless_object_inheritance(
CheckKind::UselessObjectInheritance(name.to_string()),
expr.location,
);
if matches!(autofix, fixer::Mode::Generate)
|| matches!(autofix, fixer::Mode::Apply)
{
if matches!(autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
if let Some(fix) = fixes::remove_class_def_base(
locator,
&stmt.location,
@@ -254,9 +254,7 @@ pub fn check_assert_equals(expr: &Expr, autofix: &fixer::Mode) -> Option<Check>
if let ExprKind::Name { id, .. } = &value.node {
if id == "self" {
let mut check = Check::new(CheckKind::NoAssertEquals, expr.location);
if matches!(autofix, fixer::Mode::Generate)
|| matches!(autofix, fixer::Mode::Apply)
{
if matches!(autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
check.amend(Fix {
content: "assertEqual".to_string(),
start: Location::new(expr.location.row(), expr.location.column() + 1),
@@ -315,7 +313,7 @@ pub fn check_repeated_keys(
(Some(DictionaryKey::Variable(v1)), Some(DictionaryKey::Variable(v2))) => {
if check_repeated_variables && v1 == v2 {
checks.push(Check::new(
CheckKind::MultiValueRepeatedKeyVariable(v2.to_string()),
CheckKind::MultiValueRepeatedKeyVariable((*v2).to_string()),
k2.location,
))
}
@@ -436,6 +434,90 @@ pub fn check_literal_comparisons(
checks
}
fn is_constant(expr: &Expr) -> bool {
match &expr.node {
ExprKind::Constant { .. } => true,
ExprKind::Tuple { elts, .. } => elts.iter().all(is_constant),
_ => false,
}
}
fn is_singleton(expr: &Expr) -> bool {
matches!(
expr.node,
ExprKind::Constant {
value: Constant::None | Constant::Bool(_) | Constant::Ellipsis,
..
}
)
}
fn is_constant_non_singleton(expr: &Expr) -> bool {
is_constant(expr) && !is_singleton(expr)
}
/// Check IsLiteral compliance.
pub fn check_is_literal(
left: &Expr,
ops: &Vec<Cmpop>,
comparators: &Vec<Expr>,
location: Location,
) -> Vec<Check> {
let mut checks: Vec<Check> = vec![];
let mut left = left;
for (op, right) in izip!(ops, comparators) {
if matches!(op, Cmpop::Is | Cmpop::IsNot)
&& (is_constant_non_singleton(left) || is_constant_non_singleton(right))
{
checks.push(Check::new(CheckKind::IsLiteral, location));
}
left = right;
}
checks
}
/// Check TypeComparison compliance.
pub fn check_type_comparison(
ops: &Vec<Cmpop>,
comparators: &Vec<Expr>,
location: Location,
) -> Vec<Check> {
let mut checks: Vec<Check> = vec![];
for (op, right) in izip!(ops, comparators) {
if matches!(op, Cmpop::Is | Cmpop::IsNot | Cmpop::Eq | Cmpop::NotEq) {
match &right.node {
ExprKind::Call { func, args, .. } => {
if let ExprKind::Name { id, .. } = &func.node {
// Ex) type(False)
if id == "type" {
if let Some(arg) = args.first() {
// Allow comparison for types which are not obvious.
if !matches!(arg.node, ExprKind::Name { .. }) {
checks.push(Check::new(CheckKind::TypeComparison, location));
}
}
}
}
}
ExprKind::Attribute { value, .. } => {
if let ExprKind::Name { id, .. } = &value.node {
// Ex) types.IntType
if id == "types" {
checks.push(Check::new(CheckKind::TypeComparison, location));
}
}
}
_ => {}
}
}
}
checks
}
/// Check TwoStarredExpressions and TooManyExpressionsInStarredAssignment compliance.
pub fn check_starred_expressions(
elts: &[Expr],

View File

@@ -22,7 +22,7 @@ pub fn extract_all_names(stmt: &Stmt, scope: &Scope) -> Vec<String> {
if let StmtKind::AugAssign { .. } = &stmt.node {
if let Some(binding) = scope.values.get("__all__") {
if let BindingKind::Export(existing) = &binding.kind {
names.extend(existing.clone());
names.extend_from_slice(existing);
}
}
}
@@ -70,9 +70,7 @@ pub fn extract_all_names(stmt: &Stmt, scope: &Scope) -> Vec<String> {
pub fn on_conditional_branch(parent_stack: &[usize], parents: &[&Stmt]) -> bool {
for index in parent_stack.iter().rev() {
let parent = parents[*index];
if matches!(parent.node, StmtKind::If { .. })
|| matches!(parent.node, StmtKind::While { .. })
{
if matches!(parent.node, StmtKind::If { .. } | StmtKind::While { .. }) {
return true;
}
if let StmtKind::Expr { value } = &parent.node {
@@ -89,10 +87,10 @@ pub fn on_conditional_branch(parent_stack: &[usize], parents: &[&Stmt]) -> bool
pub fn in_nested_block(parent_stack: &[usize], parents: &[&Stmt]) -> bool {
for index in parent_stack.iter().rev() {
let parent = parents[*index];
if matches!(parent.node, StmtKind::Try { .. })
|| matches!(parent.node, StmtKind::If { .. })
|| matches!(parent.node, StmtKind::With { .. })
{
if matches!(
parent.node,
StmtKind::Try { .. } | StmtKind::If { .. } | StmtKind::With { .. }
) {
return true;
}
}

View File

@@ -53,5 +53,7 @@ pub enum BindingKind {
pub struct Binding {
pub kind: BindingKind,
pub location: Location,
pub used: Option<usize>,
/// Tuple of (scope index, location) indicating the scope and location at which the binding was
/// last used.
pub used: Option<(usize, Location)>,
}

View File

@@ -1,8 +1,10 @@
use std::collections::hash_map::DefaultHasher;
use std::fs::Metadata;
use std::fs::{create_dir_all, File, Metadata};
use std::hash::{Hash, Hasher};
use std::io::Write;
use std::path::Path;
use anyhow::Result;
use cacache::Error::EntryNotFound;
use filetime::FileTime;
use log::error;
@@ -83,6 +85,16 @@ fn cache_key(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> String
)
}
pub fn init() -> Result<()> {
let gitignore_path = Path::new(cache_dir()).join(".gitignore");
if gitignore_path.exists() {
return Ok(());
}
create_dir_all(cache_dir())?;
let mut file = File::create(gitignore_path)?;
file.write_all(b"*").map_err(|e| e.into())
}
pub fn get(
path: &Path,
metadata: &Metadata,

View File

@@ -3,7 +3,7 @@ use std::path::Path;
use rustpython_parser::ast::{
Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind,
KeywordData, Location, Stmt, StmtKind, Suite,
KeywordData, Location, Operator, Stmt, StmtKind, Suite,
};
use rustpython_parser::parser;
@@ -26,7 +26,7 @@ struct Checker<'a> {
locator: SourceCodeLocator<'a>,
settings: &'a Settings,
autofix: &'a fixer::Mode,
path: &'a str,
path: &'a Path,
// Computed checks.
checks: Vec<Check>,
// Retain all scopes and parent nodes, along with a stack of indexes to track which are active
@@ -36,7 +36,8 @@ struct Checker<'a> {
scopes: Vec<Scope>,
scope_stack: Vec<usize>,
dead_scopes: Vec<usize>,
deferred_annotations: Vec<(Location, &'a str)>,
deferred_string_annotations: Vec<(Location, &'a str)>,
deferred_annotations: Vec<(&'a Expr, Vec<usize>, Vec<usize>)>,
deferred_functions: Vec<(&'a Stmt, Vec<usize>, Vec<usize>)>,
deferred_lambdas: Vec<(&'a Expr, Vec<usize>, Vec<usize>)>,
deferred_assignments: Vec<usize>,
@@ -47,13 +48,14 @@ struct Checker<'a> {
seen_non_import: bool,
seen_docstring: bool,
futures_allowed: bool,
annotations_future_enabled: bool,
}
impl<'a> Checker<'a> {
pub fn new(
settings: &'a Settings,
autofix: &'a fixer::Mode,
path: &'a str,
path: &'a Path,
content: &'a str,
) -> Checker<'a> {
Checker {
@@ -67,6 +69,7 @@ impl<'a> Checker<'a> {
scopes: vec![],
scope_stack: vec![],
dead_scopes: vec![],
deferred_string_annotations: vec![],
deferred_annotations: vec![],
deferred_functions: vec![],
deferred_lambdas: vec![],
@@ -77,6 +80,7 @@ impl<'a> Checker<'a> {
seen_non_import: false,
seen_docstring: false,
futures_allowed: true,
annotations_future_enabled: false,
}
}
}
@@ -180,7 +184,7 @@ where
name.to_string(),
Binding {
kind: BindingKind::Assignment,
used: Some(global_scope_id),
used: Some((global_scope_id, stmt.location)),
location: stmt.location,
},
);
@@ -412,17 +416,22 @@ where
name,
Binding {
kind: BindingKind::FutureImportation,
used: Some(
used: Some((
self.scopes[*(self
.scope_stack
.last()
.expect("No current scope found."))]
.id,
),
stmt.location,
)),
location: stmt.location,
},
);
if alias.node.name == "annotations" {
self.annotations_future_enabled = true;
}
if self.settings.select.contains(&CheckCode::F407)
&& !ALL_FEATURE_NAMES.contains(&alias.node.name.deref())
{
@@ -472,7 +481,7 @@ where
let binding = Binding {
kind: BindingKind::Importation(match module {
None => name.clone(),
Some(parent) => format!("{}.{}", parent, name.clone()),
Some(parent) => format!("{}.{}", parent, name),
}),
used: None,
location: stmt.location,
@@ -581,6 +590,17 @@ where
let prev_in_literal = self.in_literal;
let prev_in_annotation = self.in_annotation;
// Important:
if self.in_annotation && self.annotations_future_enabled {
self.deferred_annotations.push((
expr,
self.scope_stack.clone(),
self.parent_stack.clone(),
));
visitor::walk_expr(self, expr);
return;
}
// Pre-visit.
match &expr.node {
ExprKind::Subscript { value, .. } => {
@@ -645,8 +665,7 @@ where
.settings
.select
.contains(CheckKind::YieldOutsideFunction.code())
&& matches!(scope.kind, ScopeKind::Class)
|| matches!(scope.kind, ScopeKind::Module)
&& matches!(scope.kind, ScopeKind::Class | ScopeKind::Module)
{
self.checks
.push(Check::new(CheckKind::YieldOutsideFunction, expr.location));
@@ -669,6 +688,28 @@ where
}
self.in_f_string = true;
}
ExprKind::BinOp {
left,
op: Operator::RShift,
..
} => {
if self.settings.select.contains(&CheckCode::F633) {
if let ExprKind::Name { id, .. } = &left.node {
if id == "print" {
let scope = &self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
if let Some(Binding {
kind: BindingKind::Builtin,
..
}) = scope.values.get("print")
{
self.checks
.push(Check::new(CheckKind::InvalidPrintSyntax, left.location));
}
}
}
}
}
ExprKind::UnaryOp { op, operand } => {
let check_not_in = self.settings.select.contains(&CheckCode::E713);
let check_not_is = self.settings.select.contains(&CheckCode::E714);
@@ -697,12 +738,30 @@ where
check_true_false_comparisons,
));
}
if self.settings.select.contains(&CheckCode::F632) {
self.checks.extend(checks::check_is_literal(
left,
ops,
comparators,
expr.location,
));
}
if self.settings.select.contains(&CheckCode::E721) {
self.checks.extend(checks::check_type_comparison(
ops,
comparators,
expr.location,
));
}
}
ExprKind::Constant {
value: Constant::Str(value),
..
} if self.in_annotation && !self.in_literal => {
self.deferred_annotations.push((expr.location, value));
self.deferred_string_annotations
.push((expr.location, value));
}
ExprKind::GeneratorExp { .. }
| ExprKind::ListComp { .. }
@@ -763,7 +822,7 @@ where
} else if match_name_or_attr(func, "NamedTuple") {
self.visit_expr(func);
// NamedTuple("a", [("a", int)])
// Ex) NamedTuple("a", [("a", int)])
if args.len() > 1 {
match &args[1].node {
ExprKind::List { elts, .. } | ExprKind::Tuple { elts, .. } => {
@@ -787,7 +846,7 @@ where
}
}
// NamedTuple("a", a=int)
// Ex) NamedTuple("a", a=int)
for keyword in keywords {
let KeywordData { value, .. } = &keyword.node;
self.visit_annotation(value);
@@ -795,7 +854,7 @@ where
} else if match_name_or_attr(func, "TypedDict") {
self.visit_expr(func);
// TypedDict("a", {"a": int})
// Ex) TypedDict("a", {"a": int})
if args.len() > 1 {
if let ExprKind::Dict { keys, values } = &args[1].node {
for key in keys {
@@ -809,7 +868,7 @@ where
}
}
// TypedDict("a", a=int)
// Ex) TypedDict("a", a=int)
for keyword in keywords {
let KeywordData { value, .. } = &keyword.node;
self.visit_annotation(value);
@@ -998,7 +1057,7 @@ impl<'a> Checker<'a> {
for builtin in BUILTINS {
scope.values.insert(
builtin.to_string(),
(*builtin).to_string(),
Binding {
kind: BindingKind::Builtin,
location: Default::default(),
@@ -1008,7 +1067,7 @@ impl<'a> Checker<'a> {
}
for builtin in MAGIC_GLOBALS {
scope.values.insert(
builtin.to_string(),
(*builtin).to_string(),
Binding {
kind: BindingKind::Builtin,
location: Default::default(),
@@ -1050,7 +1109,7 @@ impl<'a> Checker<'a> {
}
}
if let Some(binding) = scope.values.get_mut(id) {
binding.used = Some(scope_id);
binding.used = Some((scope_id, expr.location));
return;
}
@@ -1059,6 +1118,10 @@ impl<'a> Checker<'a> {
}
if self.settings.select.contains(&CheckCode::F821) {
// Allow __path__.
if self.path.ends_with("__init__.py") && id == "__path__" {
return;
}
self.checks.push(Check::new(
CheckKind::UndefinedName(id.clone()),
expr.location,
@@ -1077,20 +1140,15 @@ impl<'a> Checker<'a> {
&& !current.values.contains_key(id)
{
for scope in self.scopes.iter().rev().skip(1) {
if matches!(scope.kind, ScopeKind::Function)
|| matches!(scope.kind, ScopeKind::Module)
{
let used = scope
.values
.get(id)
.map(|binding| binding.used)
.unwrap_or_default();
if let Some(scope_id) = used {
if scope_id == current.id {
self.checks.push(Check::new(
CheckKind::UndefinedLocal(id.clone()),
expr.location,
));
if matches!(scope.kind, ScopeKind::Function | ScopeKind::Module) {
if let Some(binding) = scope.values.get(id) {
if let Some((scope_id, location)) = binding.used {
if scope_id == current.id {
self.checks.push(Check::new(
CheckKind::UndefinedLocal(id.clone()),
location,
));
}
}
}
}
@@ -1110,9 +1168,10 @@ impl<'a> Checker<'a> {
}
// TODO(charlie): Include comprehensions here.
if matches!(parent.node, StmtKind::For { .. })
|| matches!(parent.node, StmtKind::AsyncFor { .. })
|| operations::is_unpacking_assignment(parent)
if matches!(
parent.node,
StmtKind::For { .. } | StmtKind::AsyncFor { .. }
) || operations::is_unpacking_assignment(parent)
{
self.add_binding(
id.to_string(),
@@ -1127,9 +1186,12 @@ impl<'a> Checker<'a> {
if id == "__all__"
&& matches!(current.kind, ScopeKind::Module)
&& (matches!(parent.node, StmtKind::Assign { .. })
|| matches!(parent.node, StmtKind::AugAssign { .. })
|| matches!(parent.node, StmtKind::AnnAssign { .. }))
&& matches!(
parent.node,
StmtKind::Assign { .. }
| StmtKind::AugAssign { .. }
| StmtKind::AnnAssign { .. }
)
{
self.add_binding(
id.to_string(),
@@ -1172,13 +1234,20 @@ impl<'a> Checker<'a> {
}
}
fn check_deferred_annotations<'b>(&mut self, path: &str, allocator: &'b mut Vec<Expr>)
fn check_deferred_annotations(&mut self) {
while let Some((expr, scopes, parents)) = self.deferred_annotations.pop() {
self.parent_stack = parents;
self.scope_stack = scopes;
self.visit_expr(expr);
}
}
fn check_deferred_string_annotations<'b>(&mut self, allocator: &'b mut Vec<Expr>)
where
'b: 'a,
{
while !self.deferred_annotations.is_empty() {
let (location, expression) = self.deferred_annotations.pop().unwrap();
if let Ok(mut expr) = parser::parse_expression(expression, path) {
while let Some((location, expression)) = self.deferred_string_annotations.pop() {
if let Ok(mut expr) = parser::parse_expression(expression, "<filename>") {
relocate_expr(&mut expr, location);
allocator.push(expr);
} else if self.settings.select.contains(&CheckCode::F722) {
@@ -1194,9 +1263,7 @@ impl<'a> Checker<'a> {
}
fn check_deferred_functions(&mut self) {
while !self.deferred_functions.is_empty() {
let (stmt, scopes, parents) = self.deferred_functions.pop().unwrap();
while let Some((stmt, scopes, parents)) = self.deferred_functions.pop() {
self.parent_stack = parents;
self.scope_stack = scopes;
self.push_scope(Scope::new(ScopeKind::Function));
@@ -1220,9 +1287,7 @@ impl<'a> Checker<'a> {
}
fn check_deferred_lambdas(&mut self) {
while !self.deferred_lambdas.is_empty() {
let (expr, scopes, parents) = self.deferred_lambdas.pop().unwrap();
while let Some((expr, scopes, parents)) = self.deferred_lambdas.pop() {
self.parent_stack = parents;
self.scope_stack = scopes;
self.push_scope(Scope::new(ScopeKind::Function));
@@ -1240,8 +1305,7 @@ impl<'a> Checker<'a> {
}
fn check_deferred_assignments(&mut self) {
while !self.deferred_assignments.is_empty() {
let index = self.deferred_assignments.pop().unwrap();
while let Some(index) = self.deferred_assignments.pop() {
if self.settings.select.contains(&CheckCode::F841) {
self.checks
.extend(checks::check_unused_variables(&self.scopes[index]));
@@ -1256,7 +1320,7 @@ impl<'a> Checker<'a> {
return;
}
for index in self.dead_scopes.clone() {
for index in self.dead_scopes.iter().copied() {
let scope = &self.scopes[index];
let all_binding = scope.values.get("__all__");
@@ -1266,7 +1330,7 @@ impl<'a> Checker<'a> {
});
if self.settings.select.contains(&CheckCode::F822)
&& !Path::new(self.path).ends_with("__init__.py")
&& !self.path.ends_with("__init__.py")
{
if let Some(binding) = all_binding {
if let Some(names) = all_names {
@@ -1312,7 +1376,7 @@ pub fn check_ast(
content: &str,
settings: &Settings,
autofix: &fixer::Mode,
path: &str,
path: &Path,
) -> Vec<Check> {
let mut checker = Checker::new(settings, autofix, path, content);
checker.push_scope(Scope::new(ScopeKind::Module));
@@ -1327,8 +1391,9 @@ pub fn check_ast(
checker.check_deferred_functions();
checker.check_deferred_lambdas();
checker.check_deferred_assignments();
checker.check_deferred_annotations();
let mut allocator = vec![];
checker.check_deferred_annotations(path, &mut allocator);
checker.check_deferred_string_annotations(&mut allocator);
// Reset the scope to module-level, and check all consumed scopes.
checker.scope_stack = vec![GLOBAL_SCOPE_INDEX];

View File

@@ -14,12 +14,14 @@ pub enum CheckCode {
E712,
E713,
E714,
E721,
E722,
E731,
E741,
E742,
E743,
E902,
E999,
F401,
F403,
F404,
@@ -31,6 +33,8 @@ pub enum CheckCode {
F621,
F622,
F631,
F632,
F633,
F634,
F701,
F702,
@@ -58,13 +62,15 @@ impl FromStr for CheckCode {
"E711" => Ok(CheckCode::E711),
"E712" => Ok(CheckCode::E712),
"E713" => Ok(CheckCode::E713),
"E722" => Ok(CheckCode::E722),
"E714" => Ok(CheckCode::E714),
"E721" => Ok(CheckCode::E721),
"E722" => Ok(CheckCode::E722),
"E731" => Ok(CheckCode::E731),
"E741" => Ok(CheckCode::E741),
"E742" => Ok(CheckCode::E742),
"E743" => Ok(CheckCode::E743),
"E902" => Ok(CheckCode::E902),
"E999" => Ok(CheckCode::E999),
"F401" => Ok(CheckCode::F401),
"F403" => Ok(CheckCode::F403),
"F404" => Ok(CheckCode::F404),
@@ -76,12 +82,15 @@ impl FromStr for CheckCode {
"F621" => Ok(CheckCode::F621),
"F622" => Ok(CheckCode::F622),
"F631" => Ok(CheckCode::F631),
"F632" => Ok(CheckCode::F632),
"F633" => Ok(CheckCode::F633),
"F634" => Ok(CheckCode::F634),
"F701" => Ok(CheckCode::F701),
"F702" => Ok(CheckCode::F702),
"F704" => Ok(CheckCode::F704),
"F706" => Ok(CheckCode::F706),
"F707" => Ok(CheckCode::F707),
"F722" => Ok(CheckCode::F722),
"F821" => Ok(CheckCode::F821),
"F822" => Ok(CheckCode::F822),
"F823" => Ok(CheckCode::F823),
@@ -104,12 +113,14 @@ impl CheckCode {
CheckCode::E712 => "E712",
CheckCode::E713 => "E713",
CheckCode::E714 => "E714",
CheckCode::E721 => "E721",
CheckCode::E722 => "E722",
CheckCode::E731 => "E731",
CheckCode::E741 => "E741",
CheckCode::E742 => "E742",
CheckCode::E743 => "E743",
CheckCode::E902 => "E902",
CheckCode::E999 => "E999",
CheckCode::F401 => "F401",
CheckCode::F403 => "F403",
CheckCode::F404 => "F404",
@@ -121,6 +132,8 @@ impl CheckCode {
CheckCode::F621 => "F621",
CheckCode::F622 => "F622",
CheckCode::F631 => "F631",
CheckCode::F632 => "F632",
CheckCode::F633 => "F633",
CheckCode::F634 => "F634",
CheckCode::F701 => "F701",
CheckCode::F702 => "F702",
@@ -143,7 +156,7 @@ impl CheckCode {
pub fn lint_source(&self) -> &'static LintSource {
match self {
CheckCode::E501 => &LintSource::Lines,
CheckCode::E902 => &LintSource::FileSystem,
CheckCode::E902 | CheckCode::E999 => &LintSource::FileSystem,
_ => &LintSource::AST,
}
}
@@ -181,6 +194,8 @@ pub enum CheckKind {
IfTuple,
ImportStarNotPermitted(String),
ImportStarUsage(String),
InvalidPrintSyntax,
IsLiteral,
LateFutureImport,
LineTooLong(usize, usize),
ModuleImportNotAtTopOfFile,
@@ -192,9 +207,11 @@ pub enum CheckKind {
NotIsTest,
RaiseNotImplemented,
ReturnOutsideFunction,
SyntaxError(String),
TooManyExpressionsInStarredAssignment,
TrueFalseComparison(bool, RejectedCmpop),
TwoStarredExpressions,
TypeComparison,
UndefinedExport(String),
UndefinedLocal(String),
UndefinedName(String),
@@ -218,13 +235,15 @@ impl CheckKind {
CheckKind::DoNotAssignLambda => "DoNotAssignLambda",
CheckKind::DoNotUseBareExcept => "DoNotUseBareExcept",
CheckKind::DuplicateArgumentName => "DuplicateArgumentName",
CheckKind::ForwardAnnotationSyntaxError(_) => "ForwardAnnotationSyntaxError",
CheckKind::FStringMissingPlaceholders => "FStringMissingPlaceholders",
CheckKind::ForwardAnnotationSyntaxError(_) => "ForwardAnnotationSyntaxError",
CheckKind::FutureFeatureNotDefined(_) => "FutureFeatureNotDefined",
CheckKind::IOError(_) => "IOError",
CheckKind::IfTuple => "IfTuple",
CheckKind::ImportStarNotPermitted(_) => "ImportStarNotPermitted",
CheckKind::ImportStarUsage(_) => "ImportStarUsage",
CheckKind::InvalidPrintSyntax => "InvalidPrintSyntax",
CheckKind::IsLiteral => "IsLiteral",
CheckKind::LateFutureImport => "LateFutureImport",
CheckKind::LineTooLong(_, _) => "LineTooLong",
CheckKind::ModuleImportNotAtTopOfFile => "ModuleImportNotAtTopOfFile",
@@ -236,11 +255,13 @@ impl CheckKind {
CheckKind::NotIsTest => "NotIsTest",
CheckKind::RaiseNotImplemented => "RaiseNotImplemented",
CheckKind::ReturnOutsideFunction => "ReturnOutsideFunction",
CheckKind::SyntaxError(_) => "SyntaxError",
CheckKind::TooManyExpressionsInStarredAssignment => {
"TooManyExpressionsInStarredAssignment"
}
CheckKind::TrueFalseComparison(_, _) => "TrueFalseComparison",
CheckKind::TwoStarredExpressions => "TwoStarredExpressions",
CheckKind::TypeComparison => "TypeComparison",
CheckKind::UndefinedExport(_) => "UndefinedExport",
CheckKind::UndefinedLocal(_) => "UndefinedLocal",
CheckKind::UndefinedName(_) => "UndefinedName",
@@ -264,13 +285,15 @@ impl CheckKind {
CheckKind::DoNotAssignLambda => &CheckCode::E731,
CheckKind::DoNotUseBareExcept => &CheckCode::E722,
CheckKind::DuplicateArgumentName => &CheckCode::F831,
CheckKind::ForwardAnnotationSyntaxError(_) => &CheckCode::F722,
CheckKind::FStringMissingPlaceholders => &CheckCode::F541,
CheckKind::ForwardAnnotationSyntaxError(_) => &CheckCode::F722,
CheckKind::FutureFeatureNotDefined(_) => &CheckCode::F407,
CheckKind::IOError(_) => &CheckCode::E902,
CheckKind::IfTuple => &CheckCode::F634,
CheckKind::ImportStarNotPermitted(_) => &CheckCode::F406,
CheckKind::ImportStarUsage(_) => &CheckCode::F403,
CheckKind::InvalidPrintSyntax => &CheckCode::F633,
CheckKind::IsLiteral => &CheckCode::F632,
CheckKind::LateFutureImport => &CheckCode::F404,
CheckKind::LineTooLong(_, _) => &CheckCode::E501,
CheckKind::ModuleImportNotAtTopOfFile => &CheckCode::E402,
@@ -282,9 +305,11 @@ impl CheckKind {
CheckKind::NotIsTest => &CheckCode::E714,
CheckKind::RaiseNotImplemented => &CheckCode::F901,
CheckKind::ReturnOutsideFunction => &CheckCode::F706,
CheckKind::SyntaxError(_) => &CheckCode::E999,
CheckKind::TooManyExpressionsInStarredAssignment => &CheckCode::F621,
CheckKind::TrueFalseComparison(_, _) => &CheckCode::E712,
CheckKind::TwoStarredExpressions => &CheckCode::F622,
CheckKind::TypeComparison => &CheckCode::E721,
CheckKind::UndefinedExport(_) => &CheckCode::F822,
CheckKind::UndefinedLocal(_) => &CheckCode::F823,
CheckKind::UndefinedName(_) => &CheckCode::F821,
@@ -335,12 +360,14 @@ impl CheckKind {
format!("No such file or directory: `{name}`")
}
CheckKind::IfTuple => "If test is a tuple, which is always `True`".to_string(),
CheckKind::InvalidPrintSyntax => "use of >> is invalid with print function".to_string(),
CheckKind::ImportStarNotPermitted(name) => {
format!("`from {name} import *` only allowed at module level")
}
CheckKind::ImportStarUsage(name) => {
format!("`from {name} import *` used; unable to detect undefined names")
}
CheckKind::IsLiteral => "use ==/!= to compare constant literals".to_string(),
CheckKind::LateFutureImport => {
"from __future__ imports must occur at the beginning of the file".to_string()
}
@@ -373,6 +400,7 @@ impl CheckKind {
CheckKind::ReturnOutsideFunction => {
"a `return` statement outside of a function/method".to_string()
}
CheckKind::SyntaxError(message) => format!("SyntaxError: {message}"),
CheckKind::TooManyExpressionsInStarredAssignment => {
"too many expressions in star-unpacking assignment".to_string()
}
@@ -395,6 +423,7 @@ impl CheckKind {
},
},
CheckKind::TwoStarredExpressions => "two starred expressions in assignment".to_string(),
CheckKind::TypeComparison => "do not compare types, use `isinstance()`".to_string(),
CheckKind::UndefinedExport(name) => {
format!("Undefined name `{name}` in `__all__`")
}

134
src/fs.rs
View File

@@ -1,37 +1,86 @@
use std::env;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};
use anyhow::Result;
use glob::Pattern;
use log::debug;
use path_absolutize::Absolutize;
use walkdir::{DirEntry, WalkDir};
fn is_not_hidden(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| (entry.depth() == 0 || !s.starts_with('.')))
.unwrap_or(false)
fn is_excluded(path: &Path, exclude: &[Pattern]) -> bool {
// Check the basename.
if let Some(file_name) = path.file_name() {
if let Some(file_name) = file_name.to_str() {
for pattern in exclude {
if pattern.matches(file_name) {
return true;
}
}
}
}
// Check the complete path.
if let Some(file_name) = path.to_str() {
for pattern in exclude {
if pattern.matches(file_name) {
return true;
}
}
}
false
}
fn is_not_excluded(entry: &DirEntry, exclude: &[Pattern]) -> bool {
entry
.path()
.to_str()
.map(|s| !exclude.iter().any(|pattern| pattern.matches(s)))
.unwrap_or(false)
fn is_included(path: &Path) -> bool {
let file_name = path.to_string_lossy();
file_name.ends_with(".py") || file_name.ends_with(".pyi")
}
pub fn iter_python_files<'a>(
path: &'a PathBuf,
exclude: &'a [Pattern],
extend_exclude: &'a [Pattern],
) -> impl Iterator<Item = DirEntry> + 'a {
WalkDir::new(path)
WalkDir::new(normalize_path(path))
.follow_links(true)
.into_iter()
.filter_entry(|entry| is_not_hidden(entry) && is_not_excluded(entry, exclude))
.filter_entry(|entry| {
if exclude.is_empty() && extend_exclude.is_empty() {
return true;
}
let path = entry.path();
if is_excluded(path, exclude) {
debug!("Ignored path via `exclude`: {:?}", path);
false
} else if is_excluded(path, extend_exclude) {
debug!("Ignored path via `extend-exclude`: {:?}", path);
false
} else {
true
}
})
.filter_map(|entry| entry.ok())
.filter(|entry| entry.path().to_string_lossy().ends_with(".py"))
.filter(|entry| {
let path = entry.path();
is_included(path)
})
}
pub fn normalize_path(path: &PathBuf) -> PathBuf {
if path == Path::new(".") || path == Path::new("..") {
return path.clone();
}
if let Ok(path) = path.absolutize() {
if let Ok(root) = env::current_dir() {
if let Ok(path) = path.strip_prefix(root) {
return Path::new(".").join(path);
}
}
}
path.clone()
}
pub fn read_file(path: &Path) -> Result<String> {
@@ -41,3 +90,58 @@ pub fn read_file(path: &Path) -> Result<String> {
buf_reader.read_to_string(&mut contents)?;
Ok(contents)
}
#[cfg(test)]
mod tests {
use std::path::Path;
use glob::Pattern;
use crate::fs::{is_excluded, is_included};
#[test]
fn inclusions() {
let path = Path::new("foo/bar/baz.py");
assert!(is_included(path));
let path = Path::new("foo/bar/baz.pyi");
assert!(is_included(path));
let path = Path::new("foo/bar/baz.js");
assert!(!is_included(path));
let path = Path::new("foo/bar/baz");
assert!(!is_included(path));
}
#[test]
fn exclusions() {
let path = Path::new("foo");
let exclude = vec![Pattern::new("foo").unwrap()];
assert!(is_excluded(path, &exclude));
let path = Path::new("foo/bar");
let exclude = vec![Pattern::new("bar").unwrap()];
assert!(is_excluded(path, &exclude));
let path = Path::new("foo/bar/baz.py");
let exclude = vec![Pattern::new("baz.py").unwrap()];
assert!(is_excluded(path, &exclude));
let path = Path::new("foo/bar");
let exclude = vec![Pattern::new("foo/bar").unwrap()];
assert!(is_excluded(path, &exclude));
let path = Path::new("foo/bar/baz.py");
let exclude = vec![Pattern::new("foo/bar/baz.py").unwrap()];
assert!(is_excluded(path, &exclude));
let path = Path::new("foo/bar/baz.py");
let exclude = vec![Pattern::new("foo/bar/*.py").unwrap()];
assert!(is_excluded(path, &exclude));
let path = Path::new("foo/bar/baz.py");
let exclude = vec![Pattern::new("baz").unwrap()];
assert!(!is_excluded(path, &exclude));
}
}

View File

@@ -2,7 +2,7 @@ extern crate core;
mod ast;
mod autofix;
mod cache;
pub mod cache;
pub mod check_ast;
mod check_lines;
pub mod checks;
@@ -10,6 +10,7 @@ pub mod fs;
pub mod linter;
pub mod logging;
pub mod message;
pub mod printer;
mod pyproject;
mod python;
pub mod settings;

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::process::ExitCode;
use std::sync::mpsc::channel;
use std::time::Instant;
@@ -12,12 +12,14 @@ use notify::{raw_watcher, RecursiveMode, Watcher};
use rayon::prelude::*;
use walkdir::DirEntry;
use ::ruff::cache;
use ::ruff::checks::CheckCode;
use ::ruff::checks::CheckKind;
use ::ruff::fs::iter_python_files;
use ::ruff::linter::lint_path;
use ::ruff::logging::set_up_logging;
use ::ruff::message::Message;
use ::ruff::printer::{Printer, SerializationFormat};
use ::ruff::settings::Settings;
use ::ruff::tell_user;
@@ -54,9 +56,15 @@ struct Cli {
/// List of error codes to ignore.
#[clap(long, multiple = true)]
ignore: Vec<CheckCode>,
/// List of file and/or directory patterns to exclude from checks.
/// List of paths, used to exclude files and/or directories from checks.
#[clap(long, multiple = true)]
exclude: Vec<Pattern>,
/// Like --exclude, but adds additional files and directories on top of the excluded ones.
#[clap(long, multiple = true)]
extend_exclude: Vec<Pattern>,
/// Output serialization format for error messages.
#[clap(long, arg_enum, default_value_t=SerializationFormat::Text)]
format: SerializationFormat,
}
#[cfg(feature = "update-informer")]
@@ -93,7 +101,7 @@ fn run_once(
let start = Instant::now();
let paths: Vec<DirEntry> = files
.iter()
.flat_map(|path| iter_python_files(path, &settings.exclude))
.flat_map(|path| iter_python_files(path, &settings.exclude, &settings.extend_exclude))
.collect();
let duration = start.elapsed();
debug!("Identified files to lint in: {:?}", duration);
@@ -103,8 +111,17 @@ fn run_once(
.par_iter()
.map(|entry| {
lint_path(entry.path(), settings, &cache.into(), &autofix.into()).unwrap_or_else(|e| {
error!("Failed to check {}: {e:?}", entry.path().to_string_lossy());
vec![]
if settings.select.contains(&CheckCode::E999) {
vec![Message {
kind: CheckKind::SyntaxError(e.to_string()),
fixed: false,
location: Default::default(),
filename: entry.path().to_string_lossy().to_string(),
}]
} else {
error!("Failed to check {}: {e:?}", entry.path().to_string_lossy());
vec![]
}
})
})
.flatten()
@@ -130,62 +147,15 @@ fn run_once(
Ok(messages)
}
fn report_once(messages: &[Message]) -> Result<()> {
let (fixed, outstanding): (Vec<&Message>, Vec<&Message>) =
messages.iter().partition(|message| message.fixed);
let num_fixable = outstanding
.iter()
.filter(|message| message.kind.fixable())
.count();
if !outstanding.is_empty() {
for message in &outstanding {
println!("{}", message);
}
println!();
}
if !fixed.is_empty() {
println!(
"Found {} error(s) ({} fixed).",
outstanding.len(),
fixed.len()
);
} else {
println!("Found {} error(s).", outstanding.len());
}
if num_fixable > 0 {
println!("{num_fixable} potentially fixable with the --fix option.");
}
Ok(())
}
fn report_continuously(messages: &[Message]) -> Result<()> {
tell_user!(
"Found {} error(s). Watching for file changes.",
messages.len(),
);
if !messages.is_empty() {
println!();
for message in messages {
println!("{}", message);
}
}
Ok(())
}
fn inner_main() -> Result<ExitCode> {
let cli = Cli::parse();
set_up_logging(cli.verbose)?;
// TODO(charlie): Can we avoid this cast?
let paths: Vec<&Path> = cli.files.iter().map(PathBuf::as_path).collect();
let mut settings = Settings::from_paths(paths)?;
let mut settings = Settings::from_paths(&cli.files);
let mut printer = Printer::new(cli.format);
if !cli.select.is_empty() {
settings.select(cli.select);
}
@@ -193,21 +163,30 @@ fn inner_main() -> Result<ExitCode> {
settings.ignore(&cli.ignore);
}
if !cli.exclude.is_empty() {
settings.exclude(cli.exclude);
settings.exclude = cli.exclude;
}
if !cli.extend_exclude.is_empty() {
settings.extend_exclude = cli.extend_exclude;
}
cache::init()?;
if cli.watch {
if cli.fix {
println!("Warning: --fix is not enabled in watch mode.")
println!("Warning: --fix is not enabled in watch mode.");
}
if cli.format != SerializationFormat::Text {
println!("Warning: --format 'text' is used in watch mode.");
}
// Perform an initial run instantly.
clearscreen::clear()?;
printer.clear_screen()?;
tell_user!("Starting linter in watch mode...\n");
let messages = run_once(&cli.files, &settings, !cli.no_cache, false)?;
if !cli.quiet {
report_continuously(&messages)?;
printer.write_continuously(&messages)?;
}
// Configure the file watcher.
@@ -222,12 +201,12 @@ fn inner_main() -> Result<ExitCode> {
Ok(e) => {
if let Some(path) = e.path {
if path.to_string_lossy().ends_with(".py") {
clearscreen::clear()?;
printer.clear_screen()?;
tell_user!("File change detected...\n");
let messages = run_once(&cli.files, &settings, !cli.no_cache, false)?;
if !cli.quiet {
report_continuously(&messages)?;
printer.write_continuously(&messages)?;
}
}
}
@@ -238,7 +217,7 @@ fn inner_main() -> Result<ExitCode> {
} else {
let messages = run_once(&cli.files, &settings, !cli.no_cache, cli.fix)?;
if !cli.quiet {
report_once(&messages)?;
printer.write_once(&messages)?;
}
#[cfg(feature = "update-informer")]

80
src/printer.rs Normal file
View File

@@ -0,0 +1,80 @@
use colored::Colorize;
use anyhow::Result;
use clap::ValueEnum;
use crate::message::Message;
use crate::tell_user;
#[derive(Clone, Copy, ValueEnum, PartialEq, Eq, Debug)]
pub enum SerializationFormat {
Text,
Json,
}
pub struct Printer {
format: SerializationFormat,
}
impl Printer {
pub fn new(format: SerializationFormat) -> Self {
Self { format }
}
pub fn write_once(&mut self, messages: &[Message]) -> Result<()> {
let (fixed, outstanding): (Vec<&Message>, Vec<&Message>) =
messages.iter().partition(|message| message.fixed);
let num_fixable = outstanding
.iter()
.filter(|message| message.kind.fixable())
.count();
match self.format {
SerializationFormat::Json => {
println!("{}", serde_json::to_string_pretty(&messages)?)
}
SerializationFormat::Text => {
if !fixed.is_empty() {
println!(
"Found {} error(s) ({} fixed).",
outstanding.len(),
fixed.len()
)
} else {
println!("Found {} error(s).", outstanding.len())
}
for message in outstanding {
println!("{}", message)
}
if num_fixable > 0 {
println!("{num_fixable} potentially fixable with the --fix option.")
}
}
}
Ok(())
}
pub fn write_continuously(&mut self, messages: &[Message]) -> Result<()> {
tell_user!(
"Found {} error(s). Watching for file changes.",
messages.len(),
);
if !messages.is_empty() {
println!();
for message in messages {
println!("{}", message)
}
}
Ok(())
}
pub fn clear_screen(&mut self) -> Result<()> {
clearscreen::clear()?;
Ok(())
}
}

View File

@@ -8,29 +8,26 @@ use serde::Deserialize;
use crate::checks::CheckCode;
use crate::fs;
pub fn load_config<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<(PathBuf, Config)> {
pub fn load_config(paths: &[PathBuf]) -> Config {
match find_project_root(paths) {
Some(project_root) => match find_pyproject_toml(&project_root) {
Some(path) => {
debug!("Found pyproject.toml at: {}", path.to_string_lossy());
debug!("Found pyproject.toml at: {:?}", path);
match parse_pyproject_toml(&path) {
Ok(pyproject) => {
let config = pyproject
.tool
.and_then(|tool| tool.ruff)
.unwrap_or_default();
Ok((project_root, config))
}
Ok(pyproject) => pyproject
.tool
.and_then(|tool| tool.ruff)
.unwrap_or_default(),
Err(e) => {
println!("Failed to load pyproject.toml: {:?}", e);
println!("Falling back to default configuration...");
Ok(Default::default())
Default::default()
}
}
}
None => Ok(Default::default()),
None => Default::default(),
},
None => Ok(Default::default()),
None => Default::default(),
}
}
@@ -39,6 +36,7 @@ pub fn load_config<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<(Pat
pub struct Config {
pub line_length: Option<usize>,
pub exclude: Option<Vec<PathBuf>>,
pub extend_exclude: Option<Vec<PathBuf>>,
pub select: Option<Vec<CheckCode>>,
pub ignore: Option<Vec<CheckCode>>,
}
@@ -70,8 +68,8 @@ fn find_user_pyproject_toml() -> Option<PathBuf> {
dirs::home_dir().map(|path| path.join(".ruff"))
}
fn find_project_root<'a>(sources: impl IntoIterator<Item = &'a Path>) -> Option<PathBuf> {
if let Some(prefix) = common_path_all(sources) {
fn find_project_root(sources: &[PathBuf]) -> Option<PathBuf> {
if let Some(prefix) = common_path_all(sources.iter().map(PathBuf::as_path)) {
for directory in prefix.ancestors() {
if directory.join(".git").is_dir() {
return Some(directory.to_path_buf());
@@ -90,7 +88,7 @@ fn find_project_root<'a>(sources: impl IntoIterator<Item = &'a Path>) -> Option<
#[cfg(test)]
mod tests {
use std::path::Path;
use std::path::{Path, PathBuf};
use anyhow::Result;
@@ -123,6 +121,7 @@ mod tests {
ruff: Some(Config {
line_length: None,
exclude: None,
extend_exclude: None,
select: None,
ignore: None,
})
@@ -142,6 +141,7 @@ line-length = 79
ruff: Some(Config {
line_length: Some(79),
exclude: None,
extend_exclude: None,
select: None,
ignore: None,
})
@@ -161,6 +161,7 @@ exclude = ["foo.py"]
ruff: Some(Config {
line_length: None,
exclude: Some(vec![Path::new("foo.py").to_path_buf()]),
extend_exclude: None,
select: None,
ignore: None,
})
@@ -180,6 +181,7 @@ select = ["E501"]
ruff: Some(Config {
line_length: None,
exclude: None,
extend_exclude: None,
select: Some(vec![CheckCode::E501]),
ignore: None,
})
@@ -199,6 +201,7 @@ ignore = ["E501"]
ruff: Some(Config {
line_length: None,
exclude: None,
extend_exclude: None,
select: None,
ignore: Some(vec![CheckCode::E501]),
})
@@ -238,8 +241,9 @@ other-attribute = 1
#[test]
fn find_and_parse_pyproject_toml() -> Result<()> {
let project_root = find_project_root([Path::new("resources/test/fixtures/__init__.py")])
.expect("Unable to find project root.");
let project_root =
find_project_root(&[PathBuf::from("resources/test/fixtures/__init__.py")])
.expect("Unable to find project root.");
assert_eq!(project_root, Path::new("resources/test/fixtures"));
let path = find_pyproject_toml(&project_root).expect("Unable to find pyproject.toml.");
@@ -254,9 +258,11 @@ other-attribute = 1
config,
Config {
line_length: Some(88),
exclude: Some(vec![
exclude: None,
extend_exclude: Some(vec![
Path::new("excluded.py").to_path_buf(),
Path::new("**/migrations").to_path_buf()
Path::new("migrations").to_path_buf(),
Path::new("./resources/test/fixtures/directory/also_excluded.py").to_path_buf()
]),
select: Some(vec![
CheckCode::E402,
@@ -265,12 +271,14 @@ other-attribute = 1
CheckCode::E712,
CheckCode::E713,
CheckCode::E714,
CheckCode::E721,
CheckCode::E722,
CheckCode::E731,
CheckCode::E741,
CheckCode::E742,
CheckCode::E743,
CheckCode::E902,
CheckCode::E999,
CheckCode::F401,
CheckCode::F403,
CheckCode::F404,
@@ -282,6 +290,8 @@ other-attribute = 1
CheckCode::F621,
CheckCode::F622,
CheckCode::F631,
CheckCode::F632,
CheckCode::F633,
CheckCode::F634,
CheckCode::F701,
CheckCode::F702,

View File

@@ -1,8 +1,8 @@
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use std::collections::BTreeSet;
lazy_static! {
static ref ANNOTATED_SUBSCRIPTS: BTreeSet<&'static str> = BTreeSet::from([
static ANNOTATED_SUBSCRIPTS: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
BTreeSet::from([
"AbstractAsyncContextManager",
"AbstractContextManager",
"AbstractSet",
@@ -80,8 +80,8 @@ lazy_static! {
"set",
"tuple",
"type",
]);
}
])
});
pub fn is_annotated_subscript(name: &str) -> bool {
ANNOTATED_SUBSCRIPTS.contains(name)

View File

@@ -1,9 +1,9 @@
use std::collections::BTreeSet;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::path::PathBuf;
use anyhow::Result;
use glob::Pattern;
use once_cell::sync::Lazy;
use crate::checks::CheckCode;
use crate::pyproject::load_config;
@@ -12,6 +12,7 @@ use crate::pyproject::load_config;
pub struct Settings {
pub line_length: usize,
pub exclude: Vec<Pattern>,
pub extend_exclude: Vec<Pattern>,
pub select: BTreeSet<CheckCode>,
}
@@ -24,24 +25,57 @@ impl Hash for Settings {
}
}
static DEFAULT_EXCLUDE: Lazy<Vec<Pattern>> = Lazy::new(|| {
vec![
Pattern::new(".bzr").unwrap(),
Pattern::new(".direnv").unwrap(),
Pattern::new(".eggs").unwrap(),
Pattern::new(".git").unwrap(),
Pattern::new(".hg").unwrap(),
Pattern::new(".mypy_cache").unwrap(),
Pattern::new(".nox").unwrap(),
Pattern::new(".pants.d").unwrap(),
Pattern::new(".ruff_cache").unwrap(),
Pattern::new(".svn").unwrap(),
Pattern::new(".tox").unwrap(),
Pattern::new(".venv").unwrap(),
Pattern::new("__pypackages__").unwrap(),
Pattern::new("_build").unwrap(),
Pattern::new("buck-out").unwrap(),
Pattern::new("build").unwrap(),
Pattern::new("dist").unwrap(),
Pattern::new("node_modules").unwrap(),
Pattern::new("venv").unwrap(),
]
});
impl Settings {
pub fn from_paths<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<Self> {
let (project_root, config) = load_config(paths)?;
pub fn from_paths(paths: &[PathBuf]) -> Self {
let config = load_config(paths);
let mut settings = Settings {
line_length: config.line_length.unwrap_or(88),
exclude: config
.exclude
.unwrap_or_default()
.into_iter()
.map(|path| {
if path.is_relative() {
project_root.join(path)
} else {
path
}
.map(|paths| {
paths
.iter()
.map(|path| {
Pattern::new(&path.to_string_lossy()).expect("Invalid pattern.")
})
.collect()
})
.map(|path| Pattern::new(&path.to_string_lossy()).expect("Invalid pattern."))
.collect(),
.unwrap_or_else(|| DEFAULT_EXCLUDE.clone()),
extend_exclude: config
.extend_exclude
.map(|paths| {
paths
.iter()
.map(|path| {
Pattern::new(&path.to_string_lossy()).expect("Invalid pattern.")
})
.collect()
})
.unwrap_or_default(),
select: BTreeSet::from_iter(config.select.unwrap_or_else(|| {
vec![
CheckCode::E402,
@@ -50,12 +84,14 @@ impl Settings {
CheckCode::E712,
CheckCode::E713,
CheckCode::E714,
CheckCode::E721,
CheckCode::E722,
CheckCode::E731,
CheckCode::E741,
CheckCode::E742,
CheckCode::E743,
CheckCode::E902,
CheckCode::E999,
CheckCode::F401,
CheckCode::F403,
CheckCode::F406,
@@ -66,6 +102,8 @@ impl Settings {
CheckCode::F621,
CheckCode::F622,
CheckCode::F631,
CheckCode::F632,
CheckCode::F633,
CheckCode::F634,
CheckCode::F701,
CheckCode::F702,
@@ -88,7 +126,7 @@ impl Settings {
if let Some(ignore) = &config.ignore {
settings.ignore(ignore);
}
Ok(settings)
settings
}
pub fn select(&mut self, codes: Vec<CheckCode>) {
@@ -103,8 +141,4 @@ impl Settings {
self.select.remove(code);
}
}
pub fn exclude(&mut self, exclude: Vec<Pattern>) {
self.exclude = exclude;
}
}

View File

@@ -0,0 +1,10 @@
---
source: src/linter.rs
expression: checks
---
- kind: ModuleImportNotAtTopOfFile
location:
row: 20
column: 1
fix: ~

View File

@@ -0,0 +1,13 @@
---
source: src/linter.rs
expression: checks
---
- kind:
LineTooLong:
- 123
- 88
location:
row: 5
column: 89
fix: ~

View File

@@ -0,0 +1,53 @@
---
source: src/linter.rs
expression: checks
---
- kind:
NoneComparison: Eq
location:
row: 2
column: 11
fix: ~
- kind:
NoneComparison: NotEq
location:
row: 5
column: 11
fix: ~
- kind:
NoneComparison: Eq
location:
row: 8
column: 4
fix: ~
- kind:
NoneComparison: NotEq
location:
row: 11
column: 4
fix: ~
- kind:
NoneComparison: Eq
location:
row: 14
column: 14
fix: ~
- kind:
NoneComparison: NotEq
location:
row: 17
column: 14
fix: ~
- kind:
NoneComparison: NotEq
location:
row: 20
column: 4
fix: ~
- kind:
NoneComparison: Eq
location:
row: 23
column: 4
fix: ~

View File

@@ -0,0 +1,77 @@
---
source: src/linter.rs
expression: checks
---
- kind:
TrueFalseComparison:
- true
- Eq
location:
row: 2
column: 11
fix: ~
- kind:
TrueFalseComparison:
- false
- NotEq
location:
row: 5
column: 11
fix: ~
- kind:
TrueFalseComparison:
- true
- NotEq
location:
row: 8
column: 4
fix: ~
- kind:
TrueFalseComparison:
- false
- Eq
location:
row: 11
column: 4
fix: ~
- kind:
TrueFalseComparison:
- true
- Eq
location:
row: 14
column: 14
fix: ~
- kind:
TrueFalseComparison:
- false
- NotEq
location:
row: 17
column: 14
fix: ~
- kind:
TrueFalseComparison:
- true
- Eq
location:
row: 20
column: 20
fix: ~
- kind:
TrueFalseComparison:
- false
- Eq
location:
row: 20
column: 44
fix: ~
- kind:
TrueFalseComparison:
- true
- Eq
location:
row: 22
column: 5
fix: ~

View File

@@ -0,0 +1,30 @@
---
source: src/linter.rs
expression: checks
---
- kind: NotInTest
location:
row: 2
column: 10
fix: ~
- kind: NotInTest
location:
row: 5
column: 12
fix: ~
- kind: NotInTest
location:
row: 8
column: 10
fix: ~
- kind: NotInTest
location:
row: 11
column: 25
fix: ~
- kind: NotInTest
location:
row: 14
column: 11
fix: ~

View File

@@ -0,0 +1,20 @@
---
source: src/linter.rs
expression: checks
---
- kind: NotIsTest
location:
row: 2
column: 10
fix: ~
- kind: NotIsTest
location:
row: 5
column: 12
fix: ~
- kind: NotIsTest
location:
row: 8
column: 10
fix: ~

View File

@@ -0,0 +1,85 @@
---
source: src/linter.rs
expression: checks
---
- kind: TypeComparison
location:
row: 2
column: 14
fix: ~
- kind: TypeComparison
location:
row: 5
column: 14
fix: ~
- kind: TypeComparison
location:
row: 10
column: 8
fix: ~
- kind: TypeComparison
location:
row: 15
column: 14
fix: ~
- kind: TypeComparison
location:
row: 18
column: 18
fix: ~
- kind: TypeComparison
location:
row: 18
column: 46
fix: ~
- kind: TypeComparison
location:
row: 20
column: 18
fix: ~
- kind: TypeComparison
location:
row: 22
column: 18
fix: ~
- kind: TypeComparison
location:
row: 24
column: 18
fix: ~
- kind: TypeComparison
location:
row: 26
column: 18
fix: ~
- kind: TypeComparison
location:
row: 28
column: 18
fix: ~
- kind: TypeComparison
location:
row: 30
column: 18
fix: ~
- kind: TypeComparison
location:
row: 32
column: 18
fix: ~
- kind: TypeComparison
location:
row: 34
column: 18
fix: ~
- kind: TypeComparison
location:
row: 40
column: 18
fix: ~
- kind: TypeComparison
location:
row: 42
column: 18
fix: ~

View File

@@ -0,0 +1,20 @@
---
source: src/linter.rs
expression: checks
---
- kind: DoNotUseBareExcept
location:
row: 4
column: 1
fix: ~
- kind: DoNotUseBareExcept
location:
row: 11
column: 1
fix: ~
- kind: DoNotUseBareExcept
location:
row: 16
column: 1
fix: ~

View File

@@ -0,0 +1,30 @@
---
source: src/linter.rs
expression: checks
---
- kind: DoNotAssignLambda
location:
row: 2
column: 1
fix: ~
- kind: DoNotAssignLambda
location:
row: 4
column: 1
fix: ~
- kind: DoNotAssignLambda
location:
row: 7
column: 5
fix: ~
- kind: DoNotAssignLambda
location:
row: 12
column: 1
fix: ~
- kind: DoNotAssignLambda
location:
row: 16
column: 1
fix: ~

View File

@@ -0,0 +1,155 @@
---
source: src/linter.rs
expression: checks
---
- kind:
AmbiguousVariableName: l
location:
row: 3
column: 1
fix: ~
- kind:
AmbiguousVariableName: I
location:
row: 4
column: 1
fix: ~
- kind:
AmbiguousVariableName: O
location:
row: 5
column: 1
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 6
column: 1
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 8
column: 4
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 9
column: 5
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 10
column: 5
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 11
column: 5
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 16
column: 5
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 20
column: 8
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 25
column: 5
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 26
column: 5
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 30
column: 5
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 33
column: 9
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 34
column: 9
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 40
column: 8
fix: ~
- kind:
AmbiguousVariableName: I
location:
row: 40
column: 14
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 44
column: 8
fix: ~
- kind:
AmbiguousVariableName: I
location:
row: 44
column: 16
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 48
column: 9
fix: ~
- kind:
AmbiguousVariableName: I
location:
row: 48
column: 14
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 57
column: 16
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 66
column: 20
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 71
column: 1
fix: ~
- kind:
AmbiguousVariableName: l
location:
row: 74
column: 5
fix: ~

View File

@@ -0,0 +1,23 @@
---
source: src/linter.rs
expression: checks
---
- kind:
AmbiguousClassName: l
location:
row: 1
column: 1
fix: ~
- kind:
AmbiguousClassName: I
location:
row: 5
column: 1
fix: ~
- kind:
AmbiguousClassName: O
location:
row: 9
column: 1
fix: ~

View File

@@ -0,0 +1,23 @@
---
source: src/linter.rs
expression: checks
---
- kind:
AmbiguousFunctionName: l
location:
row: 1
column: 1
fix: ~
- kind:
AmbiguousFunctionName: I
location:
row: 5
column: 1
fix: ~
- kind:
AmbiguousFunctionName: O
location:
row: 10
column: 5
fix: ~

View File

@@ -0,0 +1,23 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UnusedImport: functools
location:
row: 3
column: 1
fix: ~
- kind:
UnusedImport: collections.OrderedDict
location:
row: 5
column: 1
fix: ~
- kind:
UnusedImport: logging.handlers
location:
row: 13
column: 1
fix: ~

View File

@@ -0,0 +1,17 @@
---
source: src/linter.rs
expression: checks
---
- kind:
ImportStarUsage: F634
location:
row: 1
column: 1
fix: ~
- kind:
ImportStarUsage: F634
location:
row: 2
column: 1
fix: ~

View File

@@ -0,0 +1,10 @@
---
source: src/linter.rs
expression: checks
---
- kind: LateFutureImport
location:
row: 7
column: 1
fix: ~

View File

@@ -0,0 +1,17 @@
---
source: src/linter.rs
expression: checks
---
- kind:
ImportStarNotPermitted: F634
location:
row: 5
column: 5
fix: ~
- kind:
ImportStarNotPermitted: F634
location:
row: 9
column: 5
fix: ~

View File

@@ -0,0 +1,11 @@
---
source: src/linter.rs
expression: checks
---
- kind:
FutureFeatureNotDefined: non_existent_feature
location:
row: 2
column: 1
fix: ~

View File

@@ -0,0 +1,20 @@
---
source: src/linter.rs
expression: checks
---
- kind: FStringMissingPlaceholders
location:
row: 4
column: 7
fix: ~
- kind: FStringMissingPlaceholders
location:
row: 5
column: 7
fix: ~
- kind: FStringMissingPlaceholders
location:
row: 7
column: 7
fix: ~

View File

@@ -0,0 +1,20 @@
---
source: src/linter.rs
expression: checks
---
- kind: MultiValueRepeatedKeyLiteral
location:
row: 3
column: 6
fix: ~
- kind: MultiValueRepeatedKeyLiteral
location:
row: 9
column: 5
fix: ~
- kind: MultiValueRepeatedKeyLiteral
location:
row: 11
column: 7
fix: ~

View File

@@ -0,0 +1,11 @@
---
source: src/linter.rs
expression: checks
---
- kind:
MultiValueRepeatedKeyVariable: a
location:
row: 5
column: 5
fix: ~

View File

@@ -0,0 +1,10 @@
---
source: src/linter.rs
expression: checks
---
- kind: TwoStarredExpressions
location:
row: 1
column: 1
fix: ~

View File

@@ -0,0 +1,15 @@
---
source: src/linter.rs
expression: checks
---
- kind: AssertTuple
location:
row: 1
column: 1
fix: ~
- kind: AssertTuple
location:
row: 2
column: 1
fix: ~

View File

@@ -0,0 +1,15 @@
---
source: src/linter.rs
expression: checks
---
- kind: IsLiteral
location:
row: 1
column: 6
fix: ~
- kind: IsLiteral
location:
row: 4
column: 8
fix: ~

View File

@@ -0,0 +1,10 @@
---
source: src/linter.rs
expression: checks
---
- kind: InvalidPrintSyntax
location:
row: 4
column: 1
fix: ~

View File

@@ -0,0 +1,15 @@
---
source: src/linter.rs
expression: checks
---
- kind: IfTuple
location:
row: 1
column: 1
fix: ~
- kind: IfTuple
location:
row: 7
column: 5
fix: ~

View File

@@ -0,0 +1,25 @@
---
source: src/linter.rs
expression: checks
---
- kind: BreakOutsideLoop
location:
row: 4
column: 5
fix: ~
- kind: BreakOutsideLoop
location:
row: 16
column: 5
fix: ~
- kind: BreakOutsideLoop
location:
row: 20
column: 5
fix: ~
- kind: BreakOutsideLoop
location:
row: 23
column: 1
fix: ~

View File

@@ -0,0 +1,25 @@
---
source: src/linter.rs
expression: checks
---
- kind: ContinueOutsideLoop
location:
row: 4
column: 5
fix: ~
- kind: ContinueOutsideLoop
location:
row: 16
column: 5
fix: ~
- kind: ContinueOutsideLoop
location:
row: 20
column: 5
fix: ~
- kind: ContinueOutsideLoop
location:
row: 23
column: 1
fix: ~

View File

@@ -0,0 +1,20 @@
---
source: src/linter.rs
expression: checks
---
- kind: YieldOutsideFunction
location:
row: 6
column: 5
fix: ~
- kind: YieldOutsideFunction
location:
row: 9
column: 1
fix: ~
- kind: YieldOutsideFunction
location:
row: 10
column: 1
fix: ~

View File

@@ -0,0 +1,15 @@
---
source: src/linter.rs
expression: checks
---
- kind: ReturnOutsideFunction
location:
row: 6
column: 5
fix: ~
- kind: ReturnOutsideFunction
location:
row: 9
column: 1
fix: ~

View File

@@ -0,0 +1,20 @@
---
source: src/linter.rs
expression: checks
---
- kind: DefaultExceptNotLast
location:
row: 3
column: 1
fix: ~
- kind: DefaultExceptNotLast
location:
row: 10
column: 1
fix: ~
- kind: DefaultExceptNotLast
location:
row: 19
column: 1
fix: ~

View File

@@ -0,0 +1,11 @@
---
source: src/linter.rs
expression: checks
---
- kind:
ForwardAnnotationSyntaxError: ///
location:
row: 9
column: 13
fix: ~

View File

@@ -0,0 +1,41 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UndefinedName: self
location:
row: 2
column: 12
fix: ~
- kind:
UndefinedName: self
location:
row: 6
column: 13
fix: ~
- kind:
UndefinedName: self
location:
row: 10
column: 9
fix: ~
- kind:
UndefinedName: numeric_string
location:
row: 21
column: 12
fix: ~
- kind:
UndefinedName: Bar
location:
row: 58
column: 5
fix: ~
- kind:
UndefinedName: TOMATO
location:
row: 83
column: 11
fix: ~

View File

@@ -0,0 +1,11 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UndefinedExport: b
location:
row: 3
column: 1
fix: ~

View File

@@ -0,0 +1,11 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UndefinedLocal: my_var
location:
row: 6
column: 5
fix: ~

View File

@@ -0,0 +1,20 @@
---
source: src/linter.rs
expression: checks
---
- kind: DuplicateArgumentName
location:
row: 1
column: 25
fix: ~
- kind: DuplicateArgumentName
location:
row: 5
column: 28
fix: ~
- kind: DuplicateArgumentName
location:
row: 9
column: 27
fix: ~

View File

@@ -0,0 +1,35 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UnusedVariable: e
location:
row: 3
column: 1
fix: ~
- kind:
UnusedVariable: z
location:
row: 16
column: 5
fix: ~
- kind:
UnusedVariable: foo
location:
row: 20
column: 5
fix: ~
- kind:
UnusedVariable: a
location:
row: 21
column: 6
fix: ~
- kind:
UnusedVariable: b
location:
row: 21
column: 9
fix: ~

View File

@@ -0,0 +1,15 @@
---
source: src/linter.rs
expression: checks
---
- kind: RaiseNotImplemented
location:
row: 2
column: 25
fix: ~
- kind: RaiseNotImplemented
location:
row: 6
column: 11
fix: ~

View File

@@ -0,0 +1,17 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UnusedImport: models.Nut
location:
row: 5
column: 1
fix: ~
- kind:
UndefinedName: Bar
location:
row: 22
column: 19
fix: ~

View File

@@ -0,0 +1,6 @@
---
source: src/linter.rs
expression: checks
---
[]

View File

@@ -0,0 +1,285 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UselessObjectInheritance: A
location:
row: 5
column: 9
fix:
content: ""
start:
row: 5
column: 8
end:
row: 5
column: 16
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 10
column: 5
fix:
content: ""
start:
row: 9
column: 8
end:
row: 11
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 16
column: 5
fix:
content: ""
start:
row: 15
column: 8
end:
row: 18
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 24
column: 5
fix:
content: ""
start:
row: 22
column: 8
end:
row: 25
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 31
column: 5
fix:
content: ""
start:
row: 29
column: 8
end:
row: 32
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 37
column: 5
fix:
content: ""
start:
row: 36
column: 8
end:
row: 39
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 45
column: 5
fix:
content: ""
start:
row: 43
column: 8
end:
row: 47
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 53
column: 5
fix:
content: ""
start:
row: 51
column: 8
end:
row: 55
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 61
column: 5
fix:
content: ""
start:
row: 59
column: 8
end:
row: 63
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 69
column: 5
fix:
content: ""
start:
row: 67
column: 8
end:
row: 71
column: 2
applied: false
- kind:
UselessObjectInheritance: B
location:
row: 75
column: 12
fix:
content: ""
start:
row: 75
column: 10
end:
row: 75
column: 18
applied: false
- kind:
UselessObjectInheritance: B
location:
row: 79
column: 9
fix:
content: ""
start:
row: 79
column: 9
end:
row: 79
column: 17
applied: false
- kind:
UselessObjectInheritance: B
location:
row: 84
column: 5
fix:
content: ""
start:
row: 84
column: 5
end:
row: 85
column: 5
applied: false
- kind:
UselessObjectInheritance: B
location:
row: 92
column: 5
fix:
content: ""
start:
row: 91
column: 6
end:
row: 92
column: 11
applied: false
- kind:
UselessObjectInheritance: B
location:
row: 98
column: 5
fix:
content: ""
start:
row: 98
column: 5
end:
row: 100
column: 5
applied: false
- kind:
UselessObjectInheritance: B
location:
row: 108
column: 5
fix:
content: ""
start:
row: 107
column: 6
end:
row: 108
column: 11
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 114
column: 13
fix:
content: ""
start:
row: 114
column: 12
end:
row: 114
column: 20
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 119
column: 5
fix:
content: ""
start:
row: 118
column: 8
end:
row: 120
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 125
column: 5
fix:
content: ""
start:
row: 124
column: 8
end:
row: 126
column: 2
applied: false
- kind:
UselessObjectInheritance: A
location:
row: 131
column: 5
fix:
content: ""
start:
row: 130
column: 8
end:
row: 133
column: 2
applied: false

View File

@@ -0,0 +1,31 @@
---
source: src/linter.rs
expression: checks
---
- kind: NoAssertEquals
location:
row: 1
column: 5
fix:
content: assertEqual
start:
row: 1
column: 6
end:
row: 1
column: 18
applied: false
- kind: NoAssertEquals
location:
row: 2
column: 5
fix:
content: assertEqual
start:
row: 2
column: 6
end:
row: 2
column: 18
applied: false