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

"object" has no attribute "__iter__" false-positive #8706

Closed
tucked opened this issue Apr 21, 2020 · 3 comments
Closed

"object" has no attribute "__iter__" false-positive #8706

tucked opened this issue Apr 21, 2020 · 3 comments

Comments

@tucked
Copy link

tucked commented Apr 21, 2020

# sscce.py


def no_repro(bar: str = '4'):
    key_values = {
        'key': [1, 2, 3],
        'key2': [1, 2],
    }
    return all(
        value
        for key, values in key_values.items()
        for value in values
    )


def repro(bar: str = '4'):
    key_values = {
        'key': [1, 2, 3, bar],  # <--- bar is included
        'key2': [1, 2],
    }
    return all(
        value
        for key, values in key_values.items()
        for value in values
    )
  • Note: no repro without 'key2': [1, 2],
mypy sscce.py
sscce.py:21: error: "object" has no attribute "__iter__"; maybe "__str__" or "__dir__"? (not iterable)
Found 1 error in 1 file (checked 1 source file)
  • Python 3.6.8
  • mypy 0.770 (mypy-0.770+dev.833fc2caec6773683d691a72058293ae87139210 too)

Potentially related: #4975

@msullivan
Copy link
Collaborator

This is frustrating but I don't think it's a bug.

We infer the type List[object] for the first list and List[int] for the second list. List[int] is not a subtype of List[object], so the best common subtype for them is unfortunately object.

@tucked
Copy link
Author

tucked commented Apr 29, 2020

Is the correct fix to use tuples instead?

@JukkaL
Copy link
Collaborator

JukkaL commented May 1, 2020

The recommended fix is to add an annotation for key_values, but using tuples may also work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants