Skip to content

Commit

Permalink
fix: false positive name errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Aug 10, 2024
1 parent 55d44bc commit 8cf472f
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 27 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion src/handle_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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),
}
}
5 changes: 3 additions & 2 deletions tests/list.py
Original file line number Diff line number Diff line change
@@ -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])
6 changes: 6 additions & 0 deletions tests/narrowing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions tests/shadowing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,28 @@
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
4 changes: 0 additions & 4 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 8cf472f

Please sign in to comment.