Let T be a supertype of U. As far as I know, there is no way of type hinting that you allow any subtype of T *except* for U. We already have a special type for grouping multiple allowed types together - Union. How about, then, adding something akin to Difference and Complement? Difference[T, U] would then signify the constraint above, and with T being Any you could write it as Complement[U].

That would make type checking at least co-NP-hard; not sure if real-life use cases could actually become expensive.

typing issues is where topics like this tend to be discussed. There’s long discussion on it here. Short summary interesting feature, likely pretty complex to support and work out. Intersection is probably better starting point but also complex.

Difference[T, U] is Intersection[T, Complement[U]]

My view, I think Intersection is simpler then Complement and more useful one as it allows extending type with more attributes/methods (dynamic patching). Both do have use cases. I’d guess path would be first do Intersection (maybe constrained in some way like require all but one argument to be protocol) and evolve from there.

What’s your use case? The only one I’ve seen so far is monkey-patching string annotations with `str - Sequence[str]`

.

Intersection is very significantly more useful.

Currently, Python does not have a built-in type hint specifically designed for expressing a constraint that allows any subtype of a supertype T except for a specific subtype U. However, you can achieve a similar effect by using type aliases and the Union type hint. By defining a type alias that represents the Union of T and U, and then using that alias as the supertype in your type hint, you can communicate the constraint effectively. Thanks

A sensible design does not require such hints, because they are tantamount to an assertion that U violates the Liskov Substitution Principle.