Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ~<type> mean in type checker diagnostic messages?

When writing code I have encountered my linter expressing the type of ~<type> (where <type> is a type not <type> literary). An example of this is self.

class A:
    def foo(self):
        reveal_type(self) # reveals "~A"

I assume this means "A or a subclass of A"; however, I was unable to find any information about it online.

Also, you can't use this syntax in code:

a: ~A # raises an exception: TypeError: bad operand type for unary ~: 'type'

If a would have been revealed it would be Unknown.

like image 409
Minek Po1 Avatar asked Dec 14 '25 10:12

Minek Po1


1 Answers

Looking at the source code, TypeVars are printed (repr) with a leading "+" if they are covariant, "-" if contravariant, and "~" if invariant (neither covariant nor contravariant).

like image 117
AllenT Avatar answered Dec 15 '25 23:12

AllenT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!