Skip to content

Narrowing tuple length when using lambda functions #8340

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

You must be logged in to vote

This behavior is intended, so it's not a bug.

When you define a lambda function, it captures the associated variable value. This variable can subsequently be modified in the outer context. For example, it could be reassigned a new value. By the time the lambda is executed, the value of value might be different from the value you checked with your assert. In general, it's unsafe for a type checker to assume that narrowed types will be retained for captured variables. There are certain cases where pyright can prove to itself that this is safe, but your code sample isn't one of them.

For more details, refer to the pyright documentation.

A typical way to make this code type safe — and allow a…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by gaufde
Comment options

You must be logged in to vote
0 replies
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