Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix crash in refactoring checker from unaryop with variable #9075

Conversation

Hnasar
Copy link
Contributor

@Hnasar Hnasar commented 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:

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

Type of Changes

Type
🐛 Bug fix

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 Hnasar force-pushed the fix-enumerate-refactoring-negative-var-crash branch from 8e14344 to ab3e0f1 Compare September 26, 2023 21:51
@codecov
Copy link

codecov bot commented Sep 26, 2023

Codecov Report

Merging #9075 (ab3e0f1) into main (b8a7cc5) will increase coverage by 0.18%.
Report is 440 commits behind head on main.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #9075      +/-   ##
==========================================
+ Coverage   95.57%   95.75%   +0.18%     
==========================================
  Files         178      173       -5     
  Lines       18869    18664     -205     
==========================================
- Hits        18034    17872     -162     
+ Misses        835      792      -43     
Files Coverage Δ
pylint/checkers/refactoring/refactoring_checker.py 98.23% <100.00%> (-0.12%) ⬇️

... and 101 files with indirect coverage changes

@github-actions
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit ab3e0f1

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch and fix !

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.17.7 milestone Sep 27, 2023
@Pierre-Sassoulas Pierre-Sassoulas merged commit aa29201 into pylint-dev:main Sep 27, 2023
44 checks passed
github-actions bot pushed a commit that referenced this pull request 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 pushed a commit that referenced this pull request 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>
@Pierre-Sassoulas
Copy link
Member

Are Hashem Nasarat and Nazia Povey the same person or should they be credited separately ?

@Hnasar
Copy link
Contributor Author

Hnasar commented Sep 27, 2023

Hashem is fine, thanks!

@Pierre-Sassoulas
Copy link
Member

I'm trying to populate the alias file here: https://github.com/pylint-dev/pylint/blob/main/script/.contributors_aliases.json#L55

Would this work for you ?

  "Hnasar@users.noreply.github.com": {
    "mails": ["Hnasar@users.noreply.github.com", "hashem@hudson-trading.com"],
    "name": "Hashem Nasarat"
  },

(Due to the Co-authored-by: Hashem Nasarat <hashem@hudson-trading.com> in this MR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported Crash 💥 A bug that makes pylint crash
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactoring checker crashes when unary operator used with variable
2 participants