Skip to content

Commit fe3a091

Browse files
authored
Update with our new plans for IsSub (#67)
1 parent 48d0722 commit fe3a091

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

design-qs.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Big (open?) questions
33

44
1.
55
Can we actually implement IsSubtype at runtime in a satisfactory way?
6-
(PROBABLE DECISION: external library *and* restricted checking.)
6+
(PROBABLE DECISION: external library *and* "full" best effort checking.)
77

88
- There is a lot that needs to happen, like protocols and variance
99
inference and callable subtyping (which might require matching

pep.rst

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@ typing. The situation was substantially improved by the introducing of
261261
``ParamSpec`` in :pep:`PEP 612 <612>`, but a number of patterns remain
262262
unsupported:
263263

264-
* Adding/removing/modifying a keyword parameter
265-
* Modifying a variable number of parameters -- XXX: check how well TypeVarTuple does this
264+
* Adding/removing/modifying a keyword parameter
265+
* Adding/removing/modifying a variable number of parameters. (Though
266+
``TypeVarTuple`` is close to being able to support adding and
267+
removing, if multiple unpackings were to be allowed, and Pyre
268+
implemented a ``Map`` operator that allowed modifying multiple.)
266269

267270
This proposal will cover those cases.
268271

269-
XXX: Ehhhhhh the generic situation could be bad for some of it? For
270-
partial certainly, I think, which otherwise we can almost do.
271-
272272
NumPy-style broadcasting
273273
------------------------
274274

@@ -534,11 +534,11 @@ proposing to add that as a notion, but we could.
534534
Boolean operators
535535
'''''''''''''''''
536536

537-
* ``IsSub[T, S]``: What we would **want** is that it returns a boolean
538-
literal type indicating whether ``T`` is a subtype of ``S``.
539-
To support runtime checking, we probably need something weaker.
537+
* ``IsSub[T, S]``: Returns a boolean literal type indicating whether
538+
``T`` is a subtype of ``S``.
540539

541-
TODO: Discuss this in detail.
540+
(Actually, I suppose, whether ``S`` is "assignable to" ``T``. We can
541+
certainly bikeshed on this name.)
542542

543543
* ``Matches[T, S]``:
544544
Equivalent to ``IsSub[T, S] and IsSub[S, T]``.
@@ -560,10 +560,13 @@ Basic operators
560560

561561
Negative indexes work in the usual way.
562562

563-
N.B: *Unfortunately* ``Base`` must be a proper class, *not* a
564-
protocol. So, for example, ``GetArg[Ty, Iterable, 0]]`` to get the
565-
type of something iterable *won't* work. This is because we can't do
566-
protocol checks at runtime in general. Special forms unfortunately
563+
N.B: Runtime evaluation will only be able to support proper classes
564+
as ``Base``, *not* protocols. So, for example, ``GetArg[Ty,
565+
Iterable, 0]]`` to get the type of something iterable will need to
566+
fail in a runtime evaluator. We should be able to allow it
567+
statically though.
568+
569+
Special forms unfortunately
567570
require some special handling: the arguments list of a ``Callable``
568571
will be packed in a tuple, and a ``...`` will become
569572
``SpecialFormEllipsis``.
@@ -1143,6 +1146,31 @@ Support TypeScript style pattern matching in subtype checking
11431146
This would almost certainly only be possible if we also decide not to
11441147
care about runtime evaluation, as above.
11451148

1149+
Replace ``IsSub`` with something weaker than "assignable to" checking
1150+
---------------------------------------------------------------------
1151+
1152+
Full python typing assignability checking is not fully implementable
1153+
at runtime (in particular, even if all the typeshed types for the
1154+
stdlib were made available, checking against protocols will often not
1155+
be possible, because class attributes may be inferred and have no visible
1156+
presence at runtime).
1157+
1158+
As proposed, a runtime evaluator will need to be "best effort",
1159+
ideally with the contours of that effort well-documented.
1160+
1161+
An alternative approach would be to have a weaker predicate as the
1162+
core primitive.
1163+
1164+
One possibility would be a "sub-similarity" check: ``IsSubSimilar``
1165+
would do *simple* checking of the *head* of types, essentially,
1166+
without looking at type parameters. It would not work with protocols.
1167+
It would still lift over unions and would check literals.
1168+
1169+
We decided it probably was not a good idea to introduce a new notion
1170+
that is similar to but not the same as subtyping, and that would need
1171+
to either have a long and weird name like ``IsSubSimilar`` or a
1172+
misleading short one like ``IsSub``.
1173+
11461174
.. _less_syntax:
11471175

11481176

0 commit comments

Comments
 (0)