pylint
pylint copied to clipboard
False positive for optional class attribute
Bug description
"""test_lint.py"""
from typing import Optional
class Foo:
"""class Foo"""
a: Optional[dict] = None
def empty(self):
"""foo"""
@classmethod
def init(cls):
"""init"""
if cls.a is None:
cls.a = {}
assert cls.a is not None
assert isinstance(cls.a, dict)
if cls.a is None:
return
cls.a[1] = 2
_ = cls.a[1]
_ = 1 in cls.a
Configuration
No response
Command used
pylint test_lint.py
Pylint output
************* Module test_lint
test_lint.py:19:8: E1137: 'cls.a' does not support item assignment (unsupported-assignment-operation)
test_lint.py:20:12: E1136: Value 'cls.a' is unsubscriptable (unsubscriptable-object)
test_lint.py:21:19: E1135: Value 'cls.a' 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
All above errors should not be triggered by cls.a
Pylint version
pylint 3.2.5
astroid 3.2.2
Python 3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)]
OS / Environment
MacOS with homebrew
Additional dependencies
pylint 3.2.5
astroid 3.2.2
Python 3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)]