Skip to content

Commit

Permalink
v1.1.2
Browse files Browse the repository at this point in the history
fixes #7
  • Loading branch information
James-Ansley committed Aug 11, 2022
1 parent c5411b3 commit 2dd4ef9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

## [1.1.2]

### BugFixes

- `RedundantNot` now does not match chained comparisons
- `WhileAsFor` now ignores cases where test names have possibly mutating
attribute calls in the while body.
[#7](https://github.com/James-Ansley/qchecker/issues/7)

## [1.1.1]

### BugFixes
Expand Down Expand Up @@ -84,6 +93,8 @@ All notable changes to this project will be documented in this file.
too.
- Deprecation warnings have been removed

[1.1.2]: https://github.com/James-Ansley/qchecker/compare/v1.1.1...v1.1.2

[1.1.1]: https://github.com/James-Ansley/qchecker/compare/v1.1.0...v1.1.1

[1.1.0]: https://github.com/James-Ansley/qchecker/compare/v1.0.2...v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ authors:
given-names: James
orcid: https://orcid.org/0000-0002-4279-6284
title: "qChecker"
version: 1.1.0
date-released: 2022-06-23
version: 1.1.2
date-released: 2022-08-11
url: "https://github.com/James-Ansley/qchecker"
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = qchecker
version = 1.1.1
version = 1.1.2
author = James Finnie-Ansley
url = https://github.com/James-Ansley/qchecker
description = A simple library for finding statement-level substructures in Abstract Syntax Trees
Expand Down
29 changes: 18 additions & 11 deletions src/qchecker/substructures/_ast_substructures.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import abc
from ast import *
from collections.abc import Iterator, Iterable
from collections.abc import Iterable, Iterator
from dataclasses import dataclass
from itertools import chain, combinations
from typing import Any

from deprecated.sphinx import deprecated

from qchecker.match import Match, TextRange
from qchecker.parser import CodeModule
from qchecker.substructures._base import Substructure
Expand Down Expand Up @@ -431,7 +432,7 @@ class RedundantNot(ASTSubstructure):
def _iter_matches(cls, module: Module) -> Iterator[Match]:
for node in nodes_of_class(module, UnaryOp):
match node:
case UnaryOp(Not(), Compare()):
case UnaryOp(Not(), Compare(ops=[_], comparators=[_])):
yield cls._make_match(node)


Expand Down Expand Up @@ -582,18 +583,17 @@ def _iter_matches(cls, module: Module) -> Iterator[Match]:
test=Compare() as cmp,
body=body,
):
test_name_ids = {
n.id for n in nodes_of_class(cmp, Name, excluding=Call)
}
ctx_store = {
n.id for n in nodes_of_class(body, Name)
if isinstance(n.ctx, Store)
}
test_name_ids = {n.id for n in nodes_of_class(cmp, Name)}
possibly_updated = (
{n.id for n in nodes_of_class(body, Name)
if isinstance(n.ctx, Store)}
| {n.id for n in names_with_attribute_calls(body)}
)
updated_by_constant = set(names_updated_by_constant(body))
if (
len(test_name_ids & ctx_store) == 1
len(test_name_ids & possibly_updated) == 1
and len(test_name_ids & updated_by_constant) == 1
and len(ctx_store & updated_by_constant) == 1
and len(possibly_updated & updated_by_constant) == 1
):
yield cls._make_match(node)

Expand Down Expand Up @@ -842,3 +842,10 @@ def all_ctx_are_load(nodes: Iterable[Name | Subscript]):

def all_names_of(node: AST | Iterable[AST], *name_ids: str):
yield from (n for n in nodes_of_class(node, Name) if n.id in name_ids)


def names_with_attribute_calls(body: Iterable[AST]):
for node in nodes_of_class(body, Call):
match node:
case Call(func=Attribute(value=Name() as n)):
yield n
12 changes: 11 additions & 1 deletion tests/test_substructures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from textwrap import dedent

import pytest

from qchecker.match import TextRange
from qchecker.parser import CodeModule
from qchecker.substructures import *
Expand Down Expand Up @@ -695,7 +696,10 @@ def test_redundant_arithmetic(line: str, should_match: bool):
('x != y', False),
('x < y', False),
('x and not y', False),
('not x and y', False))
('not x and y', False),
('not x < val <= y', False),
('not x > val <= y', False),
)
)
def test_redundant_not(line, should_match):
match = next(RedundantNot.iter_matches(CodeModule(line)), None)
Expand Down Expand Up @@ -915,6 +919,12 @@ def test_while_as_for():
y = 2
x += 3
print('do something')
# no match
while x < len(y):
y.pop()
x += 1
print('do something')
'''))
match1, match2 = WhileAsFor.iter_matches(code)
assert match1.text_range == TextRange(2, 0, 5, 28)
Expand Down

0 comments on commit 2dd4ef9

Please sign in to comment.