• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _stubs:
2
3**********
4Type Stubs
5**********
6
7Introduction
8============
9
10*type stubs*, also called *stub files*, provide type information for untyped
11Python packages and modules. Type stubs serve multiple purposes:
12
13* They are the only way to add type information to extension modules.
14* They can provide type information for packages that do not wish to
15  add them inline.
16* They can be distributed separately from the implementation.
17  This allows stubs to be developed at a different pace or by different
18  authors, which is especially useful when adding type annotations to
19  existing packages.
20* They can act as documentation, succinctly explaining the external
21  API of a package, without including the implementation or private
22  members.
23
24This document aims to give guidance to both authors of type stubs and developers
25of type checkers and other tools. It describes the constructs that can be used safely in type stubs,
26suggests a style guide for them, and lists constructs that type
27checkers are expected to support.
28
29Type stubs that only use constructs described in this document should work with
30all type checkers that also follow this document.
31Type stub authors can elect to use additional constructs, but
32must be prepared that some type checkers will not parse them as expected.
33
34A type checker that conforms to this document will parse a type stub that only uses
35constructs described here without error and will not interpret any
36construct in a contradictory manner. However, type checkers are not
37required to implement checks for all these constructs, and
38can elect to ignore unsupported ones. Additionally type checkers
39can support constructs not described in this document and tool authors are
40encouraged to experiment with additional features.
41
42Syntax
43======
44
45Type stubs are syntactically valid Python 3.7 files with a ``.pyi`` suffix.
46The Python syntax used for type stubs is independent from the Python
47versions supported by the implementation, and from the Python version the type
48checker runs under (if any). Therefore, type stub authors should use the
49latest available syntax features in stubs (up to Python 3.7), even if the
50implementation supports older, pre-3.7 Python versions.
51Type checker authors are encouraged to support syntax features from
52post-3.7 Python versions, although type stub authors should not use such
53features if they wish to maintain compatibility with all type checkers.
54
55For example, Python 3.7 added the ``async`` keyword (see PEP 492 [#pep492]_).
56Stub authors should use it to mark coroutines, even if the implementation
57still uses the ``@coroutine`` decorator. On the other hand, type stubs should
58not use the positional-only syntax from PEP 570 [#pep570]_, introduced in
59Python 3.8, although type checker authors are encouraged to support it.
60
61Stubs are treated as if ``from __future__ import annotations`` is enabled.
62In particular, built-in generics, pipe union syntax (``X | Y``), and forward
63references can be used.
64
65Starting with Python 3.8, the :py:mod:`ast` module from the standard library supports
66all syntax features required by this PEP. Older Python versions can use the
67`typed_ast <https://pypi.org/project/typed-ast/>`_ package from the
68Python Package Index, which also supports Python 3.7 syntax and ``# type``
69comments.
70
71Distribution
72============
73
74Type stubs can be distributed with or separately from the implementation;
75see PEP 561 [#pep561]_ for more information. The
76`typeshed <https://github.com/python/typeshed>`_ project
77includes stubs for Python's standard library and several third-party
78packages. The stubs for the standard library are usually distributed with type checkers and do not
79require separate installation. Stubs for third-party libraries are
80available on the `Python Package Index <https://pypi.org>`_. A stub package for
81a library called ``widget`` will be called ``types-widget``.
82
83Supported Constructs
84====================
85
86This sections lists constructs that type checkers will accept in type stubs.
87Type stub authors can safely use these constructs. If a
88construct is marked as "unspecified", type checkers may handle it
89as they best see fit or report an error. Linters should usually
90flag those constructs. Type stub authors should avoid using them to
91ensure compatibility across type checkers.
92
93Unless otherwise mentioned, type stubs support all features from the
94``typing`` module of the latest released Python version. If a stub uses
95typing features from a later Python version than what the implementation
96supports, these features can be imported from ``typing_extensions`` instead
97of ``typing``.
98
99For example, a stub could use ``Literal``, introduced in Python 3.8,
100for a library supporting Python 3.7+::
101
102    from typing_extensions import Literal
103
104    def foo(x: Literal[""]) -> int: ...
105
106Comments
107--------
108
109Standard Python comments are accepted everywhere Python syntax allows them.
110
111Two kinds of structured comments are accepted:
112
113* A ``# type: X`` comment at the end of a line that defines a variable,
114  declaring that the variable has type ``X``. However, PEP 526-style [#pep526]_
115  variable annotations are preferred over type comments.
116* A ``# type: ignore`` comment at the end of any line, which suppresses all type
117  errors in that line. The type checker mypy supports suppressing certain
118  type errors by using ``# type: ignore[error-type]``. This is not supported
119  by other type checkers and should not be used in stubs.
120
121Imports
122-------
123
124Type stubs distinguish between imports that are re-exported and those
125that are only used internally. Imports are re-exported if they use one of these
126forms:[#pep484]_
127
128* ``import X as X``
129* ``from Y import X as X``
130* ``from Y import *``
131
132Here are some examples of imports that make names available for internal use in
133a stub but do not re-export them::
134
135    import X
136    from Y import X
137    from Y import X as OtherX
138
139Type aliases can be used to re-export an import under a different name::
140
141    from foo import bar as _bar
142    new_bar = _bar  # "bar" gets re-exported with the name "new_bar"
143
144Sub-modules are always exported when they are imported in a module.
145For example, consider the following file structure::
146
147    foo/
148        __init__.pyi
149        bar.pyi
150
151Then ``foo`` will export ``bar`` when one of the following constructs is used in
152``__init__.pyi``::
153
154    from . import bar
155    from .bar import Bar
156
157Stubs support customizing star import semantics by defining a module-level
158variable called ``__all__``. In stubs, this must be a string list literal.
159Other types are not supported. Neither is the dynamic creation of this
160variable (for example by concatenation).
161
162By default, ``from foo import *`` imports all names in ``foo`` that
163do not begin with an underscore. When ``__all__`` is defined, only those names
164specified in ``__all__`` are imported::
165
166    __all__ = ['public_attr', '_private_looking_public_attr']
167
168    public_attr: int
169    _private_looking_public_attr: int
170    private_attr: int
171
172Type checkers support cyclic imports in stub files.
173
174Built-in Generics
175-----------------
176
177PEP 585 [#pep585]_ built-in generics are supported and should be used instead
178of the corresponding types from ``typing``::
179
180    from collections import defaultdict
181
182    def foo(t: type[MyClass]) -> list[int]: ...
183    x: defaultdict[int]
184
185Using imports from ``collections.abc`` instead of ``typing`` is
186generally possible and recommended::
187
188    from collections.abc import Iterable
189
190    def foo(iter: Iterable[int]) -> None: ...
191
192Unions
193------
194
195Declaring unions with ``Union`` and ``Optional`` is supported by all
196type checkers. With a few exceptions [#ts-4819]_, the shorthand syntax
197is also supported::
198
199    def foo(x: int | str) -> int | None: ...  # recommended
200    def foo(x: Union[int, str]) -> Optional[int]: ...  # ok
201
202Module Level Attributes
203-----------------------
204
205Module level variables and constants can be annotated using either
206type comments or variable annotation syntax::
207
208    x: int  # recommended
209    x: int = 0
210    x = 0  # type: int
211    x = ...  # type: int
212
213The type of a variable is unspecified when the variable is unannotated or
214when the annotation
215and the assigned value disagree. As an exception, the ellipsis literal can
216stand in for any type::
217
218    x = 0  # type is unspecified
219    x = ...  # type is unspecified
220    x: int = ""  # type is unspecified
221    x: int = ...  # type is int
222
223Classes
224-------
225
226Class definition syntax follows general Python syntax, but type checkers
227are only expected to understand the following constructs in class bodies:
228
229* The ellipsis literal ``...`` is ignored and used for empty
230  class bodies. Using ``pass`` in class bodies is undefined.
231* Instance attributes follow the same rules as module level attributes
232  (see above).
233* Method definitions (see below) and properties.
234* Method aliases.
235* Inner class definitions.
236
237More complex statements don't need to be supported::
238
239    class Simple: ...
240
241    class Complex(Base):
242        read_write: int
243        @property
244        def read_only(self) -> int: ...
245        def do_stuff(self, y: str) -> None: ...
246        doStuff = do_stuff
247
248The type of generic classes can be narrowed by annotating the ``self``
249argument of the ``__init__`` method::
250
251    class Foo(Generic[_T]):
252        @overload
253        def __init__(self: Foo[str], type: Literal["s"]) -> None: ...
254        @overload
255        def __init__(self: Foo[int], type: Literal["i"]) -> None: ...
256        @overload
257        def __init__(self, type: str) -> None: ...
258
259The class must match the class in which it is declared. Using other classes,
260including sub or super classes, will not work. In addition, the ``self``
261annotation cannot contain type variables.
262
263.. _supported-functions:
264
265Functions and Methods
266---------------------
267
268Function and method definition syntax follows general Python syntax.
269Unless an argument name is prefixed with two underscores (but not suffixed
270with two underscores), it can be used as a keyword argument [#pep484]_::
271
272    # x is positional-only
273    # y can be used positionally or as keyword argument
274    # z is keyword-only
275    def foo(__x, y, *, z): ...
276
277PEP 570 [#pep570]_ style positional-only parameters are currently not
278supported.
279
280If an argument or return type is unannotated, per PEP 484 [#pep484]_ its
281type is assumed to be ``Any``. It is preferred to leave unknown
282types unannotated rather than explicitly marking them as ``Any``, as some
283type checkers can optionally warn about unannotated arguments.
284
285If an argument has a literal or constant default value, it must match the implementation
286and the type of the argument (if specified) must match the default value.
287Alternatively, ``...`` can be used in place of any default value::
288
289    # The following arguments all have type Any.
290    def unannotated(a, b=42, c=...): ...
291    # The following arguments all have type int.
292    def annotated(a: int, b: int = 42, c: int = ...): ...
293    # The following default values are invalid and the types are unspecified.
294    def invalid(a: int = "", b: Foo = Foo()): ...
295
296For a class ``C``, the type of the first argument to a classmethod is
297assumed to be ``type[C]``, if unannotated. For other non-static methods,
298its type is assumed to be ``C``::
299
300    class Foo:
301        def do_things(self): ...  # self has type Foo
302        @classmethod
303        def create_it(cls): ...  # cls has type Type[Foo]
304        @staticmethod
305        def utility(x): ...  # x has type Any
306
307But::
308
309    _T = TypeVar("_T")
310
311    class Foo:
312        def do_things(self: _T) -> _T: ...  # self has type _T
313        @classmethod
314        def create_it(cls: _T) -> _T: ...  # cls has type _T
315
316PEP 612 [#pep612]_ parameter specification variables (``ParamSpec``)
317are supported in argument and return types::
318
319    _P = ParamSpec("_P")
320    _R = TypeVar("_R")
321
322    def foo(cb: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
323
324However, ``Concatenate`` from PEP 612 is not yet supported; nor is using
325a ``ParamSpec`` to parameterize a generic class.
326
327PEP 647 [#pep647]_ type guards are supported.
328
329Using a function or method body other than the ellipsis literal is currently
330unspecified. Stub authors may experiment with other bodies, but it is up to
331individual type checkers how to interpret them::
332
333    def foo(): ...  # compatible
334    def bar(): pass  # behavior undefined
335
336All variants of overloaded functions and methods must have an ``@overload``
337decorator::
338
339    @overload
340    def foo(x: str) -> str: ...
341    @overload
342    def foo(x: float) -> int: ...
343
344The following (which would be used in the implementation) is wrong in
345type stubs::
346
347    @overload
348    def foo(x: str) -> str: ...
349    @overload
350    def foo(x: float) -> int: ...
351    def foo(x: str | float) -> Any: ...
352
353Aliases and NewType
354-------------------
355
356Type checkers should accept module-level type aliases, optionally using
357``TypeAlias`` (PEP 613 [#pep613]_), e.g.::
358
359  _IntList = list[int]
360  _StrList: TypeAlias = list[str]
361
362Type checkers should also accept regular module-level or class-level aliases,
363e.g.::
364
365  def a() -> None: ...
366  b = a
367
368  class C:
369      def f(self) -> int: ...
370      g = f
371
372A type alias may contain type variables. As per PEP 484 [#pep484]_,
373all type variables must be substituted when the alias is used::
374
375  _K = TypeVar("_K")
376  _V = TypeVar("_V")
377  _MyMap: TypeAlias = dict[str, dict[_K, _V]]
378
379  # either concrete types or other type variables can be substituted
380  def f(x: _MyMap[str, _V]) -> _V: ...
381  # explicitly substitute in Any rather than using a bare alias
382  def g(x: _MyMap[Any, Any]) -> Any: ...
383
384Otherwise, type variables in aliases follow the same rules as type variables in
385generic class definitions.
386
387``typing.NewType`` is also supported in stubs.
388
389Decorators
390----------
391
392Type stubs may only use decorators defined in the ``typing`` module, plus a
393fixed set of additional ones:
394
395* ``classmethod``
396* ``staticmethod``
397* ``property`` (including ``.setter``)
398* ``abc.abstractmethod``
399* ``dataclasses.dataclass``
400* ``asyncio.coroutine`` (although ``async`` should be used instead)
401
402The behavior of other decorators should instead be incorporated into the types.
403For example, for the following function::
404
405  import contextlib
406  @contextlib.contextmanager
407  def f():
408      yield 42
409
410the stub definition should be::
411
412  from contextlib import AbstractContextManager
413  def f() -> AbstractContextManager[int]: ...
414
415Version and Platform Checks
416---------------------------
417
418Type stubs for libraries that support multiple Python versions can use version
419checks to supply version-specific type hints. Type stubs for different Python
420versions should still conform to the most recent supported Python version's
421syntax, as explain in the Syntax_ section above.
422
423Version checks are if-statements that use ``sys.version_info`` to determine the
424current Python version. Version checks should only check against the ``major`` and
425``minor`` parts of ``sys.version_info``. Type checkers are only required to
426support the tuple-based version check syntax::
427
428    if sys.version_info >= (3,):
429        # Python 3-specific type hints. This tuple-based syntax is recommended.
430    else:
431        # Python 2-specific type hints.
432
433    if sys.version_info >= (3, 5):
434        # Specific minor version features can be easily checked with tuples.
435
436    if sys.version_info < (3,):
437        # This is only necessary when a feature has no Python 3 equivalent.
438
439Type stubs should avoid checking against ``sys.version_info.major``
440directly and should not use comparison operators other than ``<`` and ``>=``.
441
442No::
443
444    if sys.version_info.major >= 3:
445        # Semantically the same as the first tuple check.
446
447    if sys.version_info[0] >= 3:
448        # This is also the same.
449
450    if sys.version_info <= (2, 7):
451        # This does not work because e.g. (2, 7, 1) > (2, 7).
452
453Some type stubs also may need to specify type hints for different platforms.
454Platform checks must be equality comparisons between ``sys.platform`` and the name
455of a platform as a string literal:
456
457Yes::
458
459    if sys.platform == 'win32':
460        # Windows-specific type hints.
461    else:
462        # Posix-specific type hints.
463
464No::
465
466    if sys.platform.startswith('linux'):
467        # Not necessary since Python 3.3.
468
469    if sys.platform in ['linux', 'cygwin', 'darwin']:
470        # Only '==' or '!=' should be used in platform checks.
471
472Version and platform comparisons can be chained using the ``and`` and ``or``
473operators::
474
475    if sys.platform == 'linux' and (sys.version_info < (3,) or sys,version_info >= (3, 7)): ...
476
477Enums
478-----
479
480Enum classes are supported in stubs, regardless of the Python version targeted by
481the stubs.
482
483Enum members may be specified just like other forms of assignments, for example as
484``x: int``, ``x = 0``, or ``x = ...``.  The first syntax is preferred because it
485allows type checkers to correctly type the ``.value`` attribute of enum members,
486without providing unnecessary information like the runtime value of the enum member.
487
488Additional properties on enum members should be specified with ``@property``, so they
489do not get interpreted by type checkers as enum members.
490
491Yes::
492
493    from enum import Enum
494
495    class Color(Enum):
496        RED: int
497        BLUE: int
498        @property
499        def rgb_value(self) -> int: ...
500
501    class Color(Enum):
502        # discouraged; type checkers will not understand that Color.RED.value is an int
503        RED = ...
504        BLUE = ...
505        @property
506        def rgb_value(self) -> int: ...
507
508No::
509
510    from enum import Enum
511
512    class Color(Enum):
513        RED: int
514        BLUE: int
515        rgb_value: int  # no way for type checkers to know that this is not an enum member
516
517Unsupported Features
518--------------------
519
520Currently, the following features are not supported by all type checkers
521and should not be used in stubs:
522
523* Positional-only argument syntax (PEP 570 [#pep570]_). Instead, use
524  the syntax described in the section :ref:`supported-functions`.
525  [#ts-4972]_
526
527Type Stub Content
528=================
529
530This section documents best practices on what elements to include or
531leave out of type stubs.
532
533Modules excluded fom stubs
534--------------------------
535
536Not all modules should be included into stubs.
537
538It is recommended to exclude:
539
5401. Implementation details, with `multiprocessing/popen_spawn_win32.py <https://github.com/python/cpython/blob/main/Lib/multiprocessing/popen_spawn_win32.py>`_ as a notable example
5412. Modules that are not supposed to be imported, such as ``__main__.py``
5423. Protected modules that start with a single ``_`` char. However, when needed protected modules can still be added (see :ref:`undocumented-objects` section below)
543
544Public Interface
545----------------
546
547Stubs should include the complete public interface (classes, functions,
548constants, etc.) of the module they cover, but it is not always
549clear exactly what is part of the interface.
550
551The following should always be included:
552
553* All objects listed in the module's documentation.
554* All objects included in ``__all__`` (if present).
555
556Other objects may be included if they are not prefixed with an underscore
557or if they are being used in practice. (See the next section.)
558
559.. _undocumented-objects:
560
561Undocumented Objects
562--------------------
563
564Undocumented objects may be included as long as they are marked with a comment
565of the form ``# undocumented``.
566
567Example::
568
569    def list2cmdline(seq: Sequence[str]) -> str: ...  # undocumented
570
571Such undocumented objects are allowed because omitting objects can confuse
572users. Users who see an error like "module X has no attribute Y" will
573not know whether the error appeared because their code had a bug or
574because the stub is wrong. Although it may also be helpful for a type
575checker to point out usage of private objects, false negatives (no errors for
576wrong code) are preferable over false positives (type errors
577for correct code). In addition, even for private objects a type checker
578can be helpful in pointing out that an incorrect type was used.
579
580``__all__``
581------------
582
583A type stub should contain an ``__all__`` variable if and only if it also
584present at runtime. In that case, the contents of ``__all__`` should be
585identical in the stub and at runtime. If the runtime dynamically adds
586or removes elements (for example if certain functions are only available on
587some platforms), include all possible elements in the stubs.
588
589Stub-Only Objects
590-----------------
591
592Definitions that do not exist at runtime may be included in stubs to aid in
593expressing types. Sometimes, it is desirable to make a stub-only class available
594to a stub's users - for example, to allow them to type the return value of a
595public method for which a library does not provided a usable runtime type::
596
597  from typing import Protocol
598
599  class _Readable(Protocol):
600      def read(self) -> str: ...
601
602  def get_reader() -> _Readable: ...
603
604Structural Types
605----------------
606
607As seen in the example with ``_Readable`` in the previous section, a common use
608of stub-only objects is to model types that are best described by their
609structure. These objects are called protocols [#pep544]_, and it is encouraged
610to use them freely to describe simple structural types.
611
612It is `recommended <#private-definitions>`_ to prefix stubs-only object names with ``_``.
613
614Incomplete Stubs
615----------------
616
617Partial stubs can be useful, especially for larger packages, but they should
618follow the following guidelines:
619
620* Included functions and methods should list all arguments, but the arguments
621  can be left unannotated.
622* Do not use ``Any`` to mark unannotated arguments or return values.
623* Partial classes should include a ``__getattr__()`` method marked with an
624  ``# incomplete`` comment (see example below).
625* Partial modules (i.e. modules that are missing some or all classes,
626  functions, or attributes) should include a top-level ``__getattr__()``
627  function marked with an ``# incomplete`` comment (see example below).
628* Partial packages (i.e. packages that are missing one or more sub-modules)
629  should have a ``__init__.pyi`` stub that is marked as incomplete (see above).
630  A better alternative is to create empty stubs for all sub-modules and
631  mark them as incomplete individually.
632
633Example of a partial module with a partial class ``Foo`` and a partially
634annotated function ``bar()``::
635
636    def __getattr__(name: str) -> Any: ...  # incomplete
637
638    class Foo:
639        def __getattr__(self, name: str) -> Any:  # incomplete
640        x: int
641        y: str
642
643    def bar(x: str, y, *, z=...): ...
644
645The ``# incomplete`` comment is mainly intended as a reminder for stub
646authors, but can be used by tools to flag such items.
647
648Attribute Access
649----------------
650
651Python has several methods for customizing attribute access: ``__getattr__``,
652``__getattribute__``, ``__setattr__``, and ``__delattr__``. Of these,
653``__getattr__`` and ``__setattr___`` should sometimes be included in stubs.
654
655In addition to marking incomplete definitions, ``__getattr__`` should be
656included when a class or module allows any name to be accessed. For example, consider
657the following class::
658
659  class Foo:
660      def __getattribute__(self, name):
661          return self.__dict__.setdefault(name)
662
663An appropriate stub definition is::
664
665  from typing import Any
666  class Foo:
667      def __getattr__(self, name: str) -> Any | None: ...
668
669Note that only ``__getattr__``, not ``__getattribute__``, is guaranteed to be
670supported in stubs.
671
672On the other hand, ``__getattr__`` should be omitted even if the source code
673includes it, if only limited names are allowed. For example, consider this class::
674
675  class ComplexNumber:
676      def __init__(self, n):
677          self._n = n
678      def __getattr__(self, name):
679          if name in ("real", "imag"):
680              return getattr(self._n, name)
681          raise AttributeError(name)
682
683In this case, the stub should list the attributes individually::
684
685  class ComplexNumber:
686      @property
687      def real(self) -> float: ...
688      @property
689      def imag(self) -> float: ...
690      def __init__(self, n: complex) -> None: ...
691
692``__setattr___`` should be included when a class allows any name to be set and
693restricts the type. For example::
694
695  class IntHolder:
696      def __setattr__(self, name, value):
697          if isinstance(value, int):
698              return super().__setattr__(name, value)
699          raise ValueError(value)
700
701A good stub definition would be::
702
703  class IntHolder:
704      def __setattr__(self, name: str, value: int) -> None: ...
705
706``__delattr__`` should not be included in stubs.
707
708Finally, even in the presence of ``__getattr__`` and ``__setattr__``, it is
709still recommended to separately define known attributes.
710
711Constants
712---------
713
714When the value of a constant is important, annotate it using ``Literal``
715instead of its type.
716
717Yes::
718
719    TEL_LANDLINE: Literal["landline"]
720    TEL_MOBILE: Literal["mobile"]
721    DAY_FLAG: Literal[0x01]
722    NIGHT_FLAG: Literal[0x02]
723
724No::
725
726    TEL_LANDLINE: str
727    TEL_MOBILE: str
728    DAY_FLAG: int
729    NIGHT_FLAG: int
730
731Documentation or Implementation
732-------------------------------
733
734Sometimes a library's documented types will differ from the actual types in the
735code. In such cases, type stub authors should use their best judgment. Consider
736these two examples::
737
738  def print_elements(x):
739      """Print every element of list x."""
740      for y in x:
741          print(y)
742
743  def maybe_raise(x):
744      """Raise an error if x (a boolean) is true."""
745      if x:
746          raise ValueError()
747
748The implementation of ``print_elements`` takes any iterable, despite the
749documented type of ``list``. In this case, annotate the argument as
750``Iterable[Any]``, to follow this PEP's style recommendation of preferring
751abstract types.
752
753For ``maybe_raise``, on the other hand, it is better to annotate the argument as
754``bool`` even though the implementation accepts any object. This guards against
755common mistakes like unintentionally passing in ``None``.
756
757If in doubt, consider asking the library maintainers about their intent.
758
759Style Guide
760===========
761
762The recommendations in this section are aimed at type stub authors
763who wish to provide a consistent style for type stubs. Type checkers
764should not reject stubs that do not follow these recommendations, but
765linters can warn about them.
766
767Stub files should generally follow the Style Guide for Python Code (PEP 8)
768[#pep8]_. There are a few exceptions, outlined below, that take the
769different structure of stub files into account and are aimed to create
770more concise files.
771
772Maximum Line Length
773-------------------
774
775Type stubs should be limited to 130 characters per line.
776
777Blank Lines
778-----------
779
780Do not use empty lines between functions, methods, and fields, except to
781group them with one empty line. Use one empty line around classes, but do not
782use empty lines between body-less classes, except for grouping.
783
784Yes::
785
786    def time_func() -> None: ...
787    def date_func() -> None: ...
788
789    def ip_func() -> None: ...
790
791    class Foo:
792        x: int
793        y: int
794        def __init__(self) -> None: ...
795
796    class MyError(Exception): ...
797    class AnotherError(Exception): ...
798
799No::
800
801    def time_func() -> None: ...
802
803    def date_func() -> None: ...  # do no leave unnecessary empty lines
804
805    def ip_func() -> None: ...
806
807
808    class Foo:  # leave only one empty line above
809        x: int
810    class MyError(Exception): ...  # leave an empty line between the classes
811
812Shorthand Syntax
813----------------
814
815Where possible, use shorthand syntax for unions instead of
816``Union`` or ``Optional``. ``None`` should be the last
817element of an union.
818
819Yes::
820
821    def foo(x: str | int) -> None: ...
822    def bar(x: str | None) -> int | None: ...
823
824No::
825
826    def foo(x: Union[str, int]) -> None: ...
827    def bar(x: Optional[str]) -> Optional[int]: ...
828    def baz(x: None | str) -> None: ...
829
830Module Level Attributes
831-----------------------
832
833Do not use an assignment for module-level attributes.
834
835Yes::
836
837    CONST: Literal["const"]
838    x: int
839
840No::
841
842    CONST = "const"
843    x: int = 0
844    y: float = ...
845    z = 0  # type: int
846    a = ...  # type: int
847
848Type Aliases
849------------
850
851Use ``TypeAlias`` for type aliases (but not for regular aliases).
852
853Yes::
854
855    _IntList: TypeAlias = list[int]
856    g = os.stat
857    Path = pathlib.Path
858    ERROR = errno.EEXIST
859
860No::
861
862    _IntList = list[int]
863    g: TypeAlias = os.stat
864    Path: TypeAlias = pathlib.Path
865    ERROR: TypeAlias = errno.EEXIST
866
867Classes
868-------
869
870Classes without bodies should use the ellipsis literal ``...`` in place
871of the body on the same line as the class definition.
872
873Yes::
874
875    class MyError(Exception): ...
876
877No::
878
879    class MyError(Exception):
880        ...
881    class AnotherError(Exception): pass
882
883Instance attributes and class variables follow the same recommendations as
884module level attributes:
885
886Yes::
887
888    class Foo:
889        c: ClassVar[str]
890        x: int
891
892No::
893
894    class Foo:
895        c: ClassVar[str] = ""
896        d: ClassVar[int] = ...
897        x = 4
898        y: int = ...
899
900Functions and Methods
901---------------------
902
903Use the same argument names as in the implementation, because
904otherwise using keyword arguments will fail. Of course, this
905does not apply to positional-only arguments, which are marked with a double
906underscore.
907
908Use the ellipsis literal ``...`` in place of actual default argument
909values. Use an explicit ``X | None`` annotation instead of
910a ``None`` default.
911
912Yes::
913
914    def foo(x: int = ...) -> None: ...
915    def bar(y: str | None = ...) -> None: ...
916
917No::
918
919    def foo(x: int = 0) -> None: ...
920    def bar(y: str = None) -> None: ...
921    def baz(z: str | None = None) -> None: ...
922
923Do not annotate ``self`` and ``cls`` in method definitions, except when
924referencing a type variable.
925
926Yes::
927
928    _T = TypeVar("_T")
929    class Foo:
930        def bar(self) -> None: ...
931        @classmethod
932        def create(cls: type[_T]) -> _T: ...
933
934No::
935
936    class Foo:
937        def bar(self: Foo) -> None: ...
938        @classmethod
939        def baz(cls: type[Foo]) -> int: ...
940
941The bodies of functions and methods should consist of only the ellipsis
942literal ``...`` on the same line as the closing parenthesis and colon.
943
944Yes::
945
946    def to_int1(x: str) -> int: ...
947    def to_int2(
948        x: str,
949    ) -> int: ...
950
951No::
952
953    def to_int1(x: str) -> int:
954        return int(x)
955    def to_int2(x: str) -> int:
956        ...
957    def to_int3(x: str) -> int: pass
958
959.. _private-definitions:
960
961Private Definitions
962-------------------
963
964Type variables, type aliases, and other definitions that should not
965be used outside the stub should be marked as private by prefixing them
966with an underscore.
967
968Yes::
969
970    _T = TypeVar("_T")
971    _DictList = Dict[str, List[Optional[int]]
972
973No::
974
975    T = TypeVar("T")
976    DictList = Dict[str, List[Optional[int]]]
977
978Language Features
979-----------------
980
981Use the latest language features available as outlined
982in the Syntax_ section, even for stubs targeting older Python versions.
983Do not use quotes around forward references and do not use ``__future__``
984imports.
985
986Yes::
987
988    class Py35Class:
989        x: int
990        forward_reference: OtherClass
991    class OtherClass: ...
992
993No::
994
995    class Py35Class:
996        x = 0  # type: int
997        forward_reference: 'OtherClass'
998    class OtherClass: ...
999
1000Types
1001-----
1002
1003Generally, use ``Any`` when a type cannot be expressed appropriately
1004with the current type system or using the correct type is unergonomic.
1005
1006Use ``float`` instead of ``int | float``.
1007Use ``None`` instead of ``Literal[None]``.
1008For argument types,
1009use ``bytes`` instead of ``bytes | memoryview | bytearray``.
1010
1011Use ``Text`` in stubs that support Python 2 when something accepts both
1012``str`` and ``unicode``. Avoid using ``Text`` in stubs or branches for
1013Python 3 only.
1014
1015Yes::
1016
1017    if sys.version_info < (3,):
1018        def foo(s: Text) -> None: ...
1019    else:
1020        def foo(s: str, *, i: int) -> None: ...
1021    def bar(s: Text) -> None: ...
1022
1023No::
1024
1025    if sys.version_info < (3,):
1026        def foo(s: unicode) -> None: ...
1027    else:
1028        def foo(s: Text, *, i: int) -> None: ...
1029
1030For arguments, prefer protocols and abstract types (``Mapping``,
1031``Sequence``, ``Iterable``, etc.). If an argument accepts literally any value,
1032use ``object`` instead of ``Any``.
1033
1034For return values, prefer concrete types (``list``, ``dict``, etc.) for
1035concrete implementations. The return values of protocols
1036and abstract base classes must be judged on a case-by-case basis.
1037
1038Yes::
1039
1040    def map_it(input: Iterable[str]) -> list[int]: ...
1041    def create_map() -> dict[str, int]: ...
1042    def to_string(o: object) -> str: ...  # accepts any object
1043
1044No::
1045
1046    def map_it(input: list[str]) -> list[int]: ...
1047    def create_map() -> MutableMapping[str, int]: ...
1048    def to_string(o: Any) -> str: ...
1049
1050Maybe::
1051
1052    class MyProto(Protocol):
1053        def foo(self) -> list[int]: ...
1054        def bar(self) -> Mapping[str]: ...
1055
1056Avoid union return types, since they require ``isinstance()`` checks.
1057Use ``Any`` or ``X | Any`` if necessary.
1058
1059Use built-in generics instead of the aliases from ``typing``,
1060where possible. See the section `Built-in Generics`_ for cases,
1061where it's not possible to use them.
1062
1063Yes::
1064
1065    from collections.abc import Iterable
1066
1067    def foo(x: type[MyClass]) -> list[str]: ...
1068    def bar(x: Iterable[str]) -> None: ...
1069
1070No::
1071
1072    from typing import Iterable, List, Type
1073
1074    def foo(x: Type[MyClass]) -> List[str]: ...
1075    def bar(x: Iterable[str]) -> None: ...
1076
1077NamedTuple and TypedDict
1078------------------------
1079
1080Use the class-based syntax for ``typing.NamedTuple`` and
1081``typing.TypedDict``, following the Classes section of this style guide.
1082
1083Yes::
1084
1085    from typing import NamedTuple, TypedDict
1086    class Point(NamedTuple):
1087        x: float
1088        y: float
1089
1090    class Thing(TypedDict):
1091        stuff: str
1092        index: int
1093
1094No::
1095
1096    from typing import NamedTuple, TypedDict
1097    Point = NamedTuple("Point", [('x', float), ('y', float)])
1098    Thing = TypedDict("Thing", {'stuff': str, 'index': int})
1099
1100References
1101==========
1102
1103PEPs
1104----
1105
1106.. [#pep8] PEP 8 -- Style Guide for Python Code, van Rossum et al. (https://www.python.org/dev/peps/pep-0008/)
1107.. [#pep484] PEP 484 -- Type Hints, van Rossum et al. (https://www.python.org/dev/peps/pep-0484)
1108.. [#pep492] PEP 492 -- Coroutines with async and await syntax, Selivanov (https://www.python.org/dev/peps/pep-0492/)
1109.. [#pep526] PEP 526 -- Syntax for Variable Annotations, Gonzalez et al. (https://www.python.org/dev/peps/pep-0526)
1110.. [#pep544] PEP 544 -- Protocols: Structural Subtyping, Levkivskyi et al. (https://www.python.org/dev/peps/pep-0544)
1111.. [#pep561] PEP 561 -- Distributing and Packaging Type Information, Smith (https://www.python.org/dev/peps/pep-0561)
1112.. [#pep570] PEP 570 -- Python Positional-Only Parameters, Hastings et al. (https://www.python.org/dev/peps/pep-0570)
1113.. [#pep585] PEP 585 -- Type Hinting Generics In Standard Collections, Langa (https://www.python.org/dev/peps/pep-0585)
1114.. [#pep604] PEP 604 -- Allow writing union types as X | Y, Prados and Moss (https://www.python.org/dev/peps/pep-0604)
1115.. [#pep612] PEP 612 -- Parameter Specification Variables, Mendoza (https://www.python.org/dev/peps/pep-0612)
1116.. [#pep613] PEP 613 -- Explicit Type Aliases, Zhu (https://www.python.org/dev/peps/pep-0613)
1117.. [#pep647] PEP 647 -- User-Defined Type Guards, Traut (https://www.python.org/dev/peps/pep-0647)
1118.. [#pep3107] PEP 3107 -- Function Annotations, Winter and Lownds (https://www.python.org/dev/peps/pep-3107)
1119
1120Bugs
1121----
1122
1123.. [#ts-4819] typeshed issue #4819 -- PEP 604 tracker (https://github.com/python/typeshed/issues/4819)
1124.. [#ts-4972] typeshed issue #4972 -- PEP 570 tracker (https://github.com/python/typeshed/issues/4972)
1125
1126Copyright
1127=========
1128
1129This document is placed in the public domain or under the CC0-1.0-Universal license, whichever is more permissive.
1130