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

False positive E1133 (not-an-iterable) when returning (type hinted) class variable #10298

Open
Flamefire opened this issue Mar 21, 2025 · 0 comments
Labels
Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@Flamefire
Copy link

Flamefire commented Mar 21, 2025

Bug description

When returning a class variable from an instance the type is not correctly detected and E1133 is raised although a list will be returned.

To show I use 2 classes where one returns an instance variable and the other a class variable. The annotation is only shown for the class variable:

# pylint: disable=missing-module-docstring, missing-function-docstring, missing-class-docstring, invalid-name, attribute-defined-outside-init

class MyClass:
    @classmethod
    def setUpClass(cls):
        MyClass.x = None

    @classmethod
    def fill(cls):
        MyClass.x = [1]

    def value(self) -> list[int]:
        if MyClass.x is None:
            MyClass.fill()
        return MyClass.x


class MyClass2:
    def fill(self):
        self.x = [1]

    def value(self) -> list[int]:
        if self.x is None:
            self.fill()
        return self.x


for i in MyClass().value():  # Warning here
    print(i)

for i in MyClass2().value():  # No warning here
    print(i)

This might be related to #6044 which also uses a class variable and/or #2766 which tries to solve a similar issue with type hints.

Command used

pylint --enable=E1133 /tmp/a.py

Pylint output

************* Module a
/tmp/a.py:28:9: E1133: Non-iterable value MyClass().value() is used in an iterating context (not-an-iterable)

Expected behavior

No warning show for either case and/or at least being able to silence it using standard Python type hints as shown.

Pylint version

pylint 3.3.6
astroid 3.3.8
Python 3.12.1 (main, Dec 20 2023, 10:28:56) [GCC 11.4.0]

OS / Environment

No response

@Flamefire Flamefire added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Mar 21, 2025
@Pierre-Sassoulas Pierre-Sassoulas added Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

2 participants