Skip to content

Variable types not enforced/autosensed properly #9771

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

You must be logged in to vote

Static type checkers like pyright apply a technique called "type narrowing". You can read more about this here.

In your example above, the type of the expression net cannot be narrowed by the isinstance check; prior to the check, its type is IPv4Network | IPv6Network and after the check, its type remains the same. The type of ipnet_item is also a union and cannot be narrowed further by the in operator.

It is theoretically possible to narrow types conditionally — for example, "the type of searched_wl is IPv4Network if the type of net is also IPv4Network but it IPv6Network otherwise. However, tracking such conditional types is not computationally feasible in practice. I don't know of any st…

Replies: 2 comments

Comment options

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