Skip to content

Commit 37845a5

Browse files
committed
a bunch of clarifications based on questions from Jelle
1 parent e9134f5 commit 37845a5

1 file changed

Lines changed: 51 additions & 18 deletions

File tree

peps/pep-0827.rst

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ Operators <pep827-boolean-ops>`, defined below, potentially combined with
516516
``any``, the argument is a comprehension of type booleans, evaluated
517517
in the same way as the :ref:`unpacked comprehensions <pep827-unpacked>`.
518518

519-
When evaluated in type annotation context, they will evaluate to
519+
When evaluated in type annotation context, they will be equivalent to
520520
``Literal[True]`` or ``Literal[False]``.
521521

522522
We restrict what operators may be used in a conditional
@@ -563,6 +563,18 @@ and we want typechecking to match.
563563
Type operators
564564
--------------
565565

566+
Type operators are the core engine of type manipulation, and provide
567+
the primitives that are used to deconstruct types and construct new
568+
ones.
569+
570+
This section defines the operators being introduced, and explains how
571+
they are to be evaluated in a typechecking or type evaluation context.
572+
The actual runtime classes being introduced, though, are just regular classes,
573+
and subscripting them produces normal ``typing`` generic alias objects
574+
(with the partial exception of the boolean operators and ``Iter``,
575+
which produce aliases that have some dunder methods overloaded for
576+
:ref:`runtime hooks <pep827-rt-support>`).
577+
566578
Many of the operators specified have type bounds listed for some of
567579
their operands. These should be interpreted more as documentation than
568580
as exact type bounds. Trying to evaluate operators with invalid
@@ -607,22 +619,20 @@ Basic operators
607619

608620
Negative indexes work in the usual way.
609621

610-
Note that runtime evaluation will only be able to support proper classes
611-
as ``Base``, *not* protocols. So, for example, ``GetArg[Ty,
612-
Iterable, Literal[0]]`` to get the type of something iterable will
613-
fail in the runtime evaluator.
622+
(Note that runtime evaluators of type annotations are likely
623+
to struggle with using protocols as ``Base``. So, for example, ``GetArg[Ty,
624+
Iterable, Literal[0]]`` to get the type of something iterable may
625+
fail in a runtime evaluator of types.)
614626

615-
Special forms require special handling: the arguments list of a ``Callable``
616-
will be packed in a tuple, and a ``...`` will become
617-
``SpecialFormEllipsis``.
627+
Special forms require special handling: the arguments list of a
628+
``Callable`` will be packed in a tuple and a ``...`` will be treated
629+
as ``*args: Any`` and ``**kwargs: Any``, represented with the new
630+
``Param`` types.
618631

619632
* ``GetArgs[T, Base]``: returns a tuple containing all of the type
620633
arguments of ``T`` when interpreted as ``Base``, or ``Never`` if it
621634
cannot be.
622635

623-
* ``GetMemberType[T, S: Literal[str]]``: Extract the type of the
624-
member named ``S`` from the class ``T``.
625-
626636
* ``Length[T: tuple]`` - Gets the length of a tuple as an int literal
627637
(or ``Literal[None]`` if it is unbounded)
628638

@@ -634,6 +644,10 @@ Basic operators
634644
attributes are ``__name__``, ``__module__``, and ``__qualname__``.
635645
Returns the value as a ``Literal[str]``.
636646

647+
For non-class types, the principal should be that if ``x`` has type
648+
``T``, we want the value of ``type(x).<attr>``. So
649+
``GetSpecialAttr[Literal[1], "__name__"]`` should produce ``Literal["int"]``.
650+
637651
All of the operators in this section are :ref:`lifted over union types
638652
<pep827-lifting>`.
639653

@@ -650,17 +664,24 @@ Union processing
650664
Object inspection
651665
'''''''''''''''''
652666

667+
.. _pep827-members:
668+
653669
* ``Members[T]``: produces a ``tuple`` of ``Member`` types describing
654670
the members (attributes and methods) of class or typed dict ``T``.
655671

656-
In order to allow typechecking time and runtime evaluation to coincide
657-
more closely, **only members with explicit type annotations are included**.
672+
In order to allow typechecking time and runtime evaluation to
673+
coincide more closely, **only members with explicit type annotations
674+
are included**. (This is intended to also exclude unannotated
675+
methods, though see Open Issues.)
658676

659677
* ``Attrs[T]``: like ``Members[T]`` but only returns attributes (not
660678
methods).
661679

662680
* ``GetMember[T, S: Literal[str]]``: Produces a ``Member`` type for the
663-
member named ``S`` from the class ``T``.
681+
member named ``S`` from the class ``T``, or ``Never`` if it does not exist.
682+
683+
* ``GetMemberType[T, S: Literal[str]]``: Extract the type of the
684+
member named ``S`` from the class ``T``, or ``Never`` if it does not exist.
664685

665686
* ``Member[N: Literal[str], T, Q: MemberQuals, Init, D]``: ``Member``,
666687
is a simple type, not an operator, that is used to describe members
@@ -669,9 +690,11 @@ Object inspection
669690

670691
* ``N`` is the name, as a literal string type. Accessible with ``.name``.
671692
* ``T`` is the type. Accessible with ``.type``.
672-
* ``Q`` is a union of qualifiers (see ``MemberQuals`` below). Accessible with ``.quals``.
693+
* ``Q`` is a union of qualifiers (see ``MemberQuals``
694+
below). Accessible with ``.quals``. ``Never`` if no qualifiers.
673695
* ``Init`` is the literal type of the attribute initializer in the
674-
class (see :ref:`InitField <pep827-init-field>`). Accessible with ``.init``.
696+
class (see :ref:`InitField <pep827-init-field>`). Accessible with
697+
``.init``. ``Never`` if no initializer.
675698
* ``D`` is the defining class of the member. (That is, which class
676699
the member is inherited from. Always ``Never``, for a ``TypedDict``).
677700
Accessible with ``.definer``.
@@ -681,8 +704,10 @@ Object inspection
681704
member; currently ``ClassVar`` and ``Final`` apply to classes, and
682705
``NotRequired`` and ``ReadOnly`` apply to typed dicts.
683706

707+
Methods are functions, staticmethods, and classmethods that are
708+
defined class body. Properties should be treated as attributes.
684709

685-
Methods are returned as callables using the new ``Param`` based
710+
Methods are returned as callables that are introspectable as ``Param``-based
686711
extended callables, and carrying the ``ClassVar``
687712
qualifier. ``staticmethod`` and ``classmethod`` will return
688713
``staticmethod`` and ``classmethod`` types, which are subscriptable as
@@ -794,7 +819,7 @@ Update class
794819

795820
When a class is declared, if one or more of its ancestors have an
796821
``__init_subclass__`` with an ``UpdateClass`` return type, they are
797-
applied in reverse MRO order. N.B: If the ``cls`` param is
822+
applied in reverse MRO order. If the ``cls`` param is
798823
parameterized by ``type[T]``, then the class type should be
799824
substituted in for ``T``.
800825

@@ -1872,6 +1897,12 @@ Open Issues
18721897
there is a default, and have whether there is a default represented in
18731898
an ``init`` field, like we do for class member initializers with ``Member``.
18741899

1900+
* :ref:`Members <pep827-members>`: Should ``Members`` return all
1901+
methods, even those without annotations? We excluded them out of the
1902+
desire for some consistency with attributes, but it would not be
1903+
technically difficult to include them in either static or runtime
1904+
evaluators.
1905+
18751906
* :ref:`Generic Callable <pep827-generic-callable>`: Should we have any mechanisms
18761907
to inspect/destruct ``GenericCallable``? Maybe can fetch the variable
18771908
information and maybe can apply it to concrete types?
@@ -1884,6 +1915,8 @@ Open Issues
18841915
rejected. This does actually exactly mirror a potential **runtime**
18851916
evaluation-order dependence, though.
18861917

1918+
* Should ``RaiseError`` support string templating when outputing the types?
1919+
18871920
* Because of generic functions, there will be plenty of cases where we
18881921
can't evaluate a type operator (because it's applied to an unresolved
18891922
type variable), and exactly what the type evaluation rules should be

0 commit comments

Comments
 (0)