fix: false positive name errors

This commit is contained in:
Shunsuke Shibayama 2024-08-10 20:18:09 +09:00
parent 55d44bc5a4
commit 8cf472f469
9 changed files with 62 additions and 27 deletions

20
Cargo.lock generated
View File

@ -136,9 +136,9 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "els"
version = "0.1.53"
version = "0.1.54-nightly.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97104e067d1a4432a49a18da26af10a0f51d8d99f69e46aeb9890440f6a36457"
checksum = "4fb27dc0754e2490b8984d95f4fe6368258de544eedd37798a314acfeb5ccfc6"
dependencies = [
"erg_common",
"erg_compiler",
@ -150,9 +150,9 @@ dependencies = [
[[package]]
name = "erg_common"
version = "0.6.41"
version = "0.6.42-nightly.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3868e184db0bb4a69538aa0744f6b19e51c43233f5369f8e7e114c263b9ff996"
checksum = "415899164e7873f66eecad5ad37683c5fa31c038802033f35ad08f3fa38882dc"
dependencies = [
"backtrace-on-stack-overflow",
"erg_proc_macros",
@ -162,9 +162,9 @@ dependencies = [
[[package]]
name = "erg_compiler"
version = "0.6.41"
version = "0.6.42-nightly.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fcd5809b7797e85a87555cbe5d79580749a8b9ba8edf0ac7a422ad90c67793"
checksum = "048b3d74d5ca2c5279e18f15fd5997fe0cdfcad0a8d6ff2e4e238a8b76d86b36"
dependencies = [
"erg_common",
"erg_parser",
@ -172,9 +172,9 @@ dependencies = [
[[package]]
name = "erg_parser"
version = "0.6.41"
version = "0.6.42-nightly.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f986e2ca5c640c39083fcfe97009f11138458b1f1e3aa17595c81e9ae9e33d0"
checksum = "ca774922a3555cb1973ead24f9ad27056057480bbcfff2ccab0b8b3637517054"
dependencies = [
"erg_common",
"erg_proc_macros",
@ -183,9 +183,9 @@ dependencies = [
[[package]]
name = "erg_proc_macros"
version = "0.6.41"
version = "0.6.42-nightly.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e828d1dcb5ee351dcba9707a8ecd46e9ba510367595e922a26528fa0d2853920"
checksum = "a7500b1a02978ccf36cef9eecb31f99911a0b56753589e375f0a51b6ff57b750"
dependencies = [
"quote",
"syn 1.0.109",

View File

@ -22,9 +22,9 @@ edition = "2021"
repository = "https://github.com/mtshiba/pylyzer"
[workspace.dependencies]
erg_common = { version = "0.6.41", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.41", features = ["py_compat", "els"] }
els = { version = "0.1.53", features = ["py_compat"] }
erg_common = { version = "0.6.42-nightly.0", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.42-nightly.0", features = ["py_compat", "els"] }
els = { version = "0.1.54-nightly.0", features = ["py_compat"] }
# rustpython-parser = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
# rustpython-ast = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
rustpython-parser = { git = "https://github.com/RustPython/Parser", version = "0.4.0", features = ["all-nodes-with-ranges", "location"] }

View File

@ -41,12 +41,6 @@ On average, pylyzer can inspect Python scripts more than __100 times faster__ th
![performance](https://raw.githubusercontent.com/mtshiba/pylyzer/main/images/performance.png)
* Detailed analysis 🩺
pylyzer can do more than the type checking. For example, it can detect out-of-bounds accesses to lists and accesses to nonexistent keys in dicts.
![analysis](https://raw.githubusercontent.com/mtshiba/pylyzer/main/images/analysis.png)
* Reports readability 📖
While pytype/pyright's error reports are illegible, pylyzer shows where the error occurred and provides clear error messages.

View File

@ -12,6 +12,19 @@ pub(crate) fn filter_errors(ctx: &ModuleContext, errors: CompileErrors) -> Compi
.collect()
}
fn handle_name_error(error: CompileError) -> Option<CompileError> {
if error.core.main_message.contains("is already declared")
|| error
.core
.main_message
.contains("cannot be assigned more than once")
{
None
} else {
Some(error)
}
}
fn filter_error(_ctx: &ModuleContext, mut error: CompileError) -> Option<CompileError> {
match error.core.kind {
ErrorKind::FeatureError => {
@ -39,7 +52,7 @@ fn filter_error(_ctx: &ModuleContext, mut error: CompileError) -> Option<Compile
Some(error)
}
}
// ErrorKind::AssignError => handle_assign_error(error),
ErrorKind::NameError | ErrorKind::AssignError => handle_name_error(error),
_ => Some(error),
}
}

View File

@ -1,10 +1,11 @@
l = [1, 2, 3]
_ = l[1:2]
print(l[4]) # ERR
print(l[2])
print(l["a"]) # ERR
# OK
for i in range(3):
print(l[i])
# ERR
for i in range(4):
for i in "abcd":
print(l[i])

View File

@ -7,3 +7,9 @@ def f(x: int | None):
return None
f(1)
from typing import Optional
x: Optional[int] = None
if x is not None:
x += 1

View File

@ -12,3 +12,28 @@ while False:
def f(x: int):
i = 1 # OK
return x + i
if True:
pass
elif True:
for i in []: pass
pass
elif True:
for i in []: pass
pass
if True:
pass
elif True:
with open("") as x:
pass
pass
elif True:
with open("") as x:
pass
pass
if True:
left, right = 1, 2
if True:
left, _ = 1, 2

View File

@ -36,10 +36,6 @@ def f(d1, d2: dict[str, int]):
_ = d2[1] # ERR
dic = {"a": 1}
_ = dic["b"] # ERR
arr = [1, 2, 3]
_ = arr[4] # ERR
for i in range(4):
print(arr[i]) # ERR
i, j = 1, 2
assert i == 1

View File

@ -55,7 +55,7 @@ pub fn expect(file_path: &'static str, warns: usize, errors: usize) -> Result<()
#[test]
fn exec_test() -> Result<(), String> {
expect("tests/test.py", 0, 13)
expect("tests/test.py", 0, 11)
}
#[test]