Skip to content

Pyright complains incorrectly about object not being iterable. #9134

Answered by erictraut
fkhawajagh asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for the code.

You didn't state this, but I presume that you're using strict type checking mode or have manually enabled the strictDictionaryInference configuration option. Without this, I'm not able to repro the issue.

The problem in this code is that you're assuming that a call to x.get("data") implies that a subsequent call to x.get("data", None") will never return None. A type checker cannot make such an assumption. It must assume that each call is independent of other calls, since functions in Python are not guaranteed to return the same value even if the same arguments are passed to a function multiple times.

Here's how to make this code type safe:

    data = x.get("data")
    if

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@fkhawajagh
Comment options

@erictraut
Comment options

Answer selected by fkhawajagh
@fkhawajagh
Comment options

@erictraut
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants