Skip to content

Commit 324e598

Browse files
committed
[FIX] OLS01009 triggered on valid multiple signature + tests
1 parent 99a477b commit 324e598

File tree

5 files changed

+96
-20
lines changed

5 files changed

+96
-20
lines changed

server/src/core/evaluation.rs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use itertools::Itertools;
2+
use itertools::FoldWhile::{Continue, Done};
13
use ruff_python_ast::{Arguments, Expr, ExprCall, Identifier, Number, Operator, Parameter, UnaryOp};
24
use ruff_text_size::{Ranged, TextRange, TextSize};
35
use lsp_types::{Diagnostic, Position, Range};
@@ -784,6 +786,7 @@ impl Evaluation {
784786

785787
let mut call_argument_diagnostics = Vec::new();
786788
for base_eval_ptr in base_eval_ptrs.iter() {
789+
call_argument_diagnostics.push(Vec::new()); //one list per evaluation
787790
let EvaluationSymbolPtr::WEAK(base_sym_weak_eval) = base_eval_ptr else {continue};
788791
let Some(base_sym) = base_sym_weak_eval.weak.upgrade() else {continue};
789792
if base_sym.borrow().typ() == SymType::CLASS {
@@ -994,7 +997,7 @@ impl Evaluation {
994997
};
995998
if is_in_validation {
996999
let on_instance = base_sym_weak_eval.context.get(&S!("is_attr_of_instance")).map(|v| v.as_bool());
997-
call_argument_diagnostics.extend(Evaluation::validate_call_arguments(session,
1000+
call_argument_diagnostics.last_mut().unwrap().extend(Evaluation::validate_call_arguments(session,
9981001
&base_sym.borrow().as_func(),
9991002
expr,
10001003
call_parent.clone(),
@@ -1458,33 +1461,52 @@ impl Evaluation {
14581461
diagnostics
14591462
}
14601463

1461-
fn process_argument_diagnostics(session: &SessionInfo, expr_call: &ExprCall, diagnostics: Vec<Diagnostic>, eval_count: usize) -> Vec<Diagnostic> {
1464+
fn process_argument_diagnostics(session: &SessionInfo, expr_call: &ExprCall, diagnostics: Vec<Vec<Diagnostic>>, eval_count: usize) -> Vec<Diagnostic> {
14621465
let mut filtered_diagnostics = vec![];
1463-
let mut arg_diagnostic = diagnostics.iter().filter(|d|
1464-
d.code == Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01007.to_string())) ||
1465-
d.code == Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01008.to_string()))
1466-
);
1467-
let arg_diagnostic_count = arg_diagnostic.clone().count();
1468-
if arg_diagnostic_count > 0 {
1469-
if arg_diagnostic_count == eval_count {
1470-
// if all evaluations have some argument error, we keep the first one
1471-
filtered_diagnostics.push(arg_diagnostic.next().unwrap().clone());
1472-
} else {
1473-
// if not all evaluations have it, it means at least one is valid.
1474-
// We use a different code warning
1466+
//iter through diagnostics and check that each evaluation has the same amount of diagnostics with code OLS01007 or OLS01008 or OLS01010
1467+
let all_same_issues = diagnostics.iter().fold_while(None, |acc, diags| {
1468+
let new_count = diags.iter().filter(|d|
1469+
d.code == Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01007.to_string())) ||
1470+
d.code == Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01008.to_string())) ||
1471+
d.code == Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01010.to_string()))
1472+
).count() as i32;
1473+
match acc {
1474+
None => Continue(Some(new_count)),
1475+
Some(count) => {
1476+
if count == new_count {
1477+
Continue(Some(count))
1478+
} else {
1479+
Done(Some(-1))
1480+
}
1481+
}
1482+
}
1483+
}).into_inner();
1484+
match all_same_issues {
1485+
Some(-1) => {
14751486
if let Some(diagnostic) = create_diagnostic(session, DiagnosticCode::OLS01009, &[]) {
14761487
filtered_diagnostics.push(Diagnostic {
14771488
range: Range::new(Position::new(expr_call.range().start().to_u32(), 0), Position::new(expr_call.range().end().to_u32(), 0)),
14781489
..diagnostic
14791490
});
14801491
}
1481-
}
1492+
},
1493+
Some(_count) => {
1494+
filtered_diagnostics.extend(diagnostics[0].iter().filter(|d|
1495+
d.code == Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01007.to_string())) ||
1496+
d.code == Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01008.to_string())) ||
1497+
d.code == Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01010.to_string()))
1498+
).cloned().collect::<Vec<Diagnostic>>());
1499+
},
1500+
None => {}
1501+
}
1502+
// // we add the rest of the diagnostics as is
1503+
for eval_diag in diagnostics {
1504+
filtered_diagnostics.extend(eval_diag.into_iter().filter(|d| {
1505+
d.code != Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01007.to_string())) &&
1506+
d.code != Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01008.to_string())) &&
1507+
d.code != Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01010.to_string()))
1508+
}));
14821509
}
1483-
// we add the rest of the diagnostics as is
1484-
filtered_diagnostics.extend(diagnostics.into_iter().filter(|d| {
1485-
d.code != Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01007.to_string())) &&
1486-
d.code != Some(lsp_types::NumberOrString::String(DiagnosticCode::OLS01008.to_string()))
1487-
}));
14881510
filtered_diagnostics
14891511
}
14901512

server/tests/data/python/diagnostics/ols01008.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def d(a, *args):
2525

2626
d(5)
2727
d(5, d=3) # OLS01008
28+
d(5, d=3, e=5) #OLS01008
2829

2930
def e(a, **kwargs):
3031
pass
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def b(*args):
2+
pass
3+
4+
b(6, d=5, e=5) #OLS01008
5+
6+
def a():
7+
pass
8+
9+
a()
10+
11+
def b(*args):
12+
pass
13+
14+
b(5)
15+
b(5, d=5) #OLS01008
16+
b(6, d=5, e=5) #OLS01008
17+
18+
if intput():
19+
def c(x, y):
20+
pass
21+
else:
22+
def c(x):
23+
pass
24+
25+
c(5) #OLS01009
26+
27+
28+
if intput():
29+
def d(x, y):
30+
pass
31+
else:
32+
def d(x, *args):
33+
pass
34+
35+
d(5) #OLS01009
36+
d(x=1, y=2) #OLS01009
37+
d(1, 2)

server/tests/diagnostics/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ pub mod ols01005;
88
pub mod ols01006;
99
pub mod ols01007;
1010
pub mod ols01008;
11+
pub mod ols01009;
1112
pub mod ols0500_00_12;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::env;
2+
3+
use odoo_ls_server::utils::PathSanitizer;
4+
5+
use crate::{setup::setup::*, test_utils::{verify_diagnostics_against_doc}};
6+
7+
#[test]
8+
fn test_ols01009() {
9+
let mut odoo = setup_server(false);
10+
let path = env::current_dir().unwrap().join("tests/data/python/diagnostics/ols01009.py").sanitize();
11+
let mut session = prepare_custom_entry_point(&mut odoo, &path);
12+
let diagnostics = get_diagnostics_for_path(&mut session, &path);
13+
let doc_diags = get_diagnostics_test_comments(&mut session, &path);
14+
verify_diagnostics_against_doc(diagnostics, doc_diags);
15+
}

0 commit comments

Comments
 (0)