Skip to content

Refactoring checker crashes when unary operator used with variable #9074

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Hnasar opened this issue Sep 26, 2023 · 0 comments · Fixed by #9075 or #9093
Closed

Refactoring checker crashes when unary operator used with variable #9074

Hnasar opened this issue Sep 26, 2023 · 0 comments · Fixed by #9075 or #9093
Labels
Crash 💥 A bug that makes pylint crash
Milestone

Comments

@Hnasar
Copy link
Contributor

Hnasar commented Sep 26, 2023

Bug description

"""Regression test."""
def crash_on_unary_op_with_name():
    """Should not crash with -idx."""
    mylist = []
    idx = 5
    for _i, _val in enumerate(mylist, start=-idx):
        pass

Configuration

No response

Command used

pylint file.py

Pylint output

File "python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk
        callback(astroid)
      File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for
        self._check_unnecessary_list_index_lookup(node)
      File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2227, in _check_unnecessary_list_index_lookup
        has_start_arg, confidence = self._enumerate_with_start(node)
      File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2352, in _enumerate_with_start
        start_val, confidence = self._get_start_value(keyword.value)
      File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2369, in _get_start_value
        return node.operand.value, HIGH
    AttributeError: 'Name' object has no attribute 'value'

Expected behavior

no crash

Pylint version

python3.10 -- latest version of pylint maintenance branch

OS / Environment

No response

Additional dependencies

No response

@Hnasar Hnasar added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Sep 26, 2023
Hnasar added a commit to Hnasar/pylint that referenced this issue Sep 26, 2023
Fixes:
```
  File "python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for
    self._check_unnecessary_list_index_lookup(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2227, in _check_unnecessary_list_index_lookup
    has_start_arg, confidence = self._enumerate_with_start(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2352, in _enumerate_with_start
    start_val, confidence = self._get_start_value(keyword.value)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2369, in _get_start_value
    return node.operand.value, HIGH
AttributeError: 'Name' object has no attribute 'value'
```

Crash is reproducible if you have something like this:

```python
x=5
for _ in enumerate(range, start=-x):
    ...
```

As a workaround, remove the unary op before `for` loop (i.e. change the
variable used).

Closes pylint-dev#9074
Hnasar added a commit to Hnasar/pylint that referenced this issue Sep 26, 2023
Fixes:
```
  File "python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for
    self._check_unnecessary_list_index_lookup(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2227, in _check_unnecessary_list_index_lookup
    has_start_arg, confidence = self._enumerate_with_start(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2352, in _enumerate_with_start
    start_val, confidence = self._get_start_value(keyword.value)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2369, in _get_start_value
    return node.operand.value, HIGH
AttributeError: 'Name' object has no attribute 'value'
```

Crash is reproducible if you have something like this:

```python
x=5
for _ in enumerate(range, start=-x):
    ...
```

As a workaround, remove the unary op before `for` loop (i.e. change the
variable used).

Closes pylint-dev#9074
Pierre-Sassoulas pushed a commit that referenced this issue Sep 27, 2023
Fixes:
```
  File "python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for
    self._check_unnecessary_list_index_lookup(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2227, in _check_unnecessary_list_index_lookup
    has_start_arg, confidence = self._enumerate_with_start(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2352, in _enumerate_with_start
    start_val, confidence = self._get_start_value(keyword.value)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2369, in _get_start_value
    return node.operand.value, HIGH
AttributeError: 'Name' object has no attribute 'value'
```

Crash is reproducible if you have something like this:

```python
x=5
for _ in enumerate(range, start=-x):
    ...
```

As a workaround, remove the unary op before `for` loop (i.e. change the
variable used).

Closes #9074

Co-authored-by: Hashem Nasarat <hashem@hudson-trading.com>
github-actions bot pushed a commit that referenced this issue Sep 27, 2023
Fixes:
```
  File "python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for
    self._check_unnecessary_list_index_lookup(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2227, in _check_unnecessary_list_index_lookup
    has_start_arg, confidence = self._enumerate_with_start(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2352, in _enumerate_with_start
    start_val, confidence = self._get_start_value(keyword.value)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2369, in _get_start_value
    return node.operand.value, HIGH
AttributeError: 'Name' object has no attribute 'value'
```

Crash is reproducible if you have something like this:

```python
x=5
for _ in enumerate(range, start=-x):
    ...
```

As a workaround, remove the unary op before `for` loop (i.e. change the
variable used).

Closes #9074

Co-authored-by: Hashem Nasarat <hashem@hudson-trading.com>
(cherry picked from commit aa29201)
@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 27, 2023
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.17.7 milestone Sep 27, 2023
Pierre-Sassoulas pushed a commit that referenced this issue Sep 27, 2023
…9076)

Fixes:
```
  File "python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for
    self._check_unnecessary_list_index_lookup(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2227, in _check_unnecessary_list_index_lookup
    has_start_arg, confidence = self._enumerate_with_start(node)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2352, in _enumerate_with_start
    start_val, confidence = self._get_start_value(keyword.value)
  File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2369, in _get_start_value
    return node.operand.value, HIGH
AttributeError: 'Name' object has no attribute 'value'
```

Crash is reproducible if you have something like this:

```python
x=5
for _ in enumerate(range, start=-x):
    ...
```

As a workaround, remove the unary op before `for` loop (i.e. change the
variable used).

Closes #9074

Co-authored-by: Hashem Nasarat <hashem@hudson-trading.com>
(cherry picked from commit aa29201)

Co-authored-by: Nazia Povey <Hnasar@users.noreply.github.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Crash 💥 A bug that makes pylint crash
Projects
None yet
2 participants