Compare commits
20 Commits
v0.0.71
...
charlie/ch
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
87ab4e5c16 | ||
|
|
e73f13473d | ||
|
|
3e8ef5b40f | ||
|
|
c59610906c | ||
|
|
2353a52be8 | ||
|
|
bbffdd57ff | ||
|
|
3c15c578a7 | ||
|
|
6a8e31b2ff | ||
|
|
6407fd5a33 | ||
|
|
b64040cbb2 | ||
|
|
952a0eb4e3 | ||
|
|
3e28d6de04 | ||
|
|
9bbfd1d3b2 | ||
|
|
6fb82ab763 | ||
|
|
6b286e9bc1 | ||
|
|
bcddd9e97f | ||
|
|
3e789136af | ||
|
|
07ef3b8754 | ||
|
|
46e1b16472 | ||
|
|
720bfe0161 |
62
Cargo.lock
generated
62
Cargo.lock
generated
@@ -981,6 +981,9 @@ name = "hashbrown"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
@@ -1966,7 +1969,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ruff"
|
||||
version = "0.0.71"
|
||||
version = "0.0.75"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"assert_cmd",
|
||||
@@ -1998,6 +2001,8 @@ dependencies = [
|
||||
"serde_json",
|
||||
"strum",
|
||||
"strum_macros",
|
||||
"test-case",
|
||||
"textwrap",
|
||||
"titlecase",
|
||||
"toml",
|
||||
"update-informer",
|
||||
@@ -2260,6 +2265,12 @@ version = "1.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
|
||||
|
||||
[[package]]
|
||||
name = "smawk"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043"
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.4.7"
|
||||
@@ -2412,6 +2423,39 @@ version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b"
|
||||
|
||||
[[package]]
|
||||
name = "test-case"
|
||||
version = "2.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21d6cf5a7dffb3f9dceec8e6b8ca528d9bd71d36c9f074defb548ce161f598c0"
|
||||
dependencies = [
|
||||
"test-case-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "test-case-macros"
|
||||
version = "2.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e45b7bf6e19353ddd832745c8fcf77a17a93171df7151187f26623f2b75b5b26"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.15.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16"
|
||||
dependencies = [
|
||||
"smawk",
|
||||
"unicode-linebreak",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.37"
|
||||
@@ -2579,6 +2623,16 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-linebreak"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c5faade31a542b8b35855fff6e8def199853b2da8da256da52f52f1316ee3137"
|
||||
dependencies = [
|
||||
"hashbrown",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-normalization"
|
||||
version = "0.1.22"
|
||||
@@ -2588,6 +2642,12 @@ dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.4"
|
||||
|
||||
16
Cargo.toml
16
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ruff"
|
||||
version = "0.0.71"
|
||||
version = "0.0.75"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
@@ -23,26 +23,28 @@ itertools = { version = "0.10.5" }
|
||||
libcst = { git = "https://github.com/charliermarsh/LibCST", rev = "32a044c127668df44582f85699358e67803b0d73" }
|
||||
log = { version = "0.4.17" }
|
||||
notify = { version = "4.0.17" }
|
||||
num-bigint = { version = "0.4.3" }
|
||||
once_cell = { version = "1.13.1" }
|
||||
path-absolutize = { version = "3.0.13", features = ["once_cell_cache"] }
|
||||
rayon = { version = "1.5.3" }
|
||||
regex = { version = "1.6.0" }
|
||||
rustpython-ast = { features = ["unparse"], git = "https://github.com/charliermarsh/RustPython.git", rev = "778ae2aeb521d0438d2a91bd11238bb5c2bf9d4f" }
|
||||
rustpython-parser = { features = ["lalrpop"], git = "https://github.com/charliermarsh/RustPython.git", rev = "778ae2aeb521d0438d2a91bd11238bb5c2bf9d4f" }
|
||||
rustpython-common = { git = "https://github.com/charliermarsh/RustPython.git", rev = "778ae2aeb521d0438d2a91bd11238bb5c2bf9d4f" }
|
||||
rustpython-parser = { features = ["lalrpop"], git = "https://github.com/charliermarsh/RustPython.git", rev = "778ae2aeb521d0438d2a91bd11238bb5c2bf9d4f" }
|
||||
serde = { version = "1.0.143", features = ["derive"] }
|
||||
serde_json = { version = "1.0.83" }
|
||||
strum = { version = "0.24.1", features = ["strum_macros"] }
|
||||
strum_macros = { version = "0.24.3" }
|
||||
textwrap = { version = "0.15.1" }
|
||||
titlecase = { version = "2.2.1" }
|
||||
toml = { version = "0.5.9" }
|
||||
update-informer = { version = "0.5.0", default_features = false, features = ["pypi"], optional = true }
|
||||
walkdir = { version = "2.3.2" }
|
||||
strum = { version = "0.24.1", features = ["strum_macros"] }
|
||||
strum_macros = "0.24.3"
|
||||
num-bigint = "0.4.3"
|
||||
titlecase = "2.2.1"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_cmd = "2.0.4"
|
||||
assert_cmd = { version = "2.0.4" }
|
||||
insta = { version = "1.19.1", features = ["yaml"] }
|
||||
test-case = { version = "2.2.2" }
|
||||
|
||||
[features]
|
||||
default = ["update-informer"]
|
||||
|
||||
44
README.md
44
README.md
@@ -57,7 +57,7 @@ ruff also works with [pre-commit](https://pre-commit.com):
|
||||
```yaml
|
||||
repos:
|
||||
- repo: https://github.com/charliermarsh/ruff-pre-commit
|
||||
rev: v0.0.71
|
||||
rev: v0.0.75
|
||||
hooks:
|
||||
- id: lint
|
||||
```
|
||||
@@ -94,6 +94,8 @@ Arguments:
|
||||
<FILES>...
|
||||
|
||||
Options:
|
||||
--config <CONFIG>
|
||||
Path to the `pyproject.toml` file to use for configuration
|
||||
-v, --verbose
|
||||
Enable verbose logging
|
||||
-q, --quiet
|
||||
@@ -132,6 +134,8 @@ Options:
|
||||
Regular expression matching the name of dummy variables
|
||||
--target-version <TARGET_VERSION>
|
||||
The minimum Python version that should be supported
|
||||
--stdin-filename <STDIN_FILENAME>
|
||||
The name of the file when passing it through stdin
|
||||
-h, --help
|
||||
Print help information
|
||||
-V, --version
|
||||
@@ -212,12 +216,13 @@ variables.)
|
||||
|
||||
ruff also implements some of the most popular Flake8 plugins natively, including:
|
||||
|
||||
- [`pydocstyle`](https://pypi.org/project/pydocstyle/)
|
||||
- [`flake8-docstrings`](https://pypi.org/project/flake8-docstrings/)
|
||||
- [`flake8-builtins`](https://pypi.org/project/flake8-builtins/)
|
||||
- [`flake8-super`](https://pypi.org/project/flake8-super/)
|
||||
- [`flake8-print`](https://pypi.org/project/flake8-print/)
|
||||
- [`flake8-comprehensions`](https://pypi.org/project/flake8-comprehensions/) (12/16)
|
||||
- [`flake8-comprehensions`](https://pypi.org/project/flake8-comprehensions/)
|
||||
- [`flake8-bugbear`](https://pypi.org/project/flake8-bugbear/) (3/32)
|
||||
- [`flake8-docstrings`](https://pypi.org/project/flake8-docstrings/) (37/48)
|
||||
- [`pyupgrade`](https://pypi.org/project/pyupgrade/) (8/34)
|
||||
|
||||
Beyond rule-set parity, ruff suffers from the following limitations vis-à-vis Flake8:
|
||||
@@ -293,8 +298,12 @@ The 🛠 emoji indicates that a rule is automatically fixable by the `--fix` com
|
||||
| C408 | UnnecessaryCollectionCall | Unnecessary <dict/list/tuple> call - rewrite as a literal | | |
|
||||
| C409 | UnnecessaryLiteralWithinTupleCall | Unnecessary <list/tuple> literal passed to tuple() - remove the outer call to tuple() | | |
|
||||
| C410 | UnnecessaryLiteralWithinListCall | Unnecessary <list/tuple> literal passed to list() - rewrite as a list literal | | |
|
||||
| C411 | UnnecessaryListCall | Unnecessary list call - remove the outer call to list() | | |
|
||||
| C413 | UnnecessaryCallAroundSorted | Unnecessary <list/reversed> call around sorted() | | |
|
||||
| C414 | UnnecessaryDoubleCastOrProcess | Unnecessary <list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>(). | | |
|
||||
| C415 | UnnecessarySubscriptReversal | Unnecessary subscript reversal of iterable within <reversed/set/sorted>() | | |
|
||||
| C416 | UnnecessaryComprehension | Unnecessary <list/set> comprehension - rewrite using <list/set>() | | |
|
||||
| C417 | UnnecessaryMap | Unnecessary map usage - rewrite using a <list/set/dict> comprehension | | |
|
||||
| T201 | PrintFound | `print` found | | 🛠 |
|
||||
| T203 | PPrintFound | `pprint` found | | 🛠 |
|
||||
| U001 | UselessMetaclassType | `__metaclass__ = type` is implied | | 🛠 |
|
||||
@@ -314,34 +323,41 @@ The 🛠 emoji indicates that a rule is automatically fixable by the `--fix` com
|
||||
| D106 | PublicNestedClass | Missing docstring in public nested class | | |
|
||||
| D107 | PublicInit | Missing docstring in __init__ | | |
|
||||
| D200 | FitsOnOneLine | One-line docstring should fit on one line | | |
|
||||
| D201 | NoBlankLineBeforeFunction | No blank lines allowed before function docstring (found 1) | | |
|
||||
| D202 | NoBlankLineAfterFunction | No blank lines allowed after function docstring (found 1) | | |
|
||||
| D203 | OneBlankLineBeforeClass | 1 blank line required before class docstring | | |
|
||||
| D204 | OneBlankLineAfterClass | 1 blank line required after class docstring | | |
|
||||
| D205 | NoBlankLineAfterSummary | 1 blank line required between summary line and description | | |
|
||||
| D206 | IndentWithSpaces | Docstring should be indented with spaces, not tabs | | |
|
||||
| D207 | NoUnderIndentation | Docstring is under-indented | | |
|
||||
| D208 | NoOverIndentation | Docstring is over-indented | | |
|
||||
| D209 | NewLineAfterLastParagraph | Multi-line docstring closing quotes should be on a separate line | | |
|
||||
| D210 | NoSurroundingWhitespace | No whitespaces allowed surrounding docstring text | | |
|
||||
| D211 | NoBlankLineBeforeClass | No blank lines allowed before class docstring | | |
|
||||
| D212 | MultiLineSummaryFirstLine | Multi-line docstring summary should start at the first line | | |
|
||||
| D213 | MultiLineSummarySecondLine | Multi-line docstring summary should start at the second line | | |
|
||||
| D214 | SectionNotOverIndented | Section is over-indented ("Returns") | | |
|
||||
| D215 | SectionUnderlineNotOverIndented | Section underline is over-indented ("Returns") | | |
|
||||
| D300 | UsesTripleQuotes | Use """triple double quotes""" | | |
|
||||
| D400 | EndsInPeriod | First line should end with a period | | |
|
||||
| D402 | NoSignature | First line should not be the function's 'signature' | | |
|
||||
| D403 | FirstLineCapitalized | First word of the first line should be properly capitalized | | |
|
||||
| D404 | NoThisPrefix | First word of the docstring should not be `This` | | |
|
||||
| D415 | EndsInPunctuation | First line should end with a period, question mark, or exclamation point | | |
|
||||
| D418 | SkipDocstring | Function decorated with @overload shouldn't contain a docstring | | |
|
||||
| D419 | NonEmpty | Docstring is empty | | |
|
||||
| D201 | NoBlankLineBeforeFunction | No blank lines allowed before function docstring (found 1) | | |
|
||||
| D202 | NoBlankLineAfterFunction | No blank lines allowed after function docstring (found 1) | | |
|
||||
| D211 | NoBlankLineBeforeClass | No blank lines allowed before class docstring | | |
|
||||
| D203 | OneBlankLineBeforeClass | 1 blank line required before class docstring | | |
|
||||
| D204 | OneBlankLineAfterClass | 1 blank line required after class docstring | | |
|
||||
| D405 | CapitalizeSectionName | Section name should be properly capitalized ("returns") | | |
|
||||
| D413 | BlankLineAfterLastSection | Missing blank line after last section ("Returns") | | |
|
||||
| D410 | BlankLineAfterSection | Missing blank line after section ("Returns") | | |
|
||||
| D411 | BlankLineBeforeSection | Missing blank line before section ("Returns") | | |
|
||||
| D406 | NewLineAfterSectionName | Section name should end with a newline ("Returns") | | |
|
||||
| D407 | DashedUnderlineAfterSection | Missing dashed underline after section ("Returns") | | |
|
||||
| D408 | SectionUnderlineAfterName | Section underline should be in the line following the section's name ("Returns") | | |
|
||||
| D409 | SectionUnderlineMatchesSectionLength | Section underline should match the length of its name ("Returns") | | |
|
||||
| D410 | BlankLineAfterSection | Missing blank line after section ("Returns") | | |
|
||||
| D411 | BlankLineBeforeSection | Missing blank line before section ("Returns") | | |
|
||||
| D412 | NoBlankLinesBetweenHeaderAndContent | No blank lines allowed between a section header and its content ("Returns") | | |
|
||||
| D413 | BlankLineAfterLastSection | Missing blank line after last section ("Returns") | | |
|
||||
| D414 | NonEmptySection | Section has no content ("Returns") | | |
|
||||
| D415 | EndsInPunctuation | First line should end with a period, question mark, or exclamation point | | |
|
||||
| D416 | SectionNameEndsInColon | Section name should end with a colon ("Returns") | | |
|
||||
| D417 | DocumentAllArguments | Missing argument descriptions in the docstring: `x`, `y` | | |
|
||||
| D418 | SkipDocstring | Function decorated with @overload shouldn't contain a docstring | | |
|
||||
| D419 | NonEmpty | Docstring is empty | | |
|
||||
| M001 | UnusedNOQA | Unused `noqa` directive | | 🛠 |
|
||||
|
||||
## Integrations
|
||||
|
||||
2
resources/test/fixtures/C411.py
vendored
Normal file
2
resources/test/fixtures/C411.py
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
x = [1, 2, 3]
|
||||
list([i for i in x])
|
||||
4
resources/test/fixtures/C413.py
vendored
Normal file
4
resources/test/fixtures/C413.py
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
x = [2, 3, 1]
|
||||
list(sorted(x))
|
||||
reversed(sorted(x))
|
||||
reversed(sorted(x, reverse=True))
|
||||
6
resources/test/fixtures/C416.py
vendored
Normal file
6
resources/test/fixtures/C416.py
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
x = [1, 2, 3]
|
||||
[i for i in x]
|
||||
{i for i in x}
|
||||
|
||||
[i for i in x if i > 1]
|
||||
[i for i in x for j in x]
|
||||
6
resources/test/fixtures/C417.py
vendored
Normal file
6
resources/test/fixtures/C417.py
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
nums = [1, 2, 3]
|
||||
map(lambda x: x + 1, nums)
|
||||
map(lambda x: str(x), nums)
|
||||
list(map(lambda x: x * 2, nums))
|
||||
set(map(lambda x: x % 2 == 0, nums))
|
||||
dict(map(lambda v: (v, v**2), nums))
|
||||
108
resources/test/fixtures/canonical_google_examples.py
vendored
Normal file
108
resources/test/fixtures/canonical_google_examples.py
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
"""A one line summary of the module or program, terminated by a period.
|
||||
|
||||
Leave one blank line. The rest of this docstring should contain an
|
||||
overall description of the module or program. Optionally, it may also
|
||||
contain a brief description of exported classes and functions and/or usage
|
||||
examples.
|
||||
|
||||
Typical usage example:
|
||||
|
||||
foo = ClassFoo()
|
||||
bar = foo.FunctionBar()
|
||||
"""
|
||||
# above: "2.8.2 Modules" section example
|
||||
# https://google.github.io/styleguide/pyguide.html#382-modules
|
||||
|
||||
# Examples from the official "Google Python Style Guide" documentation:
|
||||
# * As HTML: https://google.github.io/styleguide/pyguide.html
|
||||
# * Source Markdown:
|
||||
# https://github.com/google/styleguide/blob/gh-pages/pyguide.md
|
||||
|
||||
import os
|
||||
from .expected import Expectation
|
||||
|
||||
expectation = Expectation()
|
||||
expect = expectation.expect
|
||||
|
||||
# module docstring expected violations:
|
||||
expectation.expected.add((
|
||||
os.path.normcase(__file__),
|
||||
"D213: Multi-line docstring summary should start at the second line"))
|
||||
|
||||
|
||||
# "3.8.3 Functions and Methods" section example
|
||||
# https://google.github.io/styleguide/pyguide.html#383-functions-and-methods
|
||||
@expect("D213: Multi-line docstring summary should start at the second line",
|
||||
arg_count=3)
|
||||
@expect("D401: First line should be in imperative mood "
|
||||
"(perhaps 'Fetch', not 'Fetches')", arg_count=3)
|
||||
@expect("D406: Section name should end with a newline "
|
||||
"('Raises', not 'Raises:')", arg_count=3)
|
||||
@expect("D406: Section name should end with a newline "
|
||||
"('Returns', not 'Returns:')", arg_count=3)
|
||||
@expect("D407: Missing dashed underline after section ('Raises')", arg_count=3)
|
||||
@expect("D407: Missing dashed underline after section ('Returns')",
|
||||
arg_count=3)
|
||||
@expect("D413: Missing blank line after last section ('Raises')", arg_count=3)
|
||||
def fetch_bigtable_rows(big_table, keys, other_silly_variable=None):
|
||||
"""Fetches rows from a Bigtable.
|
||||
|
||||
Retrieves rows pertaining to the given keys from the Table instance
|
||||
represented by big_table. Silly things may happen if
|
||||
other_silly_variable is not None.
|
||||
|
||||
Args:
|
||||
big_table: An open Bigtable Table instance.
|
||||
keys: A sequence of strings representing the key of each table row
|
||||
to fetch.
|
||||
other_silly_variable: Another optional variable, that has a much
|
||||
longer name than the other args, and which does nothing.
|
||||
|
||||
Returns:
|
||||
A dict mapping keys to the corresponding table row data
|
||||
fetched. Each row is represented as a tuple of strings. For
|
||||
example:
|
||||
|
||||
{'Serak': ('Rigel VII', 'Preparer'),
|
||||
'Zim': ('Irk', 'Invader'),
|
||||
'Lrrr': ('Omicron Persei 8', 'Emperor')}
|
||||
|
||||
If a key from the keys argument is missing from the dictionary,
|
||||
then that row was not found in the table.
|
||||
|
||||
Raises:
|
||||
IOError: An error occurred accessing the bigtable.Table object.
|
||||
"""
|
||||
|
||||
|
||||
# "3.8.4 Classes" section example
|
||||
# https://google.github.io/styleguide/pyguide.html#384-classes
|
||||
@expect("D203: 1 blank line required before class docstring (found 0)")
|
||||
@expect("D213: Multi-line docstring summary should start at the second line")
|
||||
@expect("D406: Section name should end with a newline "
|
||||
"('Attributes', not 'Attributes:')")
|
||||
@expect("D407: Missing dashed underline after section ('Attributes')")
|
||||
@expect("D413: Missing blank line after last section ('Attributes')")
|
||||
class SampleClass:
|
||||
"""Summary of class here.
|
||||
|
||||
Longer class information....
|
||||
Longer class information....
|
||||
|
||||
Attributes:
|
||||
likes_spam: A boolean indicating if we like SPAM or not.
|
||||
eggs: An integer count of the eggs we have laid.
|
||||
"""
|
||||
|
||||
@expect("D401: First line should be in imperative mood "
|
||||
"(perhaps 'Init', not 'Inits')", arg_count=2)
|
||||
def __init__(self, likes_spam=False):
|
||||
"""Inits SampleClass with blah."""
|
||||
if self: # added to avoid NameError when run via @expect decorator
|
||||
self.likes_spam = likes_spam
|
||||
self.eggs = 0
|
||||
|
||||
@expect("D401: First line should be in imperative mood "
|
||||
"(perhaps 'Perform', not 'Performs')", arg_count=1)
|
||||
def public_method(self):
|
||||
"""Performs operation blah."""
|
||||
163
resources/test/fixtures/canonical_numpy_examples.py
vendored
Normal file
163
resources/test/fixtures/canonical_numpy_examples.py
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
"""This is the docstring for the example.py module. Modules names should
|
||||
have short, all-lowercase names. The module name may have underscores if
|
||||
this improves readability.
|
||||
|
||||
Every module should have a docstring at the very top of the file. The
|
||||
module's docstring may extend over multiple lines. If your docstring does
|
||||
extend over multiple lines, the closing three quotation marks must be on
|
||||
a line by itself, preferably preceded by a blank line.
|
||||
|
||||
"""
|
||||
|
||||
# Example source file from the official "numpydoc docstring guide"
|
||||
# documentation (with the modification of commenting out all the original
|
||||
# ``import`` lines, plus adding this note and ``Expectation`` code):
|
||||
# * As HTML: https://numpydoc.readthedocs.io/en/latest/example.html
|
||||
# * Source Python:
|
||||
# https://github.com/numpy/numpydoc/blob/master/doc/example.py
|
||||
|
||||
# from __future__ import division, absolute_import, print_function
|
||||
#
|
||||
# import os # standard library imports first
|
||||
#
|
||||
# Do NOT import using *, e.g. from numpy import *
|
||||
#
|
||||
# Import the module using
|
||||
#
|
||||
# import numpy
|
||||
#
|
||||
# instead or import individual functions as needed, e.g
|
||||
#
|
||||
# from numpy import array, zeros
|
||||
#
|
||||
# If you prefer the use of abbreviated module names, we suggest the
|
||||
# convention used by NumPy itself::
|
||||
#
|
||||
# import numpy as np
|
||||
# import matplotlib as mpl
|
||||
# import matplotlib.pyplot as plt
|
||||
#
|
||||
# These abbreviated names are not to be used in docstrings; users must
|
||||
# be able to paste and execute docstrings after importing only the
|
||||
# numpy module itself, unabbreviated.
|
||||
|
||||
import os
|
||||
from .expected import Expectation
|
||||
|
||||
expectation = Expectation()
|
||||
expect = expectation.expect
|
||||
|
||||
# module docstring expected violations:
|
||||
expectation.expected.add((
|
||||
os.path.normcase(__file__),
|
||||
"D205: 1 blank line required between summary line and description "
|
||||
"(found 0)"))
|
||||
expectation.expected.add((
|
||||
os.path.normcase(__file__),
|
||||
"D213: Multi-line docstring summary should start at the second line"))
|
||||
expectation.expected.add((
|
||||
os.path.normcase(__file__),
|
||||
"D400: First line should end with a period (not 'd')"))
|
||||
expectation.expected.add((
|
||||
os.path.normcase(__file__),
|
||||
"D404: First word of the docstring should not be `This`"))
|
||||
expectation.expected.add((
|
||||
os.path.normcase(__file__),
|
||||
"D415: First line should end with a period, question mark, or exclamation "
|
||||
"point (not 'd')"))
|
||||
|
||||
|
||||
@expect("D213: Multi-line docstring summary should start at the second line",
|
||||
arg_count=3)
|
||||
@expect("D401: First line should be in imperative mood; try rephrasing "
|
||||
"(found 'A')", arg_count=3)
|
||||
@expect("D413: Missing blank line after last section ('Examples')",
|
||||
arg_count=3)
|
||||
def foo(var1, var2, long_var_name='hi'):
|
||||
r"""A one-line summary that does not use variable names.
|
||||
|
||||
Several sentences providing an extended description. Refer to
|
||||
variables using back-ticks, e.g. `var`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
var1 : array_like
|
||||
Array_like means all those objects -- lists, nested lists, etc. --
|
||||
that can be converted to an array. We can also refer to
|
||||
variables like `var1`.
|
||||
var2 : int
|
||||
The type above can either refer to an actual Python type
|
||||
(e.g. ``int``), or describe the type of the variable in more
|
||||
detail, e.g. ``(N,) ndarray`` or ``array_like``.
|
||||
long_var_name : {'hi', 'ho'}, optional
|
||||
Choices in brackets, default first when optional.
|
||||
|
||||
Returns
|
||||
-------
|
||||
type
|
||||
Explanation of anonymous return value of type ``type``.
|
||||
describe : type
|
||||
Explanation of return value named `describe`.
|
||||
out : type
|
||||
Explanation of `out`.
|
||||
type_without_description
|
||||
|
||||
Other Parameters
|
||||
----------------
|
||||
only_seldom_used_keywords : type
|
||||
Explanation
|
||||
common_parameters_listed_above : type
|
||||
Explanation
|
||||
|
||||
Raises
|
||||
------
|
||||
BadException
|
||||
Because you shouldn't have done that.
|
||||
|
||||
See Also
|
||||
--------
|
||||
numpy.array : Relationship (optional).
|
||||
numpy.ndarray : Relationship (optional), which could be fairly long, in
|
||||
which case the line wraps here.
|
||||
numpy.dot, numpy.linalg.norm, numpy.eye
|
||||
|
||||
Notes
|
||||
-----
|
||||
Notes about the implementation algorithm (if needed).
|
||||
|
||||
This can have multiple paragraphs.
|
||||
|
||||
You may include some math:
|
||||
|
||||
.. math:: X(e^{j\omega } ) = x(n)e^{ - j\omega n}
|
||||
|
||||
And even use a Greek symbol like :math:`\omega` inline.
|
||||
|
||||
References
|
||||
----------
|
||||
Cite the relevant literature, e.g. [1]_. You may also cite these
|
||||
references in the notes section above.
|
||||
|
||||
.. [1] O. McNoleg, "The integration of GIS, remote sensing,
|
||||
expert systems and adaptive co-kriging for environmental habitat
|
||||
modelling of the Highland Haggis using object-oriented, fuzzy-logic
|
||||
and neural-network techniques," Computers & Geosciences, vol. 22,
|
||||
pp. 585-588, 1996.
|
||||
|
||||
Examples
|
||||
--------
|
||||
These are written in doctest format, and should illustrate how to
|
||||
use the function.
|
||||
|
||||
>>> a = [1, 2, 3]
|
||||
>>> print([x + 3 for x in a])
|
||||
[4, 5, 6]
|
||||
>>> print("a\nb")
|
||||
a
|
||||
b
|
||||
"""
|
||||
# After closing class docstring, there should be one blank line to
|
||||
# separate following codes (according to PEP257).
|
||||
# But for function, method and module, there should be no blank lines
|
||||
# after closing the docstring.
|
||||
pass
|
||||
19
scripts/.flake8
Normal file
19
scripts/.flake8
Normal file
@@ -0,0 +1,19 @@
|
||||
[flake8]
|
||||
exclude =
|
||||
# Defaults
|
||||
.svn,
|
||||
CVS,
|
||||
.bzr,
|
||||
.hg,
|
||||
.git,
|
||||
__pycache__,
|
||||
.tox,
|
||||
.idea,
|
||||
.mypy_cache,
|
||||
.venv,
|
||||
node_modules,
|
||||
# Custom
|
||||
_state_machine.py,
|
||||
test_fstring.py,
|
||||
bad_coding2.py,
|
||||
badsyntax_*.py
|
||||
@@ -4,8 +4,8 @@ use itertools::izip;
|
||||
use num_bigint::BigInt;
|
||||
use regex::Regex;
|
||||
use rustpython_parser::ast::{
|
||||
Arg, ArgData, Arguments, Cmpop, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprKind,
|
||||
KeywordData, Located, Stmt, StmtKind, Unaryop,
|
||||
Arg, ArgData, Arguments, Cmpop, Comprehension, Constant, Excepthandler, ExcepthandlerKind,
|
||||
Expr, ExprKind, KeywordData, Located, Stmt, StmtKind, Unaryop,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -1004,6 +1004,42 @@ pub fn unnecessary_literal_within_list_call(
|
||||
None
|
||||
}
|
||||
|
||||
pub fn unnecessary_list_call(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
|
||||
if let ExprKind::Name { id, .. } = &func.node {
|
||||
if id == "list" {
|
||||
if let Some(arg) = args.first() {
|
||||
if let ExprKind::ListComp { .. } = &arg.node {
|
||||
return Some(Check::new(
|
||||
CheckKind::UnnecessaryListCall,
|
||||
Range::from_located(expr),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn unnecessary_call_around_sorted(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
|
||||
if let ExprKind::Name { id: outer, .. } = &func.node {
|
||||
if outer == "list" || outer == "reversed" {
|
||||
if let Some(arg) = args.first() {
|
||||
if let ExprKind::Call { func, .. } = &arg.node {
|
||||
if let ExprKind::Name { id: inner, .. } = &func.node {
|
||||
if inner == "sorted" {
|
||||
return Some(Check::new(
|
||||
CheckKind::UnnecessaryCallAroundSorted(outer.to_string()),
|
||||
Range::from_located(expr),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn unnecessary_double_cast_or_process(
|
||||
expr: &Expr,
|
||||
func: &Expr,
|
||||
@@ -1104,6 +1140,99 @@ pub fn unnecessary_subscript_reversal(expr: &Expr, func: &Expr, args: &[Expr]) -
|
||||
None
|
||||
}
|
||||
|
||||
pub fn unnecessary_comprehension(
|
||||
expr: &Expr,
|
||||
elt: &Located<ExprKind>,
|
||||
generators: &Vec<Comprehension>,
|
||||
) -> Option<Check> {
|
||||
if generators.len() == 1 {
|
||||
let generator = &generators[0];
|
||||
if generator.ifs.is_empty() && generator.is_async == 0 {
|
||||
if let ExprKind::Name { id: elt_id, .. } = &elt.node {
|
||||
if let ExprKind::Name { id: target_id, .. } = &generator.target.node {
|
||||
if elt_id == target_id {
|
||||
match &expr.node {
|
||||
ExprKind::ListComp { .. } => {
|
||||
return Some(Check::new(
|
||||
CheckKind::UnnecessaryComprehension("list".to_string()),
|
||||
Range::from_located(expr),
|
||||
))
|
||||
}
|
||||
ExprKind::SetComp { .. } => {
|
||||
return Some(Check::new(
|
||||
CheckKind::UnnecessaryComprehension("set".to_string()),
|
||||
Range::from_located(expr),
|
||||
))
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn unnecessary_map(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
|
||||
if let ExprKind::Name { id, .. } = &func.node {
|
||||
if id == "map" {
|
||||
if args.len() == 2 {
|
||||
if let ExprKind::Lambda { .. } = &args[0].node {
|
||||
return Some(Check::new(
|
||||
CheckKind::UnnecessaryMap("generator".to_string()),
|
||||
Range::from_located(expr),
|
||||
));
|
||||
}
|
||||
}
|
||||
} else if id == "list" || id == "set" {
|
||||
if let Some(arg) = args.first() {
|
||||
if let ExprKind::Call { func, args, .. } = &arg.node {
|
||||
if let ExprKind::Name { id: f, .. } = &func.node {
|
||||
if f == "map" {
|
||||
if let Some(arg) = args.first() {
|
||||
if let ExprKind::Lambda { .. } = &arg.node {
|
||||
return Some(Check::new(
|
||||
CheckKind::UnnecessaryMap(id.to_string()),
|
||||
Range::from_located(expr),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if id == "dict" {
|
||||
if args.len() == 1 {
|
||||
if let ExprKind::Call { func, args, .. } = &args[0].node {
|
||||
if let ExprKind::Name { id: f, .. } = &func.node {
|
||||
if f == "map" {
|
||||
if let Some(arg) = args.first() {
|
||||
if let ExprKind::Lambda { body, .. } = &arg.node {
|
||||
match &body.node {
|
||||
ExprKind::Tuple { elts, .. }
|
||||
| ExprKind::List { elts, .. }
|
||||
if elts.len() == 2 =>
|
||||
{
|
||||
return Some(Check::new(
|
||||
CheckKind::UnnecessaryMap(id.to_string()),
|
||||
Range::from_located(expr),
|
||||
))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// flake8-super
|
||||
/// Check that `super()` has no args
|
||||
pub fn super_args(
|
||||
|
||||
@@ -121,7 +121,7 @@ pub fn is_unpacking_assignment(stmt: &Stmt) -> bool {
|
||||
/// Struct used to efficiently slice source code at (row, column) Locations.
|
||||
pub struct SourceCodeLocator<'a> {
|
||||
content: &'a str,
|
||||
offsets: Vec<usize>,
|
||||
offsets: Vec<Vec<usize>>,
|
||||
initialized: bool,
|
||||
}
|
||||
|
||||
@@ -137,26 +137,32 @@ impl<'a> SourceCodeLocator<'a> {
|
||||
fn init(&mut self) {
|
||||
if !self.initialized {
|
||||
let mut offset = 0;
|
||||
for i in self.content.lines() {
|
||||
self.offsets.push(offset);
|
||||
offset += i.len();
|
||||
offset += 1;
|
||||
for line in self.content.lines() {
|
||||
let mut newline = 0;
|
||||
let mut line_offsets: Vec<usize> = vec![];
|
||||
for (i, _char) in line.char_indices() {
|
||||
line_offsets.push(offset + i);
|
||||
newline = i + 1;
|
||||
}
|
||||
line_offsets.push(offset + newline);
|
||||
self.offsets.push(line_offsets);
|
||||
offset += newline + 1;
|
||||
}
|
||||
self.offsets.push(offset);
|
||||
self.offsets.push(vec![offset]);
|
||||
self.initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn slice_source_code_at(&mut self, location: &Location) -> &'a str {
|
||||
self.init();
|
||||
let offset = self.offsets[location.row() - 1] + location.column() - 1;
|
||||
let offset = self.offsets[location.row() - 1][location.column() - 1];
|
||||
&self.content[offset..]
|
||||
}
|
||||
|
||||
pub fn slice_source_code_range(&mut self, range: &Range) -> &'a str {
|
||||
self.init();
|
||||
let start = self.offsets[range.location.row() - 1] + range.location.column() - 1;
|
||||
let end = self.offsets[range.end_location.row() - 1] + range.end_location.column() - 1;
|
||||
let start = self.offsets[range.location.row() - 1][range.location.column() - 1];
|
||||
let end = self.offsets[range.end_location.row() - 1][range.end_location.column() - 1];
|
||||
&self.content[start..end]
|
||||
}
|
||||
|
||||
@@ -166,12 +172,10 @@ impl<'a> SourceCodeLocator<'a> {
|
||||
inner: &Range,
|
||||
) -> (&'a str, &'a str, &'a str) {
|
||||
self.init();
|
||||
let outer_start = self.offsets[outer.location.row() - 1] + outer.location.column() - 1;
|
||||
let outer_end =
|
||||
self.offsets[outer.end_location.row() - 1] + outer.end_location.column() - 1;
|
||||
let inner_start = self.offsets[inner.location.row() - 1] + inner.location.column() - 1;
|
||||
let inner_end =
|
||||
self.offsets[inner.end_location.row() - 1] + inner.end_location.column() - 1;
|
||||
let outer_start = self.offsets[outer.location.row() - 1][outer.location.column() - 1];
|
||||
let outer_end = self.offsets[outer.end_location.row() - 1][outer.end_location.column() - 1];
|
||||
let inner_start = self.offsets[inner.location.row() - 1][inner.location.column() - 1];
|
||||
let inner_end = self.offsets[inner.end_location.row() - 1][inner.end_location.column() - 1];
|
||||
(
|
||||
&self.content[outer_start..inner_start],
|
||||
&self.content[inner_start..inner_end],
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use itertools::Itertools;
|
||||
use rustpython_parser::ast::Location;
|
||||
|
||||
@@ -24,17 +20,15 @@ impl From<bool> for Mode {
|
||||
}
|
||||
|
||||
/// Auto-fix errors in a file, and write the fixed source code to disk.
|
||||
pub fn fix_file(checks: &mut [Check], contents: &str, path: &Path) -> Result<()> {
|
||||
pub fn fix_file(checks: &mut [Check], contents: &str) -> Option<String> {
|
||||
if checks.iter().all(|check| check.fix.is_none()) {
|
||||
return Ok(());
|
||||
return None;
|
||||
}
|
||||
|
||||
let output = apply_fixes(
|
||||
Some(apply_fixes(
|
||||
checks.iter_mut().filter_map(|check| check.fix.as_mut()),
|
||||
contents,
|
||||
);
|
||||
|
||||
fs::write(path, output).map_err(|e| e.into())
|
||||
))
|
||||
}
|
||||
|
||||
/// Apply a series of fixes.
|
||||
|
||||
116
src/check_ast.rs
116
src/check_ast.rs
@@ -21,13 +21,13 @@ use crate::ast::visitor::{walk_excepthandler, Visitor};
|
||||
use crate::ast::{checkers, helpers, operations, visitor};
|
||||
use crate::autofix::{fixer, fixes};
|
||||
use crate::checks::{Check, CheckCode, CheckKind};
|
||||
use crate::docstrings::docstring_checks;
|
||||
use crate::docstrings::docstring_plugins;
|
||||
use crate::docstrings::types::{Definition, DefinitionKind, Documentable};
|
||||
use crate::plugins;
|
||||
use crate::python::builtins::{BUILTINS, MAGIC_GLOBALS};
|
||||
use crate::python::future::ALL_FEATURE_NAMES;
|
||||
use crate::settings::{PythonVersion, Settings};
|
||||
use crate::visibility::{module_visibility, transition_scope, Modifier, Visibility, VisibleScope};
|
||||
use crate::{docstrings, plugins};
|
||||
|
||||
pub const GLOBAL_SCOPE_INDEX: usize = 0;
|
||||
|
||||
@@ -572,7 +572,7 @@ where
|
||||
let prev_visibile_scope = self.visible_scope.clone();
|
||||
match &stmt.node {
|
||||
StmtKind::FunctionDef { body, .. } | StmtKind::AsyncFunctionDef { body, .. } => {
|
||||
let definition = docstring_checks::extract(
|
||||
let definition = docstrings::extraction::extract(
|
||||
&self.visible_scope,
|
||||
stmt,
|
||||
body,
|
||||
@@ -590,7 +590,7 @@ where
|
||||
));
|
||||
}
|
||||
StmtKind::ClassDef { body, .. } => {
|
||||
let definition = docstring_checks::extract(
|
||||
let definition = docstrings::extraction::extract(
|
||||
&self.visible_scope,
|
||||
stmt,
|
||||
body,
|
||||
@@ -837,6 +837,19 @@ where
|
||||
};
|
||||
}
|
||||
|
||||
if self.settings.enabled.contains(&CheckCode::C411) {
|
||||
if let Some(check) = checkers::unnecessary_list_call(expr, func, args) {
|
||||
self.checks.push(check);
|
||||
};
|
||||
}
|
||||
|
||||
if self.settings.enabled.contains(&CheckCode::C413) {
|
||||
if let Some(check) = checkers::unnecessary_call_around_sorted(expr, func, args)
|
||||
{
|
||||
self.checks.push(check);
|
||||
};
|
||||
}
|
||||
|
||||
if self.settings.enabled.contains(&CheckCode::C414) {
|
||||
if let Some(check) =
|
||||
checkers::unnecessary_double_cast_or_process(expr, func, args)
|
||||
@@ -852,6 +865,12 @@ where
|
||||
};
|
||||
}
|
||||
|
||||
if self.settings.enabled.contains(&CheckCode::C417) {
|
||||
if let Some(check) = checkers::unnecessary_map(expr, func, args) {
|
||||
self.checks.push(check);
|
||||
};
|
||||
}
|
||||
|
||||
// pyupgrade
|
||||
if self.settings.enabled.contains(&CheckCode::U002)
|
||||
&& self.settings.target_version >= PythonVersion::Py310
|
||||
@@ -1021,10 +1040,20 @@ where
|
||||
self.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
ExprKind::GeneratorExp { .. }
|
||||
| ExprKind::ListComp { .. }
|
||||
| ExprKind::DictComp { .. }
|
||||
| ExprKind::SetComp { .. } => self.push_scope(Scope::new(ScopeKind::Generator)),
|
||||
|
||||
ExprKind::ListComp { elt, generators } | ExprKind::SetComp { elt, generators } => {
|
||||
if self.settings.enabled.contains(&CheckCode::C416) {
|
||||
if let Some(check) = checkers::unnecessary_comprehension(expr, elt, generators)
|
||||
{
|
||||
self.checks.push(check);
|
||||
};
|
||||
}
|
||||
self.push_scope(Scope::new(ScopeKind::Generator))
|
||||
}
|
||||
|
||||
ExprKind::GeneratorExp { .. } | ExprKind::DictComp { .. } => {
|
||||
self.push_scope(Scope::new(ScopeKind::Generator))
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
@@ -1363,6 +1392,15 @@ impl<'a> Checker<'a> {
|
||||
self.checks.push(check);
|
||||
}
|
||||
|
||||
pub fn add_check_if<F>(&mut self, code: &CheckCode, f: F)
|
||||
where
|
||||
F: FnOnce() -> Check,
|
||||
{
|
||||
if self.settings.enabled.contains(code) {
|
||||
self.checks.push(f());
|
||||
}
|
||||
}
|
||||
|
||||
fn push_parent(&mut self, parent: &'a Stmt) {
|
||||
self.parent_stack.push(self.parents.len());
|
||||
self.parents.push(parent);
|
||||
@@ -1634,7 +1672,6 @@ impl<'a> Checker<'a> {
|
||||
|
||||
fn handle_node_delete(&mut self, expr: &Expr) {
|
||||
if let ExprKind::Name { id, .. } = &expr.node {
|
||||
// Check if we're on a conditional branch.
|
||||
if operations::on_conditional_branch(&self.parent_stack, &self.parents) {
|
||||
return;
|
||||
}
|
||||
@@ -1655,7 +1692,7 @@ impl<'a> Checker<'a> {
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
let docstring = docstring_checks::docstring_from(python_ast);
|
||||
let docstring = docstrings::extraction::docstring_from(python_ast);
|
||||
self.docstrings.push((
|
||||
Definition {
|
||||
kind: if self.path.ends_with("__init__.py") {
|
||||
@@ -1918,77 +1955,84 @@ impl<'a> Checker<'a> {
|
||||
|
||||
fn check_docstrings(&mut self) {
|
||||
while let Some((docstring, visibility)) = self.docstrings.pop() {
|
||||
if !docstring_checks::not_empty(self, &docstring) {
|
||||
if !docstring_plugins::not_empty(self, &docstring) {
|
||||
continue;
|
||||
}
|
||||
if !docstring_checks::not_missing(self, &docstring, &visibility) {
|
||||
if !docstring_plugins::not_missing(self, &docstring, &visibility) {
|
||||
continue;
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D200) {
|
||||
docstring_checks::one_liner(self, &docstring);
|
||||
docstring_plugins::one_liner(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D201)
|
||||
|| self.settings.enabled.contains(&CheckCode::D202)
|
||||
{
|
||||
docstring_checks::blank_before_after_function(self, &docstring);
|
||||
docstring_plugins::blank_before_after_function(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D203)
|
||||
|| self.settings.enabled.contains(&CheckCode::D204)
|
||||
|| self.settings.enabled.contains(&CheckCode::D211)
|
||||
{
|
||||
docstring_checks::blank_before_after_class(self, &docstring);
|
||||
docstring_plugins::blank_before_after_class(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D205) {
|
||||
docstring_checks::blank_after_summary(self, &docstring);
|
||||
docstring_plugins::blank_after_summary(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D206)
|
||||
|| self.settings.enabled.contains(&CheckCode::D207)
|
||||
|| self.settings.enabled.contains(&CheckCode::D208)
|
||||
{
|
||||
docstring_plugins::indent(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D209) {
|
||||
docstring_checks::newline_after_last_paragraph(self, &docstring);
|
||||
docstring_plugins::newline_after_last_paragraph(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D210) {
|
||||
docstring_checks::no_surrounding_whitespace(self, &docstring);
|
||||
docstring_plugins::no_surrounding_whitespace(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D212)
|
||||
|| self.settings.enabled.contains(&CheckCode::D213)
|
||||
{
|
||||
docstring_checks::multi_line_summary_start(self, &docstring);
|
||||
docstring_plugins::multi_line_summary_start(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D300) {
|
||||
docstring_checks::triple_quotes(self, &docstring);
|
||||
docstring_plugins::triple_quotes(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D400) {
|
||||
docstring_checks::ends_with_period(self, &docstring);
|
||||
docstring_plugins::ends_with_period(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D402) {
|
||||
docstring_checks::no_signature(self, &docstring);
|
||||
docstring_plugins::no_signature(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D403) {
|
||||
docstring_checks::capitalized(self, &docstring);
|
||||
docstring_plugins::capitalized(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D404) {
|
||||
docstring_checks::starts_with_this(self, &docstring);
|
||||
docstring_plugins::starts_with_this(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D415) {
|
||||
docstring_checks::ends_with_punctuation(self, &docstring);
|
||||
docstring_plugins::ends_with_punctuation(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D418) {
|
||||
docstring_checks::if_needed(self, &docstring);
|
||||
docstring_plugins::if_needed(self, &docstring);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::D407)
|
||||
|| self.settings.enabled.contains(&CheckCode::D414)
|
||||
if self.settings.enabled.contains(&CheckCode::D212)
|
||||
|| self.settings.enabled.contains(&CheckCode::D214)
|
||||
|| self.settings.enabled.contains(&CheckCode::D215)
|
||||
|| self.settings.enabled.contains(&CheckCode::D405)
|
||||
|| self.settings.enabled.contains(&CheckCode::D406)
|
||||
|| self.settings.enabled.contains(&CheckCode::D407)
|
||||
|| self.settings.enabled.contains(&CheckCode::D212)
|
||||
|| self.settings.enabled.contains(&CheckCode::D408)
|
||||
|| self.settings.enabled.contains(&CheckCode::D409)
|
||||
|| self.settings.enabled.contains(&CheckCode::D414)
|
||||
|| self.settings.enabled.contains(&CheckCode::D412)
|
||||
|| self.settings.enabled.contains(&CheckCode::D414)
|
||||
|| self.settings.enabled.contains(&CheckCode::D405)
|
||||
|| self.settings.enabled.contains(&CheckCode::D413)
|
||||
|| self.settings.enabled.contains(&CheckCode::D410)
|
||||
|| self.settings.enabled.contains(&CheckCode::D411)
|
||||
|| self.settings.enabled.contains(&CheckCode::D406)
|
||||
|| self.settings.enabled.contains(&CheckCode::D412)
|
||||
|| self.settings.enabled.contains(&CheckCode::D413)
|
||||
|| self.settings.enabled.contains(&CheckCode::D414)
|
||||
|| self.settings.enabled.contains(&CheckCode::D416)
|
||||
|| self.settings.enabled.contains(&CheckCode::D417)
|
||||
{
|
||||
docstring_checks::check_sections(self, &docstring);
|
||||
docstring_plugins::sections(self, &docstring);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,8 +137,12 @@ pub enum CheckCode {
|
||||
C408,
|
||||
C409,
|
||||
C410,
|
||||
C411,
|
||||
C413,
|
||||
C414,
|
||||
C415,
|
||||
C416,
|
||||
C417,
|
||||
// flake8-print
|
||||
T201,
|
||||
T203,
|
||||
@@ -166,11 +170,16 @@ pub enum CheckCode {
|
||||
D203,
|
||||
D204,
|
||||
D205,
|
||||
D206,
|
||||
D207,
|
||||
D208,
|
||||
D209,
|
||||
D210,
|
||||
D211,
|
||||
D212,
|
||||
D213,
|
||||
D214,
|
||||
D215,
|
||||
D300,
|
||||
D400,
|
||||
D402,
|
||||
@@ -187,6 +196,8 @@ pub enum CheckCode {
|
||||
D413,
|
||||
D414,
|
||||
D415,
|
||||
D416,
|
||||
D417,
|
||||
D418,
|
||||
D419,
|
||||
// Meta
|
||||
@@ -272,8 +283,12 @@ pub enum CheckKind {
|
||||
UnnecessaryCollectionCall(String),
|
||||
UnnecessaryLiteralWithinTupleCall(String),
|
||||
UnnecessaryLiteralWithinListCall(String),
|
||||
UnnecessaryListCall,
|
||||
UnnecessaryCallAroundSorted(String),
|
||||
UnnecessaryDoubleCastOrProcess(String, String),
|
||||
UnnecessarySubscriptReversal(String),
|
||||
UnnecessaryComprehension(String),
|
||||
UnnecessaryMap(String),
|
||||
// flake8-print
|
||||
PrintFound,
|
||||
PPrintFound,
|
||||
@@ -292,10 +307,12 @@ pub enum CheckKind {
|
||||
BlankLineBeforeSection(String),
|
||||
CapitalizeSectionName(String),
|
||||
DashedUnderlineAfterSection(String),
|
||||
DocumentAllArguments(Vec<String>),
|
||||
EndsInPeriod,
|
||||
EndsInPunctuation,
|
||||
FirstLineCapitalized,
|
||||
FitsOnOneLine,
|
||||
IndentWithSpaces,
|
||||
MagicMethod,
|
||||
MultiLineSummaryFirstLine,
|
||||
MultiLineSummarySecondLine,
|
||||
@@ -306,9 +323,11 @@ pub enum CheckKind {
|
||||
NoBlankLineBeforeClass(usize),
|
||||
NoBlankLineBeforeFunction(usize),
|
||||
NoBlankLinesBetweenHeaderAndContent(String),
|
||||
NoOverIndentation,
|
||||
NoSignature,
|
||||
NoSurroundingWhitespace,
|
||||
NoThisPrefix,
|
||||
NoUnderIndentation,
|
||||
NonEmpty,
|
||||
NonEmptySection(String),
|
||||
OneBlankLineAfterClass(usize),
|
||||
@@ -320,8 +339,11 @@ pub enum CheckKind {
|
||||
PublicModule,
|
||||
PublicNestedClass,
|
||||
PublicPackage,
|
||||
SectionNameEndsInColon(String),
|
||||
SectionNotOverIndented(String),
|
||||
SectionUnderlineAfterName(String),
|
||||
SectionUnderlineMatchesSectionLength(String),
|
||||
SectionUnderlineNotOverIndented(String),
|
||||
SkipDocstring,
|
||||
UsesTripleQuotes,
|
||||
// Meta
|
||||
@@ -414,6 +436,10 @@ impl CheckCode {
|
||||
CheckCode::C410 => {
|
||||
CheckKind::UnnecessaryLiteralWithinListCall("<list/tuple>".to_string())
|
||||
}
|
||||
CheckCode::C411 => CheckKind::UnnecessaryListCall,
|
||||
CheckCode::C413 => {
|
||||
CheckKind::UnnecessaryCallAroundSorted("<list/reversed>".to_string())
|
||||
}
|
||||
CheckCode::C414 => CheckKind::UnnecessaryDoubleCastOrProcess(
|
||||
"<list/reversed/set/sorted/tuple>".to_string(),
|
||||
"<list/set/sorted/tuple>".to_string(),
|
||||
@@ -421,6 +447,8 @@ impl CheckCode {
|
||||
CheckCode::C415 => {
|
||||
CheckKind::UnnecessarySubscriptReversal("<reversed/set/sorted>".to_string())
|
||||
}
|
||||
CheckCode::C416 => CheckKind::UnnecessaryComprehension("<list/set>".to_string()),
|
||||
CheckCode::C417 => CheckKind::UnnecessaryMap("<list/set/dict>".to_string()),
|
||||
// flake8-print
|
||||
CheckCode::T201 => CheckKind::PrintFound,
|
||||
CheckCode::T203 => CheckKind::PPrintFound,
|
||||
@@ -451,11 +479,16 @@ impl CheckCode {
|
||||
CheckCode::D203 => CheckKind::OneBlankLineBeforeClass(0),
|
||||
CheckCode::D204 => CheckKind::OneBlankLineAfterClass(0),
|
||||
CheckCode::D205 => CheckKind::NoBlankLineAfterSummary,
|
||||
CheckCode::D206 => CheckKind::IndentWithSpaces,
|
||||
CheckCode::D207 => CheckKind::NoUnderIndentation,
|
||||
CheckCode::D208 => CheckKind::NoOverIndentation,
|
||||
CheckCode::D209 => CheckKind::NewLineAfterLastParagraph,
|
||||
CheckCode::D210 => CheckKind::NoSurroundingWhitespace,
|
||||
CheckCode::D211 => CheckKind::NoBlankLineBeforeClass(1),
|
||||
CheckCode::D212 => CheckKind::MultiLineSummaryFirstLine,
|
||||
CheckCode::D213 => CheckKind::MultiLineSummarySecondLine,
|
||||
CheckCode::D214 => CheckKind::SectionNotOverIndented("Returns".to_string()),
|
||||
CheckCode::D215 => CheckKind::SectionUnderlineNotOverIndented("Returns".to_string()),
|
||||
CheckCode::D300 => CheckKind::UsesTripleQuotes,
|
||||
CheckCode::D400 => CheckKind::EndsInPeriod,
|
||||
CheckCode::D402 => CheckKind::NoSignature,
|
||||
@@ -476,6 +509,10 @@ impl CheckCode {
|
||||
CheckCode::D413 => CheckKind::BlankLineAfterLastSection("Returns".to_string()),
|
||||
CheckCode::D414 => CheckKind::NonEmptySection("Returns".to_string()),
|
||||
CheckCode::D415 => CheckKind::EndsInPunctuation,
|
||||
CheckCode::D416 => CheckKind::SectionNameEndsInColon("Returns".to_string()),
|
||||
CheckCode::D417 => {
|
||||
CheckKind::DocumentAllArguments(vec!["x".to_string(), "y".to_string()])
|
||||
}
|
||||
CheckCode::D418 => CheckKind::SkipDocstring,
|
||||
CheckCode::D419 => CheckKind::NonEmpty,
|
||||
// Meta
|
||||
@@ -552,8 +589,12 @@ impl CheckKind {
|
||||
CheckKind::UnnecessaryCollectionCall(_) => &CheckCode::C408,
|
||||
CheckKind::UnnecessaryLiteralWithinTupleCall(..) => &CheckCode::C409,
|
||||
CheckKind::UnnecessaryLiteralWithinListCall(..) => &CheckCode::C410,
|
||||
CheckKind::UnnecessaryListCall => &CheckCode::C411,
|
||||
CheckKind::UnnecessaryCallAroundSorted(_) => &CheckCode::C413,
|
||||
CheckKind::UnnecessaryDoubleCastOrProcess(..) => &CheckCode::C414,
|
||||
CheckKind::UnnecessarySubscriptReversal(_) => &CheckCode::C415,
|
||||
CheckKind::UnnecessaryComprehension(..) => &CheckCode::C416,
|
||||
CheckKind::UnnecessaryMap(_) => &CheckCode::C417,
|
||||
// flake8-print
|
||||
CheckKind::PrintFound => &CheckCode::T201,
|
||||
CheckKind::PPrintFound => &CheckCode::T203,
|
||||
@@ -572,10 +613,12 @@ impl CheckKind {
|
||||
CheckKind::BlankLineBeforeSection(_) => &CheckCode::D411,
|
||||
CheckKind::CapitalizeSectionName(_) => &CheckCode::D405,
|
||||
CheckKind::DashedUnderlineAfterSection(_) => &CheckCode::D407,
|
||||
CheckKind::DocumentAllArguments(_) => &CheckCode::D417,
|
||||
CheckKind::EndsInPeriod => &CheckCode::D400,
|
||||
CheckKind::EndsInPunctuation => &CheckCode::D415,
|
||||
CheckKind::FirstLineCapitalized => &CheckCode::D403,
|
||||
CheckKind::FitsOnOneLine => &CheckCode::D200,
|
||||
CheckKind::IndentWithSpaces => &CheckCode::D206,
|
||||
CheckKind::MagicMethod => &CheckCode::D105,
|
||||
CheckKind::MultiLineSummaryFirstLine => &CheckCode::D212,
|
||||
CheckKind::MultiLineSummarySecondLine => &CheckCode::D213,
|
||||
@@ -586,9 +629,11 @@ impl CheckKind {
|
||||
CheckKind::NoBlankLineBeforeClass(_) => &CheckCode::D211,
|
||||
CheckKind::NoBlankLineBeforeFunction(_) => &CheckCode::D201,
|
||||
CheckKind::NoBlankLinesBetweenHeaderAndContent(_) => &CheckCode::D412,
|
||||
CheckKind::NoOverIndentation => &CheckCode::D208,
|
||||
CheckKind::NoSignature => &CheckCode::D402,
|
||||
CheckKind::NoSurroundingWhitespace => &CheckCode::D210,
|
||||
CheckKind::NoThisPrefix => &CheckCode::D404,
|
||||
CheckKind::NoUnderIndentation => &CheckCode::D207,
|
||||
CheckKind::NonEmpty => &CheckCode::D419,
|
||||
CheckKind::NonEmptySection(_) => &CheckCode::D414,
|
||||
CheckKind::OneBlankLineAfterClass(_) => &CheckCode::D204,
|
||||
@@ -600,8 +645,11 @@ impl CheckKind {
|
||||
CheckKind::PublicModule => &CheckCode::D100,
|
||||
CheckKind::PublicNestedClass => &CheckCode::D106,
|
||||
CheckKind::PublicPackage => &CheckCode::D104,
|
||||
CheckKind::SectionNameEndsInColon(_) => &CheckCode::D416,
|
||||
CheckKind::SectionNotOverIndented(_) => &CheckCode::D214,
|
||||
CheckKind::SectionUnderlineAfterName(_) => &CheckCode::D408,
|
||||
CheckKind::SectionUnderlineMatchesSectionLength(_) => &CheckCode::D409,
|
||||
CheckKind::SectionUnderlineNotOverIndented(_) => &CheckCode::D215,
|
||||
CheckKind::SkipDocstring => &CheckCode::D418,
|
||||
CheckKind::UsesTripleQuotes => &CheckCode::D300,
|
||||
// Meta
|
||||
@@ -816,12 +864,28 @@ impl CheckKind {
|
||||
)
|
||||
}
|
||||
}
|
||||
CheckKind::UnnecessaryListCall => {
|
||||
"Unnecessary list call - remove the outer call to list()".to_string()
|
||||
}
|
||||
CheckKind::UnnecessaryCallAroundSorted(func) => {
|
||||
format!("Unnecessary {func} call around sorted()")
|
||||
}
|
||||
CheckKind::UnnecessaryDoubleCastOrProcess(inner, outer) => {
|
||||
format!("Unnecessary {inner} call within {outer}().")
|
||||
}
|
||||
CheckKind::UnnecessarySubscriptReversal(func) => {
|
||||
format!("Unnecessary subscript reversal of iterable within {func}()")
|
||||
}
|
||||
CheckKind::UnnecessaryComprehension(obj_type) => {
|
||||
format!(" Unnecessary {obj_type} comprehension - rewrite using {obj_type}()")
|
||||
}
|
||||
CheckKind::UnnecessaryMap(obj_type) => {
|
||||
if obj_type == "generator" {
|
||||
"Unnecessary map usage - rewrite using a generator expression".to_string()
|
||||
} else {
|
||||
format!("Unnecessary map usage - rewrite using a {obj_type} comprehension")
|
||||
}
|
||||
}
|
||||
// flake8-print
|
||||
CheckKind::PrintFound => "`print` found".to_string(),
|
||||
CheckKind::PPrintFound => "`pprint` found".to_string(),
|
||||
@@ -939,6 +1003,29 @@ impl CheckKind {
|
||||
)
|
||||
}
|
||||
CheckKind::NonEmptySection(name) => format!("Section has no content (\"{name}\")"),
|
||||
CheckKind::SectionNotOverIndented(name) => {
|
||||
format!("Section is over-indented (\"{name}\")")
|
||||
}
|
||||
CheckKind::SectionUnderlineNotOverIndented(name) => {
|
||||
format!("Section underline is over-indented (\"{name}\")")
|
||||
}
|
||||
CheckKind::SectionNameEndsInColon(name) => {
|
||||
format!("Section name should end with a colon (\"{name}\")")
|
||||
}
|
||||
CheckKind::DocumentAllArguments(names) => {
|
||||
if names.len() == 1 {
|
||||
let name = &names[0];
|
||||
format!("Missing argument description in the docstring: `{name}`")
|
||||
} else {
|
||||
let names = names.iter().map(|name| format!("`{name}`")).join(", ");
|
||||
format!("Missing argument descriptions in the docstring: {names}")
|
||||
}
|
||||
}
|
||||
CheckKind::IndentWithSpaces => {
|
||||
"Docstring should be indented with spaces, not tabs".to_string()
|
||||
}
|
||||
CheckKind::NoUnderIndentation => "Docstring is under-indented".to_string(),
|
||||
CheckKind::NoOverIndentation => "Docstring is over-indented".to_string(),
|
||||
// Meta
|
||||
CheckKind::UnusedNOQA(codes) => match codes {
|
||||
None => "Unused `noqa` directive".to_string(),
|
||||
|
||||
@@ -17,6 +17,9 @@ use crate::RawSettings;
|
||||
pub struct Cli {
|
||||
#[arg(required = true)]
|
||||
pub files: Vec<PathBuf>,
|
||||
/// Path to the `pyproject.toml` file to use for configuration.
|
||||
#[arg(long)]
|
||||
pub config: Option<PathBuf>,
|
||||
/// Enable verbose logging.
|
||||
#[arg(short, long)]
|
||||
pub verbose: bool,
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
pub mod docstring_checks;
|
||||
pub mod docstring_plugins;
|
||||
pub mod extraction;
|
||||
mod google;
|
||||
mod helpers;
|
||||
mod numpy;
|
||||
pub mod sections;
|
||||
mod styles;
|
||||
pub mod types;
|
||||
|
||||
@@ -2,99 +2,19 @@
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use rustpython_ast::{Constant, Expr, ExprKind, Location, Stmt, StmtKind};
|
||||
use rustpython_ast::{Constant, ExprKind, Location, StmtKind};
|
||||
|
||||
use crate::ast::types::Range;
|
||||
use crate::check_ast::Checker;
|
||||
use crate::checks::{Check, CheckCode, CheckKind};
|
||||
use crate::docstrings::sections::{check_numpy_section, section_contexts};
|
||||
use crate::docstrings::types::{Definition, DefinitionKind, Documentable};
|
||||
use crate::visibility::{is_init, is_magic, is_overload, Modifier, Visibility, VisibleScope};
|
||||
|
||||
/// Extract a docstring from a function or class body.
|
||||
pub fn docstring_from(suite: &[Stmt]) -> Option<&Expr> {
|
||||
if let Some(stmt) = suite.first() {
|
||||
if let StmtKind::Expr { value } = &stmt.node {
|
||||
if matches!(
|
||||
&value.node,
|
||||
ExprKind::Constant {
|
||||
value: Constant::Str(_),
|
||||
..
|
||||
}
|
||||
) {
|
||||
return Some(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Extract a `Definition` from the AST node defined by a `Stmt`.
|
||||
pub fn extract<'a>(
|
||||
scope: &VisibleScope,
|
||||
stmt: &'a Stmt,
|
||||
body: &'a [Stmt],
|
||||
kind: &Documentable,
|
||||
) -> Definition<'a> {
|
||||
let expr = docstring_from(body);
|
||||
match kind {
|
||||
Documentable::Function => match scope {
|
||||
VisibleScope {
|
||||
modifier: Modifier::Module,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::Function(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
VisibleScope {
|
||||
modifier: Modifier::Class,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::Method(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
VisibleScope {
|
||||
modifier: Modifier::Function,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::NestedFunction(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
},
|
||||
Documentable::Class => match scope {
|
||||
VisibleScope {
|
||||
modifier: Modifier::Module,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::Class(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
VisibleScope {
|
||||
modifier: Modifier::Class,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::NestedClass(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
VisibleScope {
|
||||
modifier: Modifier::Function,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::NestedClass(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the source code range for a docstring.
|
||||
pub fn range_for(docstring: &Expr) -> Range {
|
||||
// RustPython currently omits the first quotation mark in a string, so offset the location.
|
||||
Range {
|
||||
location: Location::new(docstring.location.row(), docstring.location.column() - 1),
|
||||
end_location: docstring.end_location,
|
||||
}
|
||||
}
|
||||
use crate::docstrings::google::check_google_section;
|
||||
use crate::docstrings::helpers;
|
||||
use crate::docstrings::helpers::{indentation, leading_space};
|
||||
use crate::docstrings::numpy::check_numpy_section;
|
||||
use crate::docstrings::sections::section_contexts;
|
||||
use crate::docstrings::styles::SectionStyle;
|
||||
use crate::docstrings::types::{Definition, DefinitionKind};
|
||||
use crate::visibility::{is_init, is_magic, is_overload, Visibility};
|
||||
|
||||
/// D100, D101, D102, D103, D104, D105, D106, D107
|
||||
pub fn not_missing(
|
||||
@@ -216,7 +136,10 @@ pub fn one_liner(checker: &mut Checker, definition: &Definition) {
|
||||
}
|
||||
|
||||
if non_empty_line_count == 1 && line_count > 1 {
|
||||
checker.add_check(Check::new(CheckKind::FitsOnOneLine, range_for(docstring)));
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::FitsOnOneLine,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -239,9 +162,10 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio
|
||||
..
|
||||
} = &docstring.node
|
||||
{
|
||||
let (before, _, after) = checker
|
||||
.locator
|
||||
.partition_source_code_at(&Range::from_located(parent), &range_for(docstring));
|
||||
let (before, _, after) = checker.locator.partition_source_code_at(
|
||||
&Range::from_located(parent),
|
||||
&helpers::range_for(docstring),
|
||||
);
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D201) {
|
||||
let blank_lines_before = before
|
||||
@@ -253,7 +177,7 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio
|
||||
if blank_lines_before != 0 {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoBlankLineBeforeFunction(blank_lines_before),
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -278,7 +202,7 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio
|
||||
{
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoBlankLineAfterFunction(blank_lines_after),
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -298,9 +222,10 @@ pub fn blank_before_after_class(checker: &mut Checker, definition: &Definition)
|
||||
..
|
||||
} = &docstring.node
|
||||
{
|
||||
let (before, _, after) = checker
|
||||
.locator
|
||||
.partition_source_code_at(&Range::from_located(parent), &range_for(docstring));
|
||||
let (before, _, after) = checker.locator.partition_source_code_at(
|
||||
&Range::from_located(parent),
|
||||
&helpers::range_for(docstring),
|
||||
);
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D203)
|
||||
|| checker.settings.enabled.contains(&CheckCode::D211)
|
||||
@@ -311,20 +236,20 @@ pub fn blank_before_after_class(checker: &mut Checker, definition: &Definition)
|
||||
.skip(1)
|
||||
.take_while(|line| line.trim().is_empty())
|
||||
.count();
|
||||
if blank_lines_before != 0
|
||||
&& checker.settings.enabled.contains(&CheckCode::D211)
|
||||
{
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoBlankLineBeforeClass(blank_lines_before),
|
||||
range_for(docstring),
|
||||
));
|
||||
if blank_lines_before != 0 {
|
||||
checker.add_check_if(&CheckCode::D211, || {
|
||||
Check::new(
|
||||
CheckKind::NoBlankLineBeforeClass(blank_lines_before),
|
||||
helpers::range_for(docstring),
|
||||
)
|
||||
});
|
||||
}
|
||||
if blank_lines_before != 1
|
||||
&& checker.settings.enabled.contains(&CheckCode::D203)
|
||||
{
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::OneBlankLineBeforeClass(blank_lines_before),
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -342,7 +267,7 @@ pub fn blank_before_after_class(checker: &mut Checker, definition: &Definition)
|
||||
if !all_blank_after && blank_lines_after != 1 {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::OneBlankLineAfterClass(blank_lines_after),
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -372,13 +297,96 @@ pub fn blank_after_summary(checker: &mut Checker, definition: &Definition) {
|
||||
if lines_count > 1 && blanks_count != 1 {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoBlankLineAfterSummary,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// D206, D207, D208
|
||||
pub fn indent(checker: &mut Checker, definition: &Definition) {
|
||||
if let Some(docstring) = definition.docstring {
|
||||
if let ExprKind::Constant {
|
||||
value: Constant::Str(string),
|
||||
..
|
||||
} = &docstring.node
|
||||
{
|
||||
let lines: Vec<&str> = string.lines().collect();
|
||||
if lines.len() <= 1 {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut has_seen_tab = false;
|
||||
let mut has_seen_over_indent = false;
|
||||
let mut has_seen_under_indent = false;
|
||||
|
||||
let docstring_indent = indentation(checker, docstring).to_string();
|
||||
if !has_seen_tab {
|
||||
if docstring_indent.contains('\t') {
|
||||
if checker.settings.enabled.contains(&CheckCode::D206) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::IndentWithSpaces,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
has_seen_tab = true;
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..lines.len() {
|
||||
// First lines and continuations doesn't need any indentation.
|
||||
if i == 0 || lines[i - 1].ends_with('\\') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Omit empty lines, except for the last line, which is non-empty by way of
|
||||
// containing the closing quotation marks.
|
||||
if i < lines.len() - 1 && lines[i].trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let line_indent = leading_space(lines[i]);
|
||||
if !has_seen_tab {
|
||||
if line_indent.contains('\t') {
|
||||
if checker.settings.enabled.contains(&CheckCode::D206) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::IndentWithSpaces,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
has_seen_tab = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !has_seen_over_indent {
|
||||
if line_indent.len() > docstring_indent.len() {
|
||||
if checker.settings.enabled.contains(&CheckCode::D208) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoOverIndentation,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
has_seen_over_indent = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !has_seen_under_indent {
|
||||
if line_indent.len() < docstring_indent.len() {
|
||||
if checker.settings.enabled.contains(&CheckCode::D207) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoUnderIndentation,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
has_seen_under_indent = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// D209
|
||||
pub fn newline_after_last_paragraph(checker: &mut Checker, definition: &Definition) {
|
||||
if let Some(docstring) = definition.docstring {
|
||||
@@ -395,13 +403,13 @@ pub fn newline_after_last_paragraph(checker: &mut Checker, definition: &Definiti
|
||||
if line_count > 1 {
|
||||
let content = checker
|
||||
.locator
|
||||
.slice_source_code_range(&range_for(docstring));
|
||||
.slice_source_code_range(&helpers::range_for(docstring));
|
||||
if let Some(line) = content.lines().last() {
|
||||
let line = line.trim();
|
||||
if line != "\"\"\"" && line != "'''" {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NewLineAfterLastParagraph,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -428,7 +436,7 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, definition: &Definition)
|
||||
if line.starts_with(' ') || (matches!(lines.next(), None) && line.ends_with(' ')) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoSurroundingWhitespace,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -447,21 +455,23 @@ pub fn multi_line_summary_start(checker: &mut Checker, definition: &Definition)
|
||||
if string.lines().nth(1).is_some() {
|
||||
let content = checker
|
||||
.locator
|
||||
.slice_source_code_range(&range_for(docstring));
|
||||
.slice_source_code_range(&helpers::range_for(docstring));
|
||||
if let Some(first_line) = content.lines().next() {
|
||||
let first_line = first_line.trim();
|
||||
if first_line == "\"\"\"" || first_line == "'''" {
|
||||
if checker.settings.enabled.contains(&CheckCode::D212) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::MultiLineSummaryFirstLine,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
if checker.settings.enabled.contains(&CheckCode::D213) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::MultiLineSummarySecondLine,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
} else if checker.settings.enabled.contains(&CheckCode::D213) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::MultiLineSummarySecondLine,
|
||||
range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -479,18 +489,18 @@ pub fn triple_quotes(checker: &mut Checker, definition: &Definition) {
|
||||
{
|
||||
let content = checker
|
||||
.locator
|
||||
.slice_source_code_range(&range_for(docstring));
|
||||
.slice_source_code_range(&helpers::range_for(docstring));
|
||||
if string.contains("\"\"\"") {
|
||||
if !content.starts_with("'''") {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::UsesTripleQuotes,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
} else if !content.starts_with("\"\"\"") {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::UsesTripleQuotes,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -507,7 +517,10 @@ pub fn ends_with_period(checker: &mut Checker, definition: &Definition) {
|
||||
{
|
||||
if let Some(string) = string.lines().next() {
|
||||
if !string.ends_with('.') {
|
||||
checker.add_check(Check::new(CheckKind::EndsInPeriod, range_for(docstring)));
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::EndsInPeriod,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -531,7 +544,7 @@ pub fn no_signature(checker: &mut Checker, definition: &Definition) {
|
||||
if first_line.contains(&format!("{name}(")) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoSignature,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -566,7 +579,7 @@ pub fn capitalized(checker: &mut Checker, definition: &Definition) {
|
||||
if !first_char.is_uppercase() {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::FirstLineCapitalized,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -594,7 +607,10 @@ pub fn starts_with_this(checker: &mut Checker, definition: &Definition) {
|
||||
.to_lowercase()
|
||||
== "this"
|
||||
{
|
||||
checker.add_check(Check::new(CheckKind::NoThisPrefix, range_for(docstring)));
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoThisPrefix,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -613,7 +629,7 @@ pub fn ends_with_punctuation(checker: &mut Checker, definition: &Definition) {
|
||||
if !(string.ends_with('.') || string.ends_with('!') || string.ends_with('?')) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::EndsInPunctuation,
|
||||
range_for(docstring),
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -648,7 +664,10 @@ pub fn not_empty(checker: &mut Checker, definition: &Definition) -> bool {
|
||||
{
|
||||
if string.trim().is_empty() {
|
||||
if checker.settings.enabled.contains(&CheckCode::D419) {
|
||||
checker.add_check(Check::new(CheckKind::NonEmpty, range_for(docstring)));
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NonEmpty,
|
||||
helpers::range_for(docstring),
|
||||
));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -657,7 +676,8 @@ pub fn not_empty(checker: &mut Checker, definition: &Definition) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn check_sections(checker: &mut Checker, definition: &Definition) {
|
||||
/// D212, D214, D215, D405, D406, D407, D408, D409, D410, D411, D412, D413, D414, D416, D417
|
||||
pub fn sections(checker: &mut Checker, definition: &Definition) {
|
||||
if let Some(docstring) = definition.docstring {
|
||||
if let ExprKind::Constant {
|
||||
value: Constant::Str(string),
|
||||
@@ -668,9 +688,20 @@ pub fn check_sections(checker: &mut Checker, definition: &Definition) {
|
||||
if lines.len() < 2 {
|
||||
return;
|
||||
}
|
||||
for context in §ion_contexts(&lines) {
|
||||
|
||||
// First, interpret as NumPy-style sections.
|
||||
let mut found_numpy_section = false;
|
||||
for context in §ion_contexts(&lines, &SectionStyle::NumPy) {
|
||||
found_numpy_section = true;
|
||||
check_numpy_section(checker, definition, context);
|
||||
}
|
||||
|
||||
// If no such sections were identified, interpret as Google-style sections.
|
||||
if !found_numpy_section {
|
||||
for context in §ion_contexts(&lines, &SectionStyle::Google) {
|
||||
check_google_section(checker, definition, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
82
src/docstrings/extraction.rs
Normal file
82
src/docstrings/extraction.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
//! Extract docstrings from an AST.
|
||||
|
||||
use rustpython_ast::{Constant, Expr, ExprKind, Stmt, StmtKind};
|
||||
|
||||
use crate::docstrings::types::{Definition, DefinitionKind, Documentable};
|
||||
use crate::visibility::{Modifier, VisibleScope};
|
||||
|
||||
/// Extract a docstring from a function or class body.
|
||||
pub fn docstring_from(suite: &[Stmt]) -> Option<&Expr> {
|
||||
if let Some(stmt) = suite.first() {
|
||||
if let StmtKind::Expr { value } = &stmt.node {
|
||||
if matches!(
|
||||
&value.node,
|
||||
ExprKind::Constant {
|
||||
value: Constant::Str(_),
|
||||
..
|
||||
}
|
||||
) {
|
||||
return Some(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Extract a `Definition` from the AST node defined by a `Stmt`.
|
||||
pub fn extract<'a>(
|
||||
scope: &VisibleScope,
|
||||
stmt: &'a Stmt,
|
||||
body: &'a [Stmt],
|
||||
kind: &Documentable,
|
||||
) -> Definition<'a> {
|
||||
let expr = docstring_from(body);
|
||||
match kind {
|
||||
Documentable::Function => match scope {
|
||||
VisibleScope {
|
||||
modifier: Modifier::Module,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::Function(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
VisibleScope {
|
||||
modifier: Modifier::Class,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::Method(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
VisibleScope {
|
||||
modifier: Modifier::Function,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::NestedFunction(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
},
|
||||
Documentable::Class => match scope {
|
||||
VisibleScope {
|
||||
modifier: Modifier::Module,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::Class(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
VisibleScope {
|
||||
modifier: Modifier::Class,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::NestedClass(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
VisibleScope {
|
||||
modifier: Modifier::Function,
|
||||
..
|
||||
} => Definition {
|
||||
kind: DefinitionKind::NestedClass(stmt),
|
||||
docstring: expr,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
150
src/docstrings/google.rs
Normal file
150
src/docstrings/google.rs
Normal file
@@ -0,0 +1,150 @@
|
||||
//! Abstractions for Google-style docstrings.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::check_ast::Checker;
|
||||
use crate::checks::{Check, CheckCode, CheckKind};
|
||||
use crate::docstrings::helpers::range_for;
|
||||
use crate::docstrings::sections;
|
||||
use crate::docstrings::sections::SectionContext;
|
||||
use crate::docstrings::styles::SectionStyle;
|
||||
use crate::docstrings::types::Definition;
|
||||
|
||||
pub(crate) static GOOGLE_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
|
||||
BTreeSet::from([
|
||||
"Args",
|
||||
"Arguments",
|
||||
"Attention",
|
||||
"Attributes",
|
||||
"Caution",
|
||||
"Danger",
|
||||
"Error",
|
||||
"Example",
|
||||
"Examples",
|
||||
"Hint",
|
||||
"Important",
|
||||
"Keyword Args",
|
||||
"Keyword Arguments",
|
||||
"Methods",
|
||||
"Note",
|
||||
"Notes",
|
||||
"Return",
|
||||
"Returns",
|
||||
"Raises",
|
||||
"References",
|
||||
"See Also",
|
||||
"Tip",
|
||||
"Todo",
|
||||
"Warning",
|
||||
"Warnings",
|
||||
"Warns",
|
||||
"Yield",
|
||||
"Yields",
|
||||
])
|
||||
});
|
||||
|
||||
pub(crate) static LOWERCASE_GOOGLE_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
|
||||
BTreeSet::from([
|
||||
"args",
|
||||
"arguments",
|
||||
"attention",
|
||||
"attributes",
|
||||
"caution",
|
||||
"danger",
|
||||
"error",
|
||||
"example",
|
||||
"examples",
|
||||
"hint",
|
||||
"important",
|
||||
"keyword args",
|
||||
"keyword arguments",
|
||||
"methods",
|
||||
"note",
|
||||
"notes",
|
||||
"return",
|
||||
"returns",
|
||||
"raises",
|
||||
"references",
|
||||
"see also",
|
||||
"tip",
|
||||
"todo",
|
||||
"warning",
|
||||
"warnings",
|
||||
"warns",
|
||||
"yield",
|
||||
"yields",
|
||||
])
|
||||
});
|
||||
|
||||
// See: `GOOGLE_ARGS_REGEX` in `pydocstyle/checker.py`.
|
||||
static GOOGLE_ARGS_REGEX: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r"^\s*(\w+)\s*(\(.*?\))?\s*:\n?\s*.+").expect("Invalid regex"));
|
||||
|
||||
fn check_args_section(checker: &mut Checker, definition: &Definition, context: &SectionContext) {
|
||||
let mut args_sections: Vec<String> = vec![];
|
||||
for line in textwrap::dedent(&context.following_lines.join("\n")).lines() {
|
||||
if line
|
||||
.chars()
|
||||
.next()
|
||||
.map(|char| char.is_whitespace())
|
||||
.unwrap_or(true)
|
||||
{
|
||||
// This is a continuation of documentation for the last
|
||||
// parameter because it does start with whitespace.
|
||||
if let Some(current) = args_sections.last_mut() {
|
||||
current.push_str(line);
|
||||
}
|
||||
} else {
|
||||
// This line is the start of documentation for the next
|
||||
// parameter because it doesn't start with any whitespace.
|
||||
args_sections.push(line.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
sections::check_missing_args(
|
||||
checker,
|
||||
definition,
|
||||
// Collect the list of arguments documented in the docstring.
|
||||
&BTreeSet::from_iter(args_sections.iter().filter_map(|section| {
|
||||
match GOOGLE_ARGS_REGEX.captures(section.as_str()) {
|
||||
Some(caps) => caps.get(1).map(|arg_name| arg_name.as_str()),
|
||||
None => None,
|
||||
}
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn check_google_section(
|
||||
checker: &mut Checker,
|
||||
definition: &Definition,
|
||||
context: &SectionContext,
|
||||
) {
|
||||
sections::check_common_section(checker, definition, context, &SectionStyle::Google);
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D416) {
|
||||
let suffix = context
|
||||
.line
|
||||
.trim()
|
||||
.strip_prefix(&context.section_name)
|
||||
.unwrap();
|
||||
if suffix != ":" {
|
||||
let docstring = definition
|
||||
.docstring
|
||||
.expect("Sections are only available for docstrings.");
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::SectionNameEndsInColon(context.section_name.to_string()),
|
||||
range_for(docstring),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D417) {
|
||||
let capitalized_section_name = titlecase::titlecase(&context.section_name);
|
||||
if capitalized_section_name == "Args" || capitalized_section_name == "Arguments" {
|
||||
check_args_section(checker, definition, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/docstrings/helpers.rs
Normal file
37
src/docstrings/helpers.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use rustpython_ast::{Expr, Location};
|
||||
|
||||
use crate::ast::types::Range;
|
||||
use crate::check_ast::Checker;
|
||||
|
||||
/// Extract the leading words from a line of text.
|
||||
pub fn leading_words(line: &str) -> String {
|
||||
line.trim()
|
||||
.chars()
|
||||
.take_while(|char| char.is_alphanumeric() || char.is_whitespace())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Extract the leading whitespace from a line of text.
|
||||
pub fn leading_space(line: &str) -> String {
|
||||
line.chars()
|
||||
.take_while(|char| char.is_whitespace())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Extract the leading indentation from a docstring.
|
||||
pub fn indentation<'a>(checker: &'a mut Checker, docstring: &Expr) -> &'a str {
|
||||
let range = range_for(docstring);
|
||||
checker.locator.slice_source_code_range(&Range {
|
||||
location: Location::new(range.location.row(), 1),
|
||||
end_location: Location::new(range.location.row(), range.location.column()),
|
||||
})
|
||||
}
|
||||
|
||||
/// Extract the source code range for a docstring.
|
||||
pub fn range_for(docstring: &Expr) -> Range {
|
||||
// RustPython currently omits the first quotation mark in a string, so offset the location.
|
||||
Range {
|
||||
location: Location::new(docstring.location.row(), docstring.location.column() - 1),
|
||||
end_location: docstring.end_location,
|
||||
}
|
||||
}
|
||||
114
src/docstrings/numpy.rs
Normal file
114
src/docstrings/numpy.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
//! Abstractions for NumPy-style docstrings.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::check_ast::Checker;
|
||||
use crate::checks::{Check, CheckCode, CheckKind};
|
||||
use crate::docstrings::helpers::range_for;
|
||||
use crate::docstrings::sections::SectionContext;
|
||||
use crate::docstrings::styles::SectionStyle;
|
||||
use crate::docstrings::types::Definition;
|
||||
use crate::docstrings::{helpers, sections};
|
||||
|
||||
pub(crate) static LOWERCASE_NUMPY_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
|
||||
BTreeSet::from([
|
||||
"short summary",
|
||||
"extended summary",
|
||||
"parameters",
|
||||
"returns",
|
||||
"yields",
|
||||
"other parameters",
|
||||
"raises",
|
||||
"see also",
|
||||
"notes",
|
||||
"references",
|
||||
"examples",
|
||||
"attributes",
|
||||
"methods",
|
||||
])
|
||||
});
|
||||
|
||||
pub(crate) static NUMPY_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
|
||||
BTreeSet::from([
|
||||
"Short Summary",
|
||||
"Extended Summary",
|
||||
"Parameters",
|
||||
"Returns",
|
||||
"Yields",
|
||||
"Other Parameters",
|
||||
"Raises",
|
||||
"See Also",
|
||||
"Notes",
|
||||
"References",
|
||||
"Examples",
|
||||
"Attributes",
|
||||
"Methods",
|
||||
])
|
||||
});
|
||||
|
||||
fn check_parameters_section(
|
||||
checker: &mut Checker,
|
||||
definition: &Definition,
|
||||
context: &SectionContext,
|
||||
) {
|
||||
// Collect the list of arguments documented in the docstring.
|
||||
let mut docstring_args: BTreeSet<&str> = Default::default();
|
||||
let section_level_indent = helpers::leading_space(context.line);
|
||||
for i in 1..context.following_lines.len() {
|
||||
let current_line = context.following_lines[i - 1];
|
||||
let current_leading_space = helpers::leading_space(current_line);
|
||||
let next_line = context.following_lines[i];
|
||||
if current_leading_space == section_level_indent
|
||||
&& (helpers::leading_space(next_line).len() > current_leading_space.len())
|
||||
&& !next_line.trim().is_empty()
|
||||
{
|
||||
let parameters = if let Some(semi_index) = current_line.find(':') {
|
||||
// If the parameter has a type annotation, exclude it.
|
||||
¤t_line[..semi_index]
|
||||
} else {
|
||||
// Otherwise, it's just a list of parameters on the current line.
|
||||
current_line.trim()
|
||||
};
|
||||
// Notably, NumPy lets you put multiple parameters of the same type on the same line.
|
||||
for parameter in parameters.split(',') {
|
||||
docstring_args.insert(parameter.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
// Validate that all arguments were documented.
|
||||
sections::check_missing_args(checker, definition, &docstring_args);
|
||||
}
|
||||
|
||||
pub(crate) fn check_numpy_section(
|
||||
checker: &mut Checker,
|
||||
definition: &Definition,
|
||||
context: &SectionContext,
|
||||
) {
|
||||
sections::check_common_section(checker, definition, context, &SectionStyle::NumPy);
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D406) {
|
||||
let suffix = context
|
||||
.line
|
||||
.trim()
|
||||
.strip_prefix(&context.section_name)
|
||||
.unwrap();
|
||||
if !suffix.is_empty() {
|
||||
let docstring = definition
|
||||
.docstring
|
||||
.expect("Sections are only available for docstrings.");
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NewLineAfterSectionName(context.section_name.to_string()),
|
||||
range_for(docstring),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D417) {
|
||||
let capitalized_section_name = titlecase::titlecase(&context.section_name);
|
||||
if capitalized_section_name == "Parameters" {
|
||||
check_parameters_section(checker, definition, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,102 +1,32 @@
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use itertools::Itertools;
|
||||
use rustpython_ast::{Arg, StmtKind};
|
||||
use titlecase::titlecase;
|
||||
|
||||
use crate::ast::types::Range;
|
||||
use crate::check_ast::Checker;
|
||||
use crate::checks::{Check, CheckCode, CheckKind};
|
||||
use crate::docstrings::docstring_checks::range_for;
|
||||
use crate::docstrings::types::Definition;
|
||||
|
||||
static NUMPY_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
|
||||
BTreeSet::from([
|
||||
"Short Summary",
|
||||
"Extended Summary",
|
||||
"Parameters",
|
||||
"Returns",
|
||||
"Yields",
|
||||
"Other Parameters",
|
||||
"Raises",
|
||||
"See Also",
|
||||
"Notes",
|
||||
"References",
|
||||
"Examples",
|
||||
"Attributes",
|
||||
"Methods",
|
||||
])
|
||||
});
|
||||
|
||||
static NUMPY_SECTION_NAMES_LOWERCASE: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
|
||||
BTreeSet::from([
|
||||
"short summary",
|
||||
"extended summary",
|
||||
"parameters",
|
||||
"returns",
|
||||
"yields",
|
||||
"other parameters",
|
||||
"raises",
|
||||
"see also",
|
||||
"notes",
|
||||
"references",
|
||||
"examples",
|
||||
"attributes",
|
||||
"methods",
|
||||
])
|
||||
});
|
||||
|
||||
// TODO(charlie): Include Google section names.
|
||||
// static GOOGLE_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
|
||||
// BTreeSet::from([
|
||||
// "Args",
|
||||
// "Arguments",
|
||||
// "Attention",
|
||||
// "Attributes",
|
||||
// "Caution",
|
||||
// "Danger",
|
||||
// "Error",
|
||||
// "Example",
|
||||
// "Examples",
|
||||
// "Hint",
|
||||
// "Important",
|
||||
// "Keyword Args",
|
||||
// "Keyword Arguments",
|
||||
// "Methods",
|
||||
// "Note",
|
||||
// "Notes",
|
||||
// "Return",
|
||||
// "Returns",
|
||||
// "Raises",
|
||||
// "References",
|
||||
// "See Also",
|
||||
// "Tip",
|
||||
// "Todo",
|
||||
// "Warning",
|
||||
// "Warnings",
|
||||
// "Warns",
|
||||
// "Yield",
|
||||
// "Yields",
|
||||
// ])
|
||||
// });
|
||||
|
||||
fn get_leading_words(line: &str) -> String {
|
||||
line.trim()
|
||||
.chars()
|
||||
.take_while(|char| char.is_alphanumeric() || char.is_whitespace())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn suspected_as_section(line: &str) -> bool {
|
||||
NUMPY_SECTION_NAMES_LOWERCASE.contains(&get_leading_words(line).to_lowercase().as_str())
|
||||
}
|
||||
use crate::docstrings::helpers;
|
||||
use crate::docstrings::helpers::range_for;
|
||||
use crate::docstrings::styles::SectionStyle;
|
||||
use crate::docstrings::types::{Definition, DefinitionKind};
|
||||
use crate::visibility::is_static;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SectionContext<'a> {
|
||||
section_name: String,
|
||||
previous_line: &'a str,
|
||||
line: &'a str,
|
||||
following_lines: &'a [&'a str],
|
||||
pub(crate) struct SectionContext<'a> {
|
||||
pub(crate) section_name: String,
|
||||
pub(crate) previous_line: &'a str,
|
||||
pub(crate) line: &'a str,
|
||||
pub(crate) following_lines: &'a [&'a str],
|
||||
pub(crate) is_last_section: bool,
|
||||
original_index: usize,
|
||||
is_last_section: bool,
|
||||
}
|
||||
|
||||
fn suspected_as_section(line: &str, style: &SectionStyle) -> bool {
|
||||
style
|
||||
.lowercase_section_names()
|
||||
.contains(&helpers::leading_words(line).to_lowercase().as_str())
|
||||
}
|
||||
|
||||
/// Check if the suspected context is really a section header.
|
||||
@@ -127,12 +57,15 @@ fn is_docstring_section(context: &SectionContext) -> bool {
|
||||
}
|
||||
|
||||
/// Extract all `SectionContext` values from a docstring.
|
||||
pub fn section_contexts<'a>(lines: &'a [&'a str]) -> Vec<SectionContext<'a>> {
|
||||
pub(crate) fn section_contexts<'a>(
|
||||
lines: &'a [&'a str],
|
||||
style: &SectionStyle,
|
||||
) -> Vec<SectionContext<'a>> {
|
||||
let suspected_section_indices: Vec<usize> = lines
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(lineno, line)| {
|
||||
if lineno > 0 && suspected_as_section(line) {
|
||||
if lineno > 0 && suspected_as_section(line, style) {
|
||||
Some(lineno)
|
||||
} else {
|
||||
None
|
||||
@@ -143,7 +76,7 @@ pub fn section_contexts<'a>(lines: &'a [&'a str]) -> Vec<SectionContext<'a>> {
|
||||
let mut contexts = vec![];
|
||||
for lineno in suspected_section_indices {
|
||||
let context = SectionContext {
|
||||
section_name: get_leading_words(lines[lineno]),
|
||||
section_name: helpers::leading_words(lines[lineno]),
|
||||
previous_line: lines[lineno - 1],
|
||||
line: lines[lineno],
|
||||
following_lines: &lines[lineno + 1..],
|
||||
@@ -196,14 +129,12 @@ fn check_blanks_and_section_underline(
|
||||
|
||||
// Nothing but blank lines after the section header.
|
||||
if blank_lines_after_header == context.following_lines.len() {
|
||||
// D407
|
||||
if checker.settings.enabled.contains(&CheckCode::D407) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::DashedUnderlineAfterSection(context.section_name.to_string()),
|
||||
range_for(docstring),
|
||||
));
|
||||
}
|
||||
// D414
|
||||
if checker.settings.enabled.contains(&CheckCode::D414) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NonEmptySection(context.section_name.to_string()),
|
||||
@@ -219,7 +150,6 @@ fn check_blanks_and_section_underline(
|
||||
.all(|char| char.is_whitespace() || char == '-');
|
||||
|
||||
if !dash_line_found {
|
||||
// D407
|
||||
if checker.settings.enabled.contains(&CheckCode::D407) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::DashedUnderlineAfterSection(context.section_name.to_string()),
|
||||
@@ -227,7 +157,6 @@ fn check_blanks_and_section_underline(
|
||||
));
|
||||
}
|
||||
if blank_lines_after_header > 0 {
|
||||
// D212
|
||||
if checker.settings.enabled.contains(&CheckCode::D212) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoBlankLinesBetweenHeaderAndContent(
|
||||
@@ -239,7 +168,6 @@ fn check_blanks_and_section_underline(
|
||||
}
|
||||
} else {
|
||||
if blank_lines_after_header > 0 {
|
||||
// D408
|
||||
if checker.settings.enabled.contains(&CheckCode::D408) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::SectionUnderlineAfterName(context.section_name.to_string()),
|
||||
@@ -255,7 +183,6 @@ fn check_blanks_and_section_underline(
|
||||
.count()
|
||||
!= context.section_name.len()
|
||||
{
|
||||
// D409
|
||||
if checker.settings.enabled.contains(&CheckCode::D409) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::SectionUnderlineMatchesSectionLength(
|
||||
@@ -266,16 +193,24 @@ fn check_blanks_and_section_underline(
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(charlie): Implement D215, which requires indentation and leading space tracking.
|
||||
if checker.settings.enabled.contains(&CheckCode::D215) {
|
||||
if helpers::leading_space(non_empty_line).len()
|
||||
> helpers::indentation(checker, docstring).len()
|
||||
{
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::SectionUnderlineNotOverIndented(context.section_name.to_string()),
|
||||
range_for(docstring),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let line_after_dashes_index = blank_lines_after_header + 1;
|
||||
|
||||
if line_after_dashes_index < context.following_lines.len() {
|
||||
let line_after_dashes = context.following_lines[line_after_dashes_index];
|
||||
|
||||
if line_after_dashes.trim().is_empty() {
|
||||
let rest_of_lines = &context.following_lines[line_after_dashes_index..];
|
||||
if rest_of_lines.iter().all(|line| line.trim().is_empty()) {
|
||||
// D414
|
||||
if checker.settings.enabled.contains(&CheckCode::D414) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NonEmptySection(context.section_name.to_string()),
|
||||
@@ -283,7 +218,6 @@ fn check_blanks_and_section_underline(
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// 412
|
||||
if checker.settings.enabled.contains(&CheckCode::D412) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NoBlankLinesBetweenHeaderAndContent(
|
||||
@@ -295,7 +229,6 @@ fn check_blanks_and_section_underline(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// D414
|
||||
if checker.settings.enabled.contains(&CheckCode::D414) {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NonEmptySection(context.section_name.to_string()),
|
||||
@@ -306,15 +239,23 @@ fn check_blanks_and_section_underline(
|
||||
}
|
||||
}
|
||||
|
||||
fn check_common_section(checker: &mut Checker, definition: &Definition, context: &SectionContext) {
|
||||
// TODO(charlie): Implement D214, which requires indentation and leading space tracking.
|
||||
pub(crate) fn check_common_section(
|
||||
checker: &mut Checker,
|
||||
definition: &Definition,
|
||||
context: &SectionContext,
|
||||
style: &SectionStyle,
|
||||
) {
|
||||
let docstring = definition
|
||||
.docstring
|
||||
.expect("Sections are only available for docstrings.");
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D405) {
|
||||
if !NUMPY_SECTION_NAMES.contains(&context.section_name.as_str())
|
||||
&& NUMPY_SECTION_NAMES.contains(titlecase(&context.section_name).as_str())
|
||||
if !style
|
||||
.section_names()
|
||||
.contains(&context.section_name.as_str())
|
||||
&& style
|
||||
.section_names()
|
||||
.contains(titlecase(&context.section_name).as_str())
|
||||
{
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::CapitalizeSectionName(context.section_name.to_string()),
|
||||
@@ -323,6 +264,17 @@ fn check_common_section(checker: &mut Checker, definition: &Definition, context:
|
||||
}
|
||||
}
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D214) {
|
||||
if helpers::leading_space(context.line).len()
|
||||
> helpers::indentation(checker, docstring).len()
|
||||
{
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::SectionNotOverIndented(context.section_name.to_string()),
|
||||
range_for(docstring),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
if context
|
||||
.following_lines
|
||||
.last()
|
||||
@@ -354,31 +306,72 @@ fn check_common_section(checker: &mut Checker, definition: &Definition, context:
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
check_blanks_and_section_underline(checker, definition, context);
|
||||
}
|
||||
|
||||
pub fn check_numpy_section(
|
||||
pub(crate) fn check_missing_args(
|
||||
checker: &mut Checker,
|
||||
definition: &Definition,
|
||||
context: &SectionContext,
|
||||
docstrings_args: &BTreeSet<&str>,
|
||||
) {
|
||||
// TODO(charlie): Implement `_check_parameters_section`.
|
||||
check_common_section(checker, definition, context);
|
||||
check_blanks_and_section_underline(checker, definition, context);
|
||||
if let DefinitionKind::Function(parent)
|
||||
| DefinitionKind::NestedFunction(parent)
|
||||
| DefinitionKind::Method(parent) = definition.kind
|
||||
{
|
||||
if let StmtKind::FunctionDef {
|
||||
args: arguments, ..
|
||||
}
|
||||
| StmtKind::AsyncFunctionDef {
|
||||
args: arguments, ..
|
||||
} = &parent.node
|
||||
{
|
||||
// Collect all the arguments into a single vector.
|
||||
let mut all_arguments: Vec<&Arg> = arguments
|
||||
.args
|
||||
.iter()
|
||||
.chain(arguments.posonlyargs.iter())
|
||||
.chain(arguments.kwonlyargs.iter())
|
||||
.skip(
|
||||
// If this is a non-static method, skip `cls` or `self`.
|
||||
if matches!(definition.kind, DefinitionKind::Method(_)) && !is_static(parent) {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
},
|
||||
)
|
||||
.collect();
|
||||
if let Some(arg) = &arguments.vararg {
|
||||
all_arguments.push(arg);
|
||||
}
|
||||
if let Some(arg) = &arguments.kwarg {
|
||||
all_arguments.push(arg);
|
||||
}
|
||||
|
||||
if checker.settings.enabled.contains(&CheckCode::D406) {
|
||||
let suffix = context
|
||||
.line
|
||||
.trim()
|
||||
.strip_prefix(&context.section_name)
|
||||
.unwrap();
|
||||
if !suffix.is_empty() {
|
||||
let docstring = definition
|
||||
.docstring
|
||||
.expect("Sections are only available for docstrings.");
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::NewLineAfterSectionName(context.section_name.to_string()),
|
||||
range_for(docstring),
|
||||
))
|
||||
// Look for arguments that weren't included in the docstring.
|
||||
let mut missing_args: BTreeSet<&str> = Default::default();
|
||||
for arg in all_arguments {
|
||||
let arg_name = arg.node.arg.as_str();
|
||||
if arg_name.starts_with('_') {
|
||||
continue;
|
||||
}
|
||||
if docstrings_args.contains(&arg_name) {
|
||||
continue;
|
||||
}
|
||||
missing_args.insert(arg_name);
|
||||
}
|
||||
|
||||
if !missing_args.is_empty() {
|
||||
let names = missing_args
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.sorted()
|
||||
.collect();
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::DocumentAllArguments(names),
|
||||
Range::from_located(parent),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
27
src/docstrings/styles.rs
Normal file
27
src/docstrings/styles.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::docstrings::google::{GOOGLE_SECTION_NAMES, LOWERCASE_GOOGLE_SECTION_NAMES};
|
||||
use crate::docstrings::numpy::{LOWERCASE_NUMPY_SECTION_NAMES, NUMPY_SECTION_NAMES};
|
||||
|
||||
pub(crate) enum SectionStyle {
|
||||
NumPy,
|
||||
Google,
|
||||
}
|
||||
|
||||
impl SectionStyle {
|
||||
pub(crate) fn section_names(&self) -> &Lazy<BTreeSet<&'static str>> {
|
||||
match self {
|
||||
SectionStyle::NumPy => &NUMPY_SECTION_NAMES,
|
||||
SectionStyle::Google => &GOOGLE_SECTION_NAMES,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn lowercase_section_names(&self) -> &Lazy<BTreeSet<&'static str>> {
|
||||
match self {
|
||||
SectionStyle::NumPy => &LOWERCASE_NUMPY_SECTION_NAMES,
|
||||
SectionStyle::Google => &LOWERCASE_GOOGLE_SECTION_NAMES,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#![allow(clippy::collapsible_if, clippy::collapsible_else_if)]
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
1487
src/linter.rs
1487
src/linter.rs
File diff suppressed because it is too large
Load Diff
26
src/main.rs
26
src/main.rs
@@ -81,9 +81,9 @@ fn read_from_stdin() -> Result<String> {
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
fn run_once_stdin(settings: &Settings, filename: &Path) -> Result<Vec<Message>> {
|
||||
fn run_once_stdin(settings: &Settings, filename: &Path, autofix: bool) -> Result<Vec<Message>> {
|
||||
let stdin = read_from_stdin()?;
|
||||
let mut messages = lint_stdin(filename, &stdin, settings)?;
|
||||
let mut messages = lint_stdin(filename, &stdin, settings, &autofix.into())?;
|
||||
messages.sort_unstable();
|
||||
Ok(messages)
|
||||
}
|
||||
@@ -216,7 +216,9 @@ fn inner_main() -> Result<ExitCode> {
|
||||
Some(path) => debug!("Found project root at: {:?}", path),
|
||||
None => debug!("Unable to identify project root; assuming current directory..."),
|
||||
};
|
||||
let pyproject = pyproject::find_pyproject_toml(&project_root);
|
||||
let pyproject = cli
|
||||
.config
|
||||
.or_else(|| pyproject::find_pyproject_toml(&project_root));
|
||||
match &pyproject {
|
||||
Some(path) => debug!("Found pyproject.toml at: {:?}", path),
|
||||
None => debug!("Unable to find pyproject.toml; using default settings..."),
|
||||
@@ -365,18 +367,20 @@ fn inner_main() -> Result<ExitCode> {
|
||||
println!("Formatted {modifications} files.");
|
||||
}
|
||||
} else {
|
||||
let messages = if cli.files == vec![PathBuf::from("-")] {
|
||||
if cli.fix {
|
||||
eprintln!("Warning: --fix is not enabled when reading from stdin.");
|
||||
}
|
||||
|
||||
let (messages, print_messages) = if cli.files == vec![PathBuf::from("-")] {
|
||||
let filename = cli.stdin_filename.unwrap_or_else(|| "-".to_string());
|
||||
let path = Path::new(&filename);
|
||||
run_once_stdin(&settings, path)?
|
||||
(
|
||||
run_once_stdin(&settings, path, cli.fix)?,
|
||||
!cli.quiet && !cli.fix,
|
||||
)
|
||||
} else {
|
||||
run_once(&cli.files, &settings, !cli.no_cache, cli.fix)?
|
||||
(
|
||||
run_once(&cli.files, &settings, !cli.no_cache, cli.fix)?,
|
||||
!cli.quiet,
|
||||
)
|
||||
};
|
||||
if !cli.quiet {
|
||||
if print_messages {
|
||||
printer.write_once(&messages)?;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind: NoNewLineAtEndOfFile
|
||||
- kind: UnnecessaryListCall
|
||||
location:
|
||||
row: 2
|
||||
column: 9
|
||||
column: 1
|
||||
end_location:
|
||||
row: 2
|
||||
column: 9
|
||||
column: 21
|
||||
fix: ~
|
||||
|
||||
32
src/snapshots/ruff__linter__tests__C413_C413.py.snap
Normal file
32
src/snapshots/ruff__linter__tests__C413_C413.py.snap
Normal file
@@ -0,0 +1,32 @@
|
||||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind:
|
||||
UnnecessaryCallAroundSorted: list
|
||||
location:
|
||||
row: 2
|
||||
column: 1
|
||||
end_location:
|
||||
row: 2
|
||||
column: 16
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryCallAroundSorted: reversed
|
||||
location:
|
||||
row: 3
|
||||
column: 1
|
||||
end_location:
|
||||
row: 3
|
||||
column: 20
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryCallAroundSorted: reversed
|
||||
location:
|
||||
row: 4
|
||||
column: 1
|
||||
end_location:
|
||||
row: 4
|
||||
column: 34
|
||||
fix: ~
|
||||
|
||||
23
src/snapshots/ruff__linter__tests__C416_C416.py.snap
Normal file
23
src/snapshots/ruff__linter__tests__C416_C416.py.snap
Normal file
@@ -0,0 +1,23 @@
|
||||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind:
|
||||
UnnecessaryComprehension: list
|
||||
location:
|
||||
row: 2
|
||||
column: 1
|
||||
end_location:
|
||||
row: 2
|
||||
column: 15
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryComprehension: set
|
||||
location:
|
||||
row: 3
|
||||
column: 1
|
||||
end_location:
|
||||
row: 3
|
||||
column: 15
|
||||
fix: ~
|
||||
|
||||
77
src/snapshots/ruff__linter__tests__C417_C417.py.snap
Normal file
77
src/snapshots/ruff__linter__tests__C417_C417.py.snap
Normal file
@@ -0,0 +1,77 @@
|
||||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind:
|
||||
UnnecessaryMap: generator
|
||||
location:
|
||||
row: 2
|
||||
column: 1
|
||||
end_location:
|
||||
row: 2
|
||||
column: 27
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryMap: generator
|
||||
location:
|
||||
row: 3
|
||||
column: 1
|
||||
end_location:
|
||||
row: 3
|
||||
column: 28
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryMap: list
|
||||
location:
|
||||
row: 4
|
||||
column: 1
|
||||
end_location:
|
||||
row: 4
|
||||
column: 33
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryMap: generator
|
||||
location:
|
||||
row: 4
|
||||
column: 6
|
||||
end_location:
|
||||
row: 4
|
||||
column: 32
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryMap: set
|
||||
location:
|
||||
row: 5
|
||||
column: 1
|
||||
end_location:
|
||||
row: 5
|
||||
column: 37
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryMap: generator
|
||||
location:
|
||||
row: 5
|
||||
column: 5
|
||||
end_location:
|
||||
row: 5
|
||||
column: 36
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryMap: dict
|
||||
location:
|
||||
row: 6
|
||||
column: 1
|
||||
end_location:
|
||||
row: 6
|
||||
column: 37
|
||||
fix: ~
|
||||
- kind:
|
||||
UnnecessaryMap: generator
|
||||
location:
|
||||
row: 6
|
||||
column: 6
|
||||
end_location:
|
||||
row: 6
|
||||
column: 36
|
||||
fix: ~
|
||||
|
||||
29
src/snapshots/ruff__linter__tests__D207_D.py.snap
Normal file
29
src/snapshots/ruff__linter__tests__D207_D.py.snap
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind: NoUnderIndentation
|
||||
location:
|
||||
row: 225
|
||||
column: 5
|
||||
end_location:
|
||||
row: 229
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind: NoUnderIndentation
|
||||
location:
|
||||
row: 235
|
||||
column: 5
|
||||
end_location:
|
||||
row: 239
|
||||
column: 4
|
||||
fix: ~
|
||||
- kind: NoUnderIndentation
|
||||
location:
|
||||
row: 433
|
||||
column: 37
|
||||
end_location:
|
||||
row: 436
|
||||
column: 8
|
||||
fix: ~
|
||||
|
||||
29
src/snapshots/ruff__linter__tests__D208_D.py.snap
Normal file
29
src/snapshots/ruff__linter__tests__D208_D.py.snap
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind: NoOverIndentation
|
||||
location:
|
||||
row: 245
|
||||
column: 5
|
||||
end_location:
|
||||
row: 249
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind: NoOverIndentation
|
||||
location:
|
||||
row: 255
|
||||
column: 5
|
||||
end_location:
|
||||
row: 259
|
||||
column: 12
|
||||
fix: ~
|
||||
- kind: NoOverIndentation
|
||||
location:
|
||||
row: 265
|
||||
column: 5
|
||||
end_location:
|
||||
row: 269
|
||||
column: 8
|
||||
fix: ~
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind: FitsOnOneLine
|
||||
- kind:
|
||||
SectionNotOverIndented: Returns
|
||||
location:
|
||||
row: 124
|
||||
row: 135
|
||||
column: 5
|
||||
end_location:
|
||||
row: 126
|
||||
row: 141
|
||||
column: 8
|
||||
fix: ~
|
||||
|
||||
23
src/snapshots/ruff__linter__tests__D215_sections.py.snap
Normal file
23
src/snapshots/ruff__linter__tests__D215_sections.py.snap
Normal file
@@ -0,0 +1,23 @@
|
||||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind:
|
||||
SectionUnderlineNotOverIndented: Returns
|
||||
location:
|
||||
row: 147
|
||||
column: 5
|
||||
end_location:
|
||||
row: 153
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind:
|
||||
SectionUnderlineNotOverIndented: Returns
|
||||
location:
|
||||
row: 161
|
||||
column: 5
|
||||
end_location:
|
||||
row: 165
|
||||
column: 8
|
||||
fix: ~
|
||||
|
||||
140
src/snapshots/ruff__linter__tests__D407_sections.py.snap
Normal file
140
src/snapshots/ruff__linter__tests__D407_sections.py.snap
Normal file
@@ -0,0 +1,140 @@
|
||||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Returns
|
||||
location:
|
||||
row: 42
|
||||
column: 5
|
||||
end_location:
|
||||
row: 47
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Returns
|
||||
location:
|
||||
row: 54
|
||||
column: 5
|
||||
end_location:
|
||||
row: 58
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Raises
|
||||
location:
|
||||
row: 207
|
||||
column: 5
|
||||
end_location:
|
||||
row: 221
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Returns
|
||||
location:
|
||||
row: 252
|
||||
column: 5
|
||||
end_location:
|
||||
row: 262
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Raises
|
||||
location:
|
||||
row: 252
|
||||
column: 5
|
||||
end_location:
|
||||
row: 262
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 269
|
||||
column: 5
|
||||
end_location:
|
||||
row: 274
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 284
|
||||
column: 9
|
||||
end_location:
|
||||
row: 292
|
||||
column: 12
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 301
|
||||
column: 5
|
||||
end_location:
|
||||
row: 306
|
||||
column: 8
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 313
|
||||
column: 9
|
||||
end_location:
|
||||
row: 319
|
||||
column: 12
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 325
|
||||
column: 9
|
||||
end_location:
|
||||
row: 330
|
||||
column: 12
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 337
|
||||
column: 9
|
||||
end_location:
|
||||
row: 343
|
||||
column: 12
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 350
|
||||
column: 9
|
||||
end_location:
|
||||
row: 355
|
||||
column: 12
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 362
|
||||
column: 9
|
||||
end_location:
|
||||
row: 367
|
||||
column: 12
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 371
|
||||
column: 9
|
||||
end_location:
|
||||
row: 382
|
||||
column: 12
|
||||
fix: ~
|
||||
- kind:
|
||||
DashedUnderlineAfterSection: Args
|
||||
location:
|
||||
row: 490
|
||||
column: 9
|
||||
end_location:
|
||||
row: 497
|
||||
column: 12
|
||||
fix: ~
|
||||
|
||||
127
src/snapshots/ruff__linter__tests__D417_sections.py.snap
Normal file
127
src/snapshots/ruff__linter__tests__D417_sections.py.snap
Normal file
@@ -0,0 +1,127 @@
|
||||
---
|
||||
source: src/linter.rs
|
||||
expression: checks
|
||||
---
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- y
|
||||
location:
|
||||
row: 283
|
||||
column: 5
|
||||
end_location:
|
||||
row: 296
|
||||
column: 1
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- y
|
||||
location:
|
||||
row: 300
|
||||
column: 1
|
||||
end_location:
|
||||
row: 309
|
||||
column: 1
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- test
|
||||
- y
|
||||
- z
|
||||
location:
|
||||
row: 324
|
||||
column: 5
|
||||
end_location:
|
||||
row: 332
|
||||
column: 5
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- test
|
||||
- y
|
||||
- z
|
||||
location:
|
||||
row: 336
|
||||
column: 5
|
||||
end_location:
|
||||
row: 345
|
||||
column: 5
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- a
|
||||
- y
|
||||
- z
|
||||
location:
|
||||
row: 349
|
||||
column: 5
|
||||
end_location:
|
||||
row: 357
|
||||
column: 5
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- a
|
||||
- b
|
||||
location:
|
||||
row: 361
|
||||
column: 5
|
||||
end_location:
|
||||
row: 369
|
||||
column: 5
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- y
|
||||
location:
|
||||
row: 389
|
||||
column: 1
|
||||
end_location:
|
||||
row: 401
|
||||
column: 1
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- test
|
||||
- y
|
||||
- z
|
||||
location:
|
||||
row: 425
|
||||
column: 5
|
||||
end_location:
|
||||
row: 436
|
||||
column: 5
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- test
|
||||
- y
|
||||
- z
|
||||
location:
|
||||
row: 440
|
||||
column: 5
|
||||
end_location:
|
||||
row: 455
|
||||
column: 5
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- a
|
||||
- z
|
||||
location:
|
||||
row: 459
|
||||
column: 5
|
||||
end_location:
|
||||
row: 471
|
||||
column: 5
|
||||
fix: ~
|
||||
- kind:
|
||||
DocumentAllArguments:
|
||||
- y
|
||||
location:
|
||||
row: 489
|
||||
column: 5
|
||||
end_location:
|
||||
row: 498
|
||||
column: 1
|
||||
fix: ~
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user