Skip to content

Inconsistency about subclass of typing.Any #8346

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

You must be logged in to vote

Yeah, you're relying on some behaviors here that I wouldn't expect would work.

I think pyright's (new) behavior is justified in this case. Based on the behavior of the is operator, the code here is provably unreachable. If x is constrained to be an instance of int, then x is MyAny will always evaluate to False. There is no possible materialization for the Any base class that would make this evaluate to True, so it's reasonable for a type checker to mark this block as unreachable.

My recommendation is that you avoid using Any as a base class in your sentinels and then properly annotate the parameter to indicate that it is allowed to accept either an int or a sentinel.

from typing import as…

Replies: 1 comment

Comment options

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