Pyright complains incorrectly about object not being iterable. #9134
Answered
by
erictraut
fkhawajagh
asked this question in
Q&A
-
So I came across this 'not a bug' ticket. #7916. I seem to be having a similar issue even when I'm using guards (both positive and negative) If I add a default value for the Any thoughts on what I'm doing wrong here?
|
Beta Was this translation helpful? Give feedback.
Answered by
erictraut
Oct 2, 2024
Replies: 1 comment 4 replies
-
I'm happy to help you diagnose the problem, but could you please post a minimal repro in text form so I don't need to retype your code from the screen shots? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 tox.get("data", None")
will never returnNone
. 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: