Skip to content
Discussion options

You must be logged in to vote

Yes, this is intended behavior.

Pyright retains narrowing inside a closure only for captured variables (simple names), and only when it can prove the variable isn't reassigned on any code path after the closure is defined. This is documented under Narrowing for Captured Variables.

The reason for the difference you're seeing:

  • container.bar.name inside the lambdacontainer.bar is a member access expression, not a simple captured variable. Narrowing of member expressions is never retained inside closures. A closure can be invoked at any later time, and between the isinstance check and the actual call, anything holding a reference to container could reassign container.bar to a NotNamedClass

Replies: 2 comments

Comment options

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