@@ -579,18 +579,15 @@ proposing to add that as a notion, but we could.
579579Boolean operators
580580'''''''''''''''''
581581
582- * ``IsSub [T, S] ``: Returns a boolean literal type indicating whether
583- ``T `` is a subtype of `` S ``.
582+ * ``IsAssignable [T, S] ``: Returns a boolean literal type indicating whether
583+ ``S `` is assignable to `` T ``.
584584
585- (Actually, I suppose, whether ``S `` is "assignable to" ``T ``. We can
586- certainly bikeshed on this name.)
587-
588- * ``Matches[T, S] ``:
589- Equivalent to ``IsSub[T, S] and IsSub[S, T] ``.
585+ * ``IsEquivalent[T, S] ``:
586+ Equivalent to ``IsAssignable[T, S] and IsAssignable[S, T] ``.
590587
591588* ``Bool[T] ``: Returns ``Literal[True] `` if ``T `` is also
592589 ``Literal[True] `` or a union containing it.
593- Equivalent to ``IsSub [T, Literal[True]] and not IsSub [T, Never] ``.
590+ Equivalent to ``IsAssignable [T, Literal[True]] and not IsAssignable [T, Never] ``.
594591
595592 This is useful for invoking "helper aliases" that return a boolean
596593 literal type.
@@ -1021,7 +1018,7 @@ producing a new target type containing only properties and wrapping
10211018
10221019 type ConvertField[T] = (
10231020 AdjustLink[PropsOnly[PointerArg[T]], T]
1024- if typing.IsSub [T, Link]
1021+ if typing.IsAssignable [T, Link]
10251022 else PointerArg[T]
10261023 )
10271024
@@ -1044,7 +1041,7 @@ we've discussed already.
10441041::
10451042
10461043 type AdjustLink[Tgt, LinkTy] = (
1047- list[Tgt] if typing.IsSub [LinkTy, MultiLink] else Tgt
1044+ list[Tgt] if typing.IsAssignable [LinkTy, MultiLink] else Tgt
10481045 )
10491046
10501047And the final helper, ``PropsOnly[T] ``, generates a new type that
@@ -1056,7 +1053,7 @@ contains all the ``Property`` attributes of ``T``.
10561053 *[
10571054 typing.Member[typing.GetName[p], PointerArg[typing.GetType[p]]]
10581055 for p in typing.Iter[typing.Attrs[T]]
1059- if typing.IsSub [typing.GetType[p], Property]
1056+ if typing.IsAssignable [typing.GetType[p], Property]
10601057 ]
10611058 ]
10621059
@@ -1078,7 +1075,7 @@ suite, but here is a possible implementation of just ``Public``
10781075 # otherwise we return the type itself.
10791076 type GetDefault[Init] = (
10801077 GetFieldItem[Init, Literal["default"]]
1081- if typing.IsSub [Init, Field]
1078+ if typing.IsAssignable [Init, Field]
10821079 else Init
10831080 )
10841081
@@ -1092,7 +1089,7 @@ suite, but here is a possible implementation of just ``Public``
10921089 GetDefault[typing.GetInit[p]],
10931090 ]
10941091 for p in typing.Iter[typing.Attrs[T]]
1095- if not typing.IsSub [
1092+ if not typing.IsAssignable [
10961093 Literal[True],
10971094 GetFieldItem[typing.GetInit[p], Literal["primary_key"]],
10981095 ]
@@ -1131,7 +1128,7 @@ dataclasses-style method generation
11311128 # All arguments are keyword-only
11321129 # It takes a default if a default is specified in the class
11331130 Literal["keyword"]
1134- if typing.IsSub [
1131+ if typing.IsAssignable [
11351132 GetDefault[typing.GetInit[p]],
11361133 Never,
11371134 ]
@@ -1165,9 +1162,9 @@ NumPy-style broadcasting
11651162
11661163 type MergeOne[T, S] = (
11671164 T
1168- if typing.Matches [T, S] or typing.Matches [S, Literal[1]]
1165+ if typing.IsEquivalent [T, S] or typing.IsEquivalent [S, Literal[1]]
11691166 else S
1170- if typing.Matches [T, Literal[1]]
1167+ if typing.IsEquivalent [T, Literal[1]]
11711168 else typing.RaiseError[Literal["Broadcast mismatch"], T, S]
11721169 )
11731170
@@ -1176,7 +1173,7 @@ NumPy-style broadcasting
11761173
11771174 # Matching on Never here is intentional; it prevents infinite
11781175 # recursions when T is not a tuple.
1179- type Empty[T] = typing.IsSub [typing.Length[T], Literal[0]]
1176+ type Empty[T] = typing.IsAssignable [typing.Length[T], Literal[0]]
11801177
11811178 type Broadcast[T, S] = (
11821179 S
@@ -1227,7 +1224,7 @@ Generic Callable
12271224
12281225Consider a method with the following signature::
12291226
1230- def process[T](self, x: T) -> T if IsSub [T, list] else list[T]:
1227+ def process[T](self, x: T) -> T if IsAssignable [T, list] else list[T]:
12311228 ...
12321229
12331230The type of the method is generic, and the generic is bound at the
@@ -1240,13 +1237,13 @@ unbound type variables and let them be generalized::
12401237 type Foo = NewProtocol[
12411238 Member[
12421239 Literal["process"],
1243- Callable[[T], set[T] if IsSub [T, int] else T]
1240+ Callable[[T], set[T] if IsAssignable [T, int] else T]
12441241 ]
12451242 ]
12461243
12471244The problem is that this is basically incompatible with runtime
12481245evaluation support, since evaluating the alias ``Foo `` will need to
1249- evaluate the ``IsSub ``, and so we will lose one side of the
1246+ evaluate the ``IsAssignable ``, and so we will lose one side of the
12501247conditional at least. Similar problems will happen when evaluating
12511248``Members `` on a class with generic functions. By wrapping the body
12521249in a lambda, we can delay evaluation in both of these cases. (The
@@ -1356,8 +1353,8 @@ the model more complicated, but a lot of code will read much nicer.
13561353TODO: Should we do this?
13571354
13581355
1359- Replace ``IsSub `` with something weaker than "assignable to" checking
1360- ---------------------------------------------------------------------
1356+ Replace ``IsAssignable `` with something weaker than "assignable to" checking
1357+ ----------------------------------------------------------------------------
13611358
13621359Full python typing assignability checking is not fully implementable
13631360at runtime (in particular, even if all the typeshed types for the
@@ -1371,15 +1368,15 @@ ideally with the contours of that effort well-documented.
13711368An alternative approach would be to have a weaker predicate as the
13721369core primitive.
13731370
1374- One possibility would be a "sub-similarity" check: ``IsSubSimilar ``
1371+ One possibility would be a "sub-similarity" check: ``IsAssignableSimilar ``
13751372would do *simple * checking of the *head * of types, essentially,
13761373without looking at type parameters. It would not work with protocols.
13771374It would still lift over unions and would check literals.
13781375
13791376We decided it probably was not a good idea to introduce a new notion
13801377that is similar to but not the same as subtyping, and that would need
1381- to either have a long and weird name like ``IsSubSimilar `` or a
1382- misleading short one like ``IsSub ``.
1378+ to either have a long and weird name like ``IsAssignableSimilar `` or a
1379+ misleading short one like ``IsAssignable ``.
13831380
13841381.. _less_syntax :
13851382
0 commit comments