Skip to content

issubclass vs. is for narrowing types #1874

Answered by Daverball
stephenfin asked this question in Q&A
Discussion options

You must be logged in to vote

data_type could always be a subclass of list or int as well, using a constrained type var does not prevent that. Variance doesn't really have any meaning in generic functions, it just has to find a T that produces a signature that could accept your input type. So if you substitute int any subclass of int can be passed into the function, just like if you had annotated the function with int.

So is is not sufficient for narrowing classes that haven't been decorated with @final to prevent subclasses. Type checkers could narrow to int & ~Exactly[int] in the negative case but in practice that's not really useful, unless you're working with ADTs where you have a finite statically known number of…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@stephenfin
Comment options

@JelleZijlstra
Comment options

Answer selected by stephenfin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants