You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following short code snippet shows that pylint ignores the return type signatures of methods and instead tries to predict the effective runtime type of a variable:
Yes, 'a' is initialized as None, but it is later reassigned with a different value, which is ignored by pylint, making it assume that 'b' must in the end still be None and thus both, unsubscriptable and not supporting membership tests (which is both wrong).
from typing import Optional, Dict
class A:
a: Optional[Dict[str, int]] = None # error
# a: Optional[Dict[str, int]] = {} # works
@staticmethod
def my_method() -> Dict[str, int]:
A.a = {'a': 2}
return A.a
b = A.my_method()
print(b['a'])
print('a' in b)
Configuration
No response
Command used
pylint --disable=C,R main.py
Pylint output
************* Module main
main.py:13:6: E1136: Value 'b' is unsubscriptable (unsubscriptable-object)
main.py:14:13: E1135: Value 'b' doesn't support membership test (unsupported-membership-test)------------------------------------------------------------------Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)
Expected behavior
Python is a dynamic language which makes it particularly challenging to predict the potential runtime type of a variable at any given time - I totally understand that. But then again, even with type hints clearly stating that the return value of my_func will ALWAYS be a dict, pylint still assumes it must be None. I would say, pylint must either rely on given type hints and or apply a sophisticated and reliable method of determining whether a variable can really be something that is REALLY non subscriptable and that does not support membership tests.
Please also consider the following sample:
class A:
a = None
@staticmethod
def do_some_pretty_predictable_shit():
setattr(A, random.choice(['a']), {'a': 2})
@staticmethod
def my_method() -> Dict[str, int]:
A.do_some_pretty_predictable_shit()
return A.a
b = A.my_method()
print(b['a'])
print('a' in b)
Again, b will ALWAYS be subscriptable and support membership tests. Are you ready for this kind of stuff?
Now your static code analyzer needs to understand what random choice does. What if a binary third
party library is thrown into the mix?
Pylint version
pylint 2.15.9
OS / Environment
Windows 10
Additional dependencies
No response
The text was updated successfully, but these errors were encountered:
Bug description
The following short code snippet shows that pylint ignores the return type signatures of methods and instead tries to predict the effective runtime type of a variable:
Yes, 'a' is initialized as None, but it is later reassigned with a different value, which is ignored by pylint, making it assume that 'b' must in the end still be None and thus both, unsubscriptable and not supporting membership tests (which is both wrong).
from typing import Optional, Dict
Configuration
No response
Command used
Pylint output
Expected behavior
Python is a dynamic language which makes it particularly challenging to predict the potential runtime type of a variable at any given time - I totally understand that. But then again, even with type hints clearly stating that the return value of my_func will ALWAYS be a dict, pylint still assumes it must be None. I would say, pylint must either rely on given type hints and or apply a sophisticated and reliable method of determining whether a variable can really be something that is REALLY non subscriptable and that does not support membership tests.
Please also consider the following sample:
Again, b will ALWAYS be subscriptable and support membership tests. Are you ready for this kind of stuff?
Now your static code analyzer needs to understand what random choice does. What if a binary third
party library is thrown into the mix?
Pylint version
OS / Environment
Windows 10
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: