pylint
pylint copied to clipboard
E1101 false-positive/-negtive when mutating typess of dictionary values
Bug description
Pylint doesn't handle mutating the type of values in a dictionary correctly.
Minimal reproducible problem:
import re
patterns = {
'number': r'[0-9]+'
}
# change the types of dictionary values
for name, pattern in patterns.items():
patterns[name] = re.compile(pattern)
# 1. false positive
for name, pattern in patterns.items():
print(type(pattern))
match = pattern.match('testing') # E1101: Instance of 'str' has no 'match' member (no-member)
# 2. works, with warning
for name in patterns: # C0206: Consider iterating with .items() (consider-using-dict-items)
print(type(patterns[name]))
match = patterns[name].match('testing')
# 3. false negative
for name in patterns:
print(type(name))
match = name.match('testing')
Output when running python3 example.py
:
<class 're.Pattern'>
<class 're.Pattern'>
<class 'str'>
Traceback (most recent call last):
File "/home/mocqoro/Programming/Python/example.py", line 24, in <module>
match = name.match('testing')
AttributeError: 'str' object has no attribute 'match'
As you can see, pylint throws an error for the first example (even though it works), a warning for the second example, and doesn't throw any sort of error or warning at all for the third example. You can also see that both the first two examples run perfectly fine, and that the third example crashes.
Configuration
No response
Command used
pylint example.py
Pylint output
************* Module example
example.py:14:12: E1101: Instance of 'str' has no 'match' member (no-member)
example.py:17:0: C0206: Consider iterating with .items() (consider-using-dict-items)
Expected behavior
Expected output: pylint should not throw an error for the first example, but should throw an error for the third example.
Pylint version
pylint 2.14.2
astroid 2.11.6
Python 3.10.5 (main, Jun 6 2022, 18:49:26) [GCC 12.1.0]
OS / Environment
Arch Linux, Kitty terminal
Additional dependencies
No response
Thank you for opening the issue. This will require better control flow in astroid. There's a lot of other similar issues under the control flow label.