Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rst backticks rule to pygrep-hooks #1626

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ For more, see [pygrep-hooks](https://github.com/pre-commit/pygrep-hooks) on GitH
| PGH002 | DeprecatedLogWarn | `warn` is deprecated in favor of `warning` | |
| PGH003 | BlanketTypeIgnore | Use specific error codes when ignoring type issues | |
| PGH004 | BlanketNOQA | Use specific error codes when using `noqa` | |
| PGH005 | RstBackticks | Use two backticks when writing RST | |

### Pylint (PLC, PLE, PLR, PLW)

Expand Down
10 changes: 10 additions & 0 deletions resources/test/fixtures/pygrep-hooks/PGH005_0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def fn() -> None:
"""
``[code]``
i like _`kitty`
i like `kitty`_
``b``
``ef``
indented `literal` blocks
"""
return None
11 changes: 11 additions & 0 deletions resources/test/fixtures/pygrep-hooks/PGH005_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def fn() -> None:
"""
`[code]`
i like `_kitty`
i like `_`
`a`
`cd`
`indented` literal block
> quoted `literal` block
"""
return None
1 change: 1 addition & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@
"PGH002",
"PGH003",
"PGH004",
"PGH005",
"PIE",
"PIE7",
"PIE79",
Expand Down
11 changes: 10 additions & 1 deletion src/checkers/lines.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Lint rules based on checking raw physical lines.

use crate::pycodestyle::checks::{line_too_long, no_newline_at_end_of_file};
use crate::pygrep_hooks::plugins::{blanket_noqa, blanket_type_ignore};
use crate::pygrep_hooks::plugins::{blanket_noqa, blanket_type_ignore, rst_backticks};
use crate::pyupgrade::checks::unnecessary_coding_comment;
use crate::registry::{Check, CheckCode};
use crate::settings::{flags, Settings};
Expand All @@ -19,6 +19,7 @@ pub fn check_lines(
let enforce_no_newline_at_end_of_file = settings.enabled.contains(&CheckCode::W292);
let enforce_blanket_type_ignore = settings.enabled.contains(&CheckCode::PGH003);
let enforce_blanket_noqa = settings.enabled.contains(&CheckCode::PGH004);
let enforce_rst_backticks = settings.enabled.contains(&CheckCode::PGH005);

let mut commented_lines_iter = commented_lines.iter().peekable();
for (index, line) in contents.lines().enumerate() {
Expand Down Expand Up @@ -54,6 +55,14 @@ pub fn check_lines(
}
}
}

if enforce_rst_backticks {
if commented_lines.contains(&(index + 1)) {
if let Some(check) = rst_backticks(index, line) {
checks.push(check);
}
}
}
}

if enforce_line_too_long {
Expand Down
2 changes: 2 additions & 0 deletions src/pygrep_hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ mod tests {
#[test_case(CheckCode::PGH002, Path::new("PGH002_1.py"); "PGH002_1")]
#[test_case(CheckCode::PGH003, Path::new("PGH003_0.py"); "PGH003_0")]
#[test_case(CheckCode::PGH004, Path::new("PGH004_0.py"); "PGH004_0")]
#[test_case(CheckCode::PGH005, Path::new("PGH005_0.py"); "PGH005_0")]
#[test_case(CheckCode::PGH005, Path::new("PGH005_1.py"); "PGH005_1")]
fn checks(check_code: CheckCode, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", check_code.as_ref(), path.to_string_lossy());
let checks = test_path(
Expand Down
2 changes: 2 additions & 0 deletions src/pygrep_hooks/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ pub use blanket_noqa::blanket_noqa;
pub use blanket_type_ignore::blanket_type_ignore;
pub use deprecated_log_warn::deprecated_log_warn;
pub use no_eval::no_eval;
pub use rst_backticks::rst_backticks;

mod blanket_noqa;
mod blanket_type_ignore;
mod deprecated_log_warn;
mod no_eval;
mod rst_backticks;
22 changes: 22 additions & 0 deletions src/pygrep_hooks/plugins/rst_backticks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use once_cell::sync::Lazy;
use regex::Regex;
use rustpython_ast::Location;

use crate::ast::types::Range;
use crate::registry::{Check, CheckKind};

static RST_BACKTICKS_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(?! ).*(^| )`[^`]+`([^_]|$)").unwrap());

/// PGH005 - Use two backticks when writing RST
pub fn rst_backticks(lineno: usize, line: &str) -> Option<Check> {
RST_BACKTICKS_REGEX.find(line).map(|m| {
Check::new(
CheckKind::RstBackticks,
Range::new(
Location::new(lineno + 1, m.start()),
Location::new(lineno + 1, m.end()),
),
)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/pygrep_hooks/mod.rs
expression: checks
---
[]

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/pygrep_hooks/mod.rs
expression: checks
---
[]

9 changes: 8 additions & 1 deletion src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ pub enum CheckCode {
PGH002,
PGH003,
PGH004,
PGH005,
// pandas-vet
PD002,
PD003,
Expand Down Expand Up @@ -1061,6 +1062,7 @@ pub enum CheckKind {
DeprecatedLogWarn,
BlanketTypeIgnore,
BlanketNOQA,
RstBackticks,
// flake8-unused-arguments
UnusedFunctionArgument(String),
UnusedMethodArgument(String),
Expand Down Expand Up @@ -1144,7 +1146,8 @@ impl CheckCode {
| CheckCode::W292
| CheckCode::UP009
| CheckCode::PGH003
| CheckCode::PGH004 => &LintSource::Lines,
| CheckCode::PGH004
| CheckCode::PGH005 => &LintSource::Lines,
CheckCode::ERA001
| CheckCode::ISC001
| CheckCode::ISC002
Expand Down Expand Up @@ -1507,6 +1510,7 @@ impl CheckCode {
CheckCode::PGH002 => CheckKind::DeprecatedLogWarn,
CheckCode::PGH003 => CheckKind::BlanketTypeIgnore,
CheckCode::PGH004 => CheckKind::BlanketNOQA,
CheckCode::PGH005 => CheckKind::RstBackticks,
// flake8-unused-arguments
CheckCode::ARG001 => CheckKind::UnusedFunctionArgument("...".to_string()),
CheckCode::ARG002 => CheckKind::UnusedMethodArgument("...".to_string()),
Expand Down Expand Up @@ -1836,6 +1840,7 @@ impl CheckCode {
CheckCode::PGH002 => CheckCategory::PygrepHooks,
CheckCode::PGH003 => CheckCategory::PygrepHooks,
CheckCode::PGH004 => CheckCategory::PygrepHooks,
CheckCode::PGH005 => CheckCategory::PygrepHooks,
// pylint
CheckCode::PLC0414 => CheckCategory::Pylint,
CheckCode::PLC2201 => CheckCategory::Pylint,
Expand Down Expand Up @@ -2262,6 +2267,7 @@ impl CheckKind {
CheckKind::DeprecatedLogWarn => &CheckCode::PGH002,
CheckKind::BlanketTypeIgnore => &CheckCode::PGH003,
CheckKind::BlanketNOQA => &CheckCode::PGH004,
CheckKind::RstBackticks => &CheckCode::PGH005,
// flake8-unused-arguments
CheckKind::UnusedFunctionArgument(..) => &CheckCode::ARG001,
CheckKind::UnusedMethodArgument(..) => &CheckCode::ARG002,
Expand Down Expand Up @@ -3184,6 +3190,7 @@ impl CheckKind {
"Boolean positional value in function call".to_string()
}
// pygrep-hooks
CheckKind::RstBackticks => "Use two backticks when writing RST".to_string(),
CheckKind::BlanketNOQA => "Use specific error codes when using `noqa`".to_string(),
CheckKind::BlanketTypeIgnore => {
"Use specific error codes when ignoring type issues".to_string()
Expand Down
7 changes: 7 additions & 0 deletions src/registry_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ pub enum CheckCodePrefix {
PGH002,
PGH003,
PGH004,
PGH005,
PIE,
PIE7,
PIE79,
Expand Down Expand Up @@ -920,6 +921,7 @@ impl CheckCodePrefix {
CheckCode::PGH002,
CheckCode::PGH003,
CheckCode::PGH004,
CheckCode::PGH005,
CheckCode::PD002,
CheckCode::PD003,
CheckCode::PD004,
Expand Down Expand Up @@ -2176,23 +2178,27 @@ impl CheckCodePrefix {
CheckCode::PGH002,
CheckCode::PGH003,
CheckCode::PGH004,
CheckCode::PGH005,
],
CheckCodePrefix::PGH0 => vec![
CheckCode::PGH001,
CheckCode::PGH002,
CheckCode::PGH003,
CheckCode::PGH004,
CheckCode::PGH005,
],
CheckCodePrefix::PGH00 => vec![
CheckCode::PGH001,
CheckCode::PGH002,
CheckCode::PGH003,
CheckCode::PGH004,
CheckCode::PGH005,
],
CheckCodePrefix::PGH001 => vec![CheckCode::PGH001],
CheckCodePrefix::PGH002 => vec![CheckCode::PGH002],
CheckCodePrefix::PGH003 => vec![CheckCode::PGH003],
CheckCodePrefix::PGH004 => vec![CheckCode::PGH004],
CheckCodePrefix::PGH005 => vec![CheckCode::PGH005],
CheckCodePrefix::PIE => vec![CheckCode::PIE790, CheckCode::PIE794, CheckCode::PIE807],
CheckCodePrefix::PIE7 => vec![CheckCode::PIE790, CheckCode::PIE794],
CheckCodePrefix::PIE79 => vec![CheckCode::PIE790, CheckCode::PIE794],
Expand Down Expand Up @@ -3432,6 +3438,7 @@ impl CheckCodePrefix {
CheckCodePrefix::PGH002 => SuffixLength::Three,
CheckCodePrefix::PGH003 => SuffixLength::Three,
CheckCodePrefix::PGH004 => SuffixLength::Three,
CheckCodePrefix::PGH005 => SuffixLength::Three,
CheckCodePrefix::PIE => SuffixLength::Zero,
CheckCodePrefix::PIE7 => SuffixLength::One,
CheckCodePrefix::PIE79 => SuffixLength::Two,
Expand Down