mirror of https://github.com/astral-sh/ruff
Exclude function definition from too-many-statements rule (#4794)
This commit is contained in:
parent
ea3cbcc362
commit
10ba79489a
|
|
@ -151,7 +151,7 @@ pub(crate) fn too_many_statements(
|
||||||
max_statements: usize,
|
max_statements: usize,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
) -> Option<Diagnostic> {
|
) -> Option<Diagnostic> {
|
||||||
let statements = num_statements(body) + 1;
|
let statements = num_statements(body);
|
||||||
if statements > max_statements {
|
if statements > max_statements {
|
||||||
Some(Diagnostic::new(
|
Some(Diagnostic::new(
|
||||||
TooManyStatements {
|
TooManyStatements {
|
||||||
|
|
@ -176,7 +176,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn pass() -> Result<()> {
|
fn pass() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 2
|
def f():
|
||||||
pass
|
pass
|
||||||
"#;
|
"#;
|
||||||
let stmts = Suite::parse(source, "<filename>")?;
|
let stmts = Suite::parse(source, "<filename>")?;
|
||||||
|
|
@ -216,7 +216,7 @@ def f():
|
||||||
#[test]
|
#[test]
|
||||||
fn if_elif() -> Result<()> {
|
fn if_elif() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 5
|
def f():
|
||||||
if a:
|
if a:
|
||||||
print()
|
print()
|
||||||
elif a:
|
elif a:
|
||||||
|
|
@ -230,7 +230,7 @@ def f(): # 5
|
||||||
#[test]
|
#[test]
|
||||||
fn if_elif_else() -> Result<()> {
|
fn if_elif_else() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 9
|
def f():
|
||||||
if a:
|
if a:
|
||||||
print()
|
print()
|
||||||
elif a == 2:
|
elif a == 2:
|
||||||
|
|
@ -248,7 +248,7 @@ def f(): # 9
|
||||||
#[test]
|
#[test]
|
||||||
fn many_statements() -> Result<()> {
|
fn many_statements() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
async def f(): # 19
|
async def f():
|
||||||
a = 1
|
a = 1
|
||||||
b = 2
|
b = 2
|
||||||
c = 3
|
c = 3
|
||||||
|
|
@ -277,7 +277,7 @@ async def f(): # 19
|
||||||
#[test]
|
#[test]
|
||||||
fn for_() -> Result<()> {
|
fn for_() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 2
|
def f():
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
pass
|
pass
|
||||||
"#;
|
"#;
|
||||||
|
|
@ -289,7 +289,7 @@ def f(): # 2
|
||||||
#[test]
|
#[test]
|
||||||
fn for_else() -> Result<()> {
|
fn for_else() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 3
|
def f():
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
print()
|
print()
|
||||||
else:
|
else:
|
||||||
|
|
@ -303,7 +303,7 @@ def f(): # 3
|
||||||
#[test]
|
#[test]
|
||||||
fn nested_def() -> Result<()> {
|
fn nested_def() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 5
|
def f():
|
||||||
def g():
|
def g():
|
||||||
print()
|
print()
|
||||||
print()
|
print()
|
||||||
|
|
@ -318,7 +318,7 @@ def f(): # 5
|
||||||
#[test]
|
#[test]
|
||||||
fn nested_class() -> Result<()> {
|
fn nested_class() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 3
|
def f():
|
||||||
class A:
|
class A:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
@ -347,7 +347,7 @@ def f():
|
||||||
#[test]
|
#[test]
|
||||||
fn with() -> Result<()> {
|
fn with() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 6
|
def f():
|
||||||
with a:
|
with a:
|
||||||
if a:
|
if a:
|
||||||
print()
|
print()
|
||||||
|
|
@ -363,7 +363,7 @@ def f(): # 6
|
||||||
#[test]
|
#[test]
|
||||||
fn try_except() -> Result<()> {
|
fn try_except() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 5
|
def f():
|
||||||
try:
|
try:
|
||||||
print()
|
print()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
@ -377,7 +377,7 @@ def f(): # 5
|
||||||
#[test]
|
#[test]
|
||||||
fn try_except_else() -> Result<()> {
|
fn try_except_else() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 7
|
def f():
|
||||||
try:
|
try:
|
||||||
print()
|
print()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
@ -393,7 +393,7 @@ def f(): # 7
|
||||||
#[test]
|
#[test]
|
||||||
fn try_except_else_finally() -> Result<()> {
|
fn try_except_else_finally() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 10
|
def f():
|
||||||
try:
|
try:
|
||||||
print()
|
print()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
@ -411,7 +411,7 @@ def f(): # 10
|
||||||
#[test]
|
#[test]
|
||||||
fn try_except_except() -> Result<()> {
|
fn try_except_except() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 8
|
def f():
|
||||||
try:
|
try:
|
||||||
print()
|
print()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
@ -427,7 +427,7 @@ def f(): # 8
|
||||||
#[test]
|
#[test]
|
||||||
fn try_except_except_finally() -> Result<()> {
|
fn try_except_except_finally() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 11
|
def f():
|
||||||
try:
|
try:
|
||||||
print()
|
print()
|
||||||
except:
|
except:
|
||||||
|
|
@ -445,7 +445,7 @@ def f(): # 11
|
||||||
#[test]
|
#[test]
|
||||||
fn yield_() -> Result<()> {
|
fn yield_() -> Result<()> {
|
||||||
let source: &str = r#"
|
let source: &str = r#"
|
||||||
def f(): # 2
|
def f():
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
yield i
|
yield i
|
||||||
"#;
|
"#;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff/src/rules/pylint/mod.rs
|
source: crates/ruff/src/rules/pylint/mod.rs
|
||||||
---
|
---
|
||||||
too_many_statements.py:5:11: PLR0915 Too many statements (52 > 50)
|
too_many_statements.py:5:11: PLR0915 Too many statements (51 > 50)
|
||||||
|
|
|
|
||||||
5 | async def f(): # Too many statements (52/50)
|
5 | async def f(): # Too many statements (52/50)
|
||||||
| ^ PLR0915
|
| ^ PLR0915
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,7 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff/src/rules/pylint/mod.rs
|
source: crates/ruff/src/rules/pylint/mod.rs
|
||||||
---
|
---
|
||||||
too_many_statements_params.py:2:5: PLR0915 Too many statements (2 > 1)
|
too_many_statements_params.py:6:5: PLR0915 Too many statements (2 > 1)
|
||||||
|
|
|
||||||
2 | # Too may statements (2/1) for max_statements=1
|
|
||||||
3 | def f(x):
|
|
||||||
| ^ PLR0915
|
|
||||||
4 | pass
|
|
||||||
|
|
|
||||||
|
|
||||||
too_many_statements_params.py:6:5: PLR0915 Too many statements (3 > 1)
|
|
||||||
|
|
|
|
||||||
6 | def f(x):
|
6 | def f(x):
|
||||||
| ^ PLR0915
|
| ^ PLR0915
|
||||||
|
|
@ -17,12 +9,4 @@ too_many_statements_params.py:6:5: PLR0915 Too many statements (3 > 1)
|
||||||
8 | pass
|
8 | pass
|
||||||
|
|
|
|
||||||
|
|
||||||
too_many_statements_params.py:7:9: PLR0915 Too many statements (2 > 1)
|
|
||||||
|
|
|
||||||
7 | def f(x):
|
|
||||||
8 | def g(x):
|
|
||||||
| ^ PLR0915
|
|
||||||
9 | pass
|
|
||||||
|
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue