@@ -264,10 +264,10 @@ Generics can be parametrized by using a new factory available in
264
264
In this case the contract is that the returned value is consistent with
265
265
the elements held by the collection.
266
266
267
- ``TypeVar`` supports constraining parametric types to a fixed set of possible
268
- types. For example, we can define a type variable that ranges over just
269
- ``str`` and ``bytes``. By default, a type variable ranges over all
270
- possible types. Example of constraining a type variable::
267
+ ``TypeVar`` supports constraining parametric types to a fixed set of
268
+ possible types. For example, we can define a type variable that ranges
269
+ over just ``str`` and ``bytes``. By default, a type variable ranges
270
+ over all possible types. Example of constraining a type variable::
271
271
272
272
from typing import TypeVar
273
273
@@ -305,11 +305,10 @@ This is equivalent to omitting the generic notation and just saying
305
305
User-defined generic types
306
306
--------------------------
307
307
308
- You can include a ``Generic`` base class to define a user-defined class as generic.
309
- Example::
308
+ You can include a ``Generic`` base class to define a user-defined class
309
+ as generic. Example::
310
310
311
311
from typing import TypeVar, Generic
312
- ...
313
312
314
313
T = TypeVar('T')
315
314
@@ -330,21 +329,21 @@ Example::
330
329
def log(self, message: str) -> None:
331
330
self.logger.info('{}: {}'.format(self.name message))
332
331
333
- ``Generic[T]`` as a base class defines that the class ``LoggedVar`` takes a
334
- single type parameter ``T``. This also makes ``T`` valid as a type within
335
- the class body.
332
+ ``Generic[T]`` as a base class defines that the class ``LoggedVar``
333
+ takes a single type parameter ``T``. This also makes ``T`` valid as
334
+ a type within the class body.
336
335
337
- The ``Generic`` base class uses a metaclass that defines ``__getitem__`` so
338
- that ``LoggedVar[t]`` is valid as a type::
336
+ The ``Generic`` base class uses a metaclass that defines ``__getitem__``
337
+ so that ``LoggedVar[t]`` is valid as a type::
339
338
340
339
from typing import Iterable
341
340
342
341
def zero_all_vars(vars: Iterable[LoggedVar[int]]) -> None:
343
342
for var in vars:
344
343
var.set(0)
345
344
346
- A generic type can have any number of type variables, and type variables may
347
- be constrained. This is valid::
345
+ A generic type can have any number of type variables, and type variables
346
+ may be constrained. This is valid::
348
347
349
348
from typing import TypeVar, Generic
350
349
...
@@ -355,15 +354,15 @@ be constrained. This is valid::
355
354
class Pair(Generic[T, S]):
356
355
...
357
356
358
- Each type variable argument to ``Generic`` must be distinct. This is thus
359
- invalid::
357
+ Each type variable argument to ``Generic`` must be distinct. This is
358
+ thus invalid::
360
359
361
360
from typing import TypeVar, Generic
362
361
...
363
362
364
363
T = TypeVar('T')
365
364
366
- class Pair(Generic[T, T]):
365
+ class Pair(Generic[T, T]): # INVALID
367
366
...
368
367
369
368
You can use multiple inheritance with ``Generic``::
@@ -379,12 +378,12 @@ You can use multiple inheritance with ``Generic``::
379
378
Arbitrary generic types as base classes
380
379
---------------------------------------
381
380
382
- ``Generic[T]`` is only valid as a base class -- it's not a proper
383
- type. However, user-defined generic types such as ``LinkedList[T]``
384
- from the above example and built-in generic types and ABCs such as
385
- ``List[T]`` and ``Iterable[T]`` are valid both as types and as base
386
- classes. For example, we can define a subclass of ``Dict`` that
387
- specializes type arguments::
381
+ ``Generic[T]`` is only valid as a base class -- it's not a proper type.
382
+ However, user-defined generic types such as ``LinkedList[T]`` from the
383
+ above example and built-in generic types and ABCs such as ``List[T]``
384
+ and ``Iterable[T]`` are valid both as types and as base classes. For
385
+ example, we can define a subclass of ``Dict`` that specializes type
386
+ arguments::
388
387
389
388
from typing import Dict, List, Optional
390
389
@@ -402,8 +401,7 @@ specializes type arguments::
402
401
nodes = self.get(name)
403
402
if nodes:
404
403
return nodes[-1]
405
- else:
406
- return None
404
+ return None
407
405
408
406
``SymbolTable`` is a subclass of ``dict`` and a subtype of ``Dict[str,
409
407
List[Node]]``.
@@ -463,9 +461,9 @@ The local and global namespace in which it is evaluated should be the
463
461
same namespaces in which default arguments to the same function would
464
462
be evaluated.
465
463
466
- Moreover, the expression should be parseable as a valid type hint,
467
- i.e., it is constrained by the rules from the section `` Acceptable
468
- type hints`` above.
464
+ Moreover, the expression should be parseable as a valid type hint, i.e.,
465
+ it is constrained by the rules from the section `Acceptable type hints`_
466
+ above.
469
467
470
468
It is allowable to use string literals as *part* of a type hint, for
471
469
example::
@@ -527,7 +525,7 @@ When the type of a value is ``object``, the type checker will reject
527
525
almost all operations on it, and assigning it to a variable (or using
528
526
it as a return value) of a more specialized type is a type error. On
529
527
the other hand, when a value has type ``Any``, the type checker will
530
- allow all operations on it, and a value of type `Any`` can be assigned
528
+ allow all operations on it, and a value of type `` Any`` can be assigned
531
529
to a variable (or used as a return value) of a more constrained type.
532
530
533
531
@@ -654,12 +652,12 @@ Compatibility with other uses of function annotations
654
652
=====================================================
655
653
656
654
A number of existing or potential use cases for function annotations
657
- exist, which are incompatible with type hinting. These may confuse a
658
- static type checker. However, since type hinting annotations have no
659
- runtime behavior (other than evaluation of the annotation expression
660
- and storing annotations in the ``__annotations__`` attribute of the
661
- function object), this does not make the program incorrect -- it just
662
- may cause a type checker to emit spurious warnings or errors.
655
+ exist, which are incompatible with type hinting. These may confuse
656
+ a static type checker. However, since type hinting annotations have no
657
+ runtime behavior (other than evaluation of the annotation expression and
658
+ storing annotations in the ``__annotations__`` attribute of the function
659
+ object), this does not make the program incorrect -- it just may cause
660
+ a type checker to emit spurious warnings or errors.
663
661
664
662
To mark portions of the program that should not be covered by type
665
663
hinting, you can use one or more of the following:
@@ -674,10 +672,10 @@ hinting, you can use one or more of the following:
674
672
For more details see later sections.
675
673
676
674
In order for maximal compatibility with offline type checking it may
677
- eventually be a good idea to change interfaces that rely on
678
- annotations to switch to a different mechanism, for example a
679
- decorator. In Python 3.5 there is no pressure to do this, however.
680
- See also the longer discussion under " Rejected alternatives" below.
675
+ eventually be a good idea to change interfaces that rely on annotations
676
+ to switch to a different mechanism, for example a decorator. In Python
677
+ 3.5 there is no pressure to do this, however. See also the longer
678
+ discussion under ` Rejected alternatives`_ below.
681
679
682
680
683
681
Type comments
@@ -744,12 +742,11 @@ the type of ``x`` is ``t``. At runtime a cast always returns the
744
742
expression unchanged -- it does not check the type, and it does not
745
743
convert or coerce the value.
746
744
747
- Casts differ from type comments (see the previous section). When
748
- using a type comment, the type checker should still verify that the
749
- inferred type is consistent with the stated type. When using a cast,
750
- the type checker should blindly believe the programmer. Also, casts
751
- can be used in expressions, while type comments only apply to
752
- assignments.
745
+ Casts differ from type comments (see the previous section). When using
746
+ a type comment, the type checker should still verify that the inferred
747
+ type is consistent with the stated type. When using a cast, the type
748
+ checker should blindly believe the programmer. Also, casts can be used
749
+ in expressions, while type comments only apply to assignments.
753
750
754
751
755
752
Stub Files
@@ -761,23 +758,25 @@ stub files:
761
758
762
759
* Extension modules
763
760
764
- * 3rd party modules whose authors have not yet added type hints
761
+ * Third- party modules whose authors have not yet added type hints
765
762
766
- * Standard library modules for which type hints have not yet been written
763
+ * Standard library modules for which type hints have not yet been
764
+ written
767
765
768
766
* Modules that must be compatible with Python 2 and 3
769
767
770
768
* Modules that use annotations for other purposes
771
769
772
- Stub files have the same syntax as regular Python modules. There is
773
- one feature of the ``typing`` module that may only be used in stub
774
- files: the ``@overload`` decorator described below.
770
+ Stub files have the same syntax as regular Python modules. There is one
771
+ feature of the ``typing`` module that may only be used in stub files:
772
+ the ``@overload`` decorator described below.
775
773
776
774
The type checker should only check function signatures in stub files;
777
- function bodies in stub files should just be a single ``pass`` statement.
775
+ function bodies in stub files should just be a single ``pass``
776
+ statement.
778
777
779
- The type checker should have a configurable search path for stub
780
- files. If a stub file is found the type checker should not read the
778
+ The type checker should have a configurable search path for stub files.
779
+ If a stub file is found the type checker should not read the
781
780
corresponding "real" module.
782
781
783
782
While stub files are syntactically valid Python modules, they use the
@@ -850,6 +849,54 @@ satisfactory multiple dispatch design, but we don't want such a design
850
849
to be constrained by the overloading syntax defined for type hints in
851
850
stub files.
852
851
852
+ Storing and distributing stub files
853
+ -----------------------------------
854
+
855
+ The easiest form of stub file storage and distribution is to put them
856
+ alongside Python modules in the same directory. This makes them easy to
857
+ find by both programmers and the tools. However, since package
858
+ maintainers are free not to add type hinting to their packages,
859
+ third-party stubs installable by ``pip`` from PyPI are also supported.
860
+ In this case we have to consider three issues: naming, versioning,
861
+ installation path.
862
+
863
+ This PEP does not provide a recommendation on a naming scheme that
864
+ should be used for third-party stub file packages. Discoverability will
865
+ hopefully be based on package popularity, like with Django packages for
866
+ example.
867
+
868
+ Third-party stubs have to be versioned using the lowest version of the
869
+ source package that is compatible. Example: FooPackage has versions
870
+ 1.0, 1.1, 1.2, 1.3, 2.0, 2.1, 2.2. There are API changes in versions
871
+ 1.1, 2.0 and 2.2. The stub file package maintainer is free to release
872
+ stubs for all versions but at least 1.0, 1.1, 2.0 and 2.2 are required
873
+ to enable the end user type check all versions. This is because the
874
+ user knows that the closest *lower or equal* version of stubs is
875
+ compatible. In the provided example, for FooPackage 1.3 the user would
876
+ choose stubs version 1.1.
877
+
878
+ Note that if the user decides to use the "latest" available source
879
+ package, using the "latest" stub files should generally also work if
880
+ they're updated often.
881
+
882
+ Third-party stub packages can use any location for stub storage. The
883
+ type checker will search for them using PYTHONPATH. A default fallback
884
+ directory that is always checked is ``shared/typehints/python3.5/`` (or
885
+ 3.6, etc.). Since there can only be one package installed for a given
886
+ Python version per environment, no additional versioning is performed
887
+ under that directory (just like bare directory installs by ``pip`` in
888
+ site-packages). Stub file package authors might use the following
889
+ snippet in ``setup.py``::
890
+
891
+ ...
892
+ data_files=[
893
+ (
894
+ 'shared/typehints/python{}.{}'.format(*sys.version_info[:2]),
895
+ pathlib.Path(SRC_PATH).glob('**/*.pyi'),
896
+ ),
897
+ ],
898
+ ...
899
+
853
900
854
901
Exceptions
855
902
==========
0 commit comments