
Could you point us to any other language (with a somewhat similar type system to PEP 483) that has a NOT type? I'm aware of languages with Intersection types but I haven't heard of languages with NOT. Also, the only realistic use case that I have ever heard people ask for is "NOT str", as in "any sequence except str". I'd like to clarify that most of your "use cases" aren't use cases -- they are just examples. A use case is more than a code snippet, it should be a little story based in real code experience that provides an actual problem people run into regularly and for which the proposal would be a better solution than the available workarounds. On Thu, Dec 23, 2021 at 1:09 PM James H-B <gobot1234yt@gmail.com> wrote:
Thank you for the feedback, Eric. This is something I think is important to get right.
These are my initial thoughts on the matter:
* What are the subtyping rules for types and NotTypes? Any subtype of the type T to a NotType should be considered an excluded type.
e.g. ```py def fn(x: ~Sequence): … list = [1, 2, 3, 4] tuple = (1, 2, 3, 4) collection = {1, 2, 3, 4}
fn(list) # should not pass as list is a subtype of Sequence fn(tuple) # should not pass as tuple is a subtype of Sequence fn(collection) # should pass as set is not a subtype of Sequence its a subtype of Collection which is a supertype of Sequence ```
* In what ways are Any and NotType equivalent, and in what ways are they not? I’m in agreement here, it is very similar to Any in the fact that every type, just not the type T.
* How does a NotType interact with generic classes? For example, what does `~list[T]` mean if `T` is used elsewhere in the same function signature? Not an instance of that class so in this case anything that isn’t a list[T] so tuple[T, …] is fine as is int.
* How does a NotType interact with TypeVars? For example, what does ~T mean?
Anything that isn’t T
side note: currently ~T is the repr for an invariant TypeVar which is something that would probably need to be changed Related issue: https://github.com/python/typing/issues/599
* How does type narrowing work for NotType? I’m thinking you need to use __eq__/__ne__, however is none trivial for non-literals as understanding how __eq__ behaves is very difficult. Otherwise, I’d just hope you can fall back to using an isinstance check
* How does a union of a Type and a NotType work? A picture demonstrates this well where A is the Type and B is the type that’s being not’ed https://imgur.com/a/5kdE8xl
* Can a TypeVar be bound to a NotType? What does that mean? Yes, the TypeVar can be used in place of anything that’s not the type T.
* Can a NotType be used as a type argument in a generic class? If so, how does it work with existing variance rules? Yes, see my first answer
I’d suggest yes and it should follow the same variance rules as Unions. * How should the TypeVar constraint solver handle the case where a TypeVar is being matched to both a type and a NotType?
* Can a NotType be used in an isinstance call? What does that mean? I’m inclined to say yes, it should just expand to not isinstance(thing, T), I’d not be entirely sure as I think it would be dependent the team working on intersection decides to do.
* How does NotType work with runtime type checking? Introspection wise this would be very similar to UnionType so it would have __args__, __parameters__, support substitution and probably support instance checks.
Other points:
I can see the concern here with this type being similar to Any and it being bad for static type checking ideally, the user should cover both the not type and the type cases themselves and I'd perhaps consider adding a warning about what would be in essence un-typed code.
With regards to examples using intersection I think it's a feature that’s undeniably important to the type system and I agree a lot of this hinges on that being accepted as a PEP.
Another concern I would have is also about ParamSpec. Would it be any function signature that doesn’t match the currently bound one? I’m not sure. Would that ever even be useful? Again I’m not sure. _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>