Skip to content

Commit d3a652a

Browse files
committed
fix backticks, add Docstrings section
1 parent 0cc549e commit d3a652a

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

docs/guides/writing_stubs.rst

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ more concise files.
503503
Syntax Example
504504
--------------
505505

506-
The below is an excerpt from the types for the `datetime` module.
506+
The below is an excerpt from the types for the ``datetime`` module.
507507

508508
MAXYEAR: int
509509
MINYEAR: int
@@ -680,13 +680,12 @@ No::
680680
...
681681
def to_int3(x: str) -> int: pass
682682

683-
Avoid invariant collection types (`list`, `dict`) for function parameters,
684-
in favor of covariant types like `Mapping` or `Sequence`.
683+
Avoid invariant collection types (``list``, ``dict``) for function parameters,
684+
in favor of covariant types like ``Mapping`` or ``Sequence``.
685685

686686
Avoid union return types. See https://github.com/python/mypy/issues/1693
687687

688-
Use `float` instead of `int | float` for parameter annotations.
689-
See [PEP 484](https://peps.python.org/pep-0484/#the-numeric-tower).
688+
Use ``float`` instead of ``int | float`` for parameter annotations. See :pep:`484` for more details.
690689

691690
Language Features
692691
-----------------
@@ -723,7 +722,7 @@ older versions of Python.
723722
Platform-dependent APIs
724723
-----------------------
725724

726-
Use platform checks like `if sys.platform == 'win32'` to denote platform-dependent APIs.
725+
Use :ref:`version-and-platform-checks<platform checks>` like ``if sys.platform == 'win32'`` to denote platform-dependent APIs.
727726

728727
NamedTuple and TypedDict
729728
------------------------
@@ -752,7 +751,7 @@ No::
752751
Built-in Generics
753752
-----------------
754753

755-
:pep:`585` built-in generics (such as `list`, `dict`, `tuple`, `set`) are supported and should be used instead
754+
:pep:`585` built-in generics (such as ``list``, ``dict``, ``tuple``, ``set``) are supported and should be used instead
756755
of the corresponding types from ``typing``::
757756

758757
from collections import defaultdict
@@ -770,30 +769,30 @@ generally possible and recommended::
770769
Unions
771770
------
772771

773-
Declaring unions with the shorthand `|` syntax is recommended and supported by
772+
Declaring unions with the shorthand ``|`` syntax is recommended and supported by
774773
all type checkers::
775774

776775
def foo(x: int | str) -> int | None: ... # recommended
777776
def foo(x: Union[int, str]) -> Optional[int]: ... # ok
778777

779778
.. _using-any:
780779

781-
Using `Any` and `object`
782-
------------------------
780+
Using ``Any`` and ``object``
781+
----------------------------
783782

784-
When adding type hints, avoid using the `Any` type when possible. Reserve
785-
the use of `Any` for when:
783+
When adding type hints, avoid using the ``Any`` type when possible. Reserve
784+
the use of ``Any`` for when:
786785
* the correct type cannot be expressed in the current type system; and
787786
* to avoid union returns (see above).
788787

789-
Note that `Any` is not the correct type to use if you want to indicate
788+
Note that ``Any`` is not the correct type to use if you want to indicate
790789
that some function can accept literally anything: in those cases use
791-
`object` instead.
790+
``object`` instead.
792791

793-
When using `Any`, document the reason for using it in a comment. Ideally,
792+
When using ``Any``, document the reason for using it in a comment. Ideally,
794793
document what types could be used.
795794

796-
The `Any` Trick
795+
The ``Any`` Trick
797796
-----------------
798797

799798
In cases where a function or method can return ``None``, but where forcing the
@@ -822,17 +821,17 @@ Context Managers
822821
----------------
823822

824823
When adding type annotations for context manager classes, annotate
825-
the return type of `__exit__` as bool only if the context manager
826-
sometimes suppresses exceptions -- if it sometimes returns `True`
824+
the return type of ``__exit__`` as bool only if the context manager
825+
sometimes suppresses exceptions -- if it sometimes returns ``True``
827826
at runtime. If the context manager never suppresses exceptions,
828-
have the return type be either `None` or `bool | None`. If you
827+
have the return type be either ``None`` or ``bool | None``. If you
829828
are not sure whether exceptions are suppressed or not or if the
830-
context manager is meant to be subclassed, pick `bool | None`.
829+
context manager is meant to be subclassed, pick ``bool | None``.
831830
See https://github.com/python/mypy/issues/7214 for more details.
832831

833-
`__enter__` methods and other methods that return instances of the
834-
current class should be annotated with `typing_extensions.Self`
835-
([example](https://github.com/python/typeshed/blob/3581846/stdlib/contextlib.pyi#L151)).
832+
``__enter__`` methods and other methods that return ``self`` or ``cls(...)``
833+
should be annotated with the `typing.Self`
834+
(`example <https://github.com/python/typeshed/blob/3581846/stdlib/contextlib.pyi#L151>`_).
836835

837836
Naming
838837
------
@@ -845,29 +844,39 @@ A few guidelines for protocol names below. In cases that don't fall
845844
into any of those categories, use your best judgement.
846845

847846
* Use plain names for protocols that represent a clear concept
848-
(e.g. `Iterator`, `Container`).
849-
* Use `SupportsX` for protocols that provide callable methods (e.g.
850-
`SupportsInt`, `SupportsRead`, `SupportsReadSeek`).
851-
* Use `HasX` for protocols that have readable and/or writable attributes
852-
or getter/setter methods (e.g. `HasItems`, `HasFileno`).
847+
(e.g. ``Iterator``, ``Container``).
848+
* Use ``SupportsX`` for protocols that provide callable methods (e.g.
849+
``SupportsInt``, ``SupportsRead``, ``SupportsReadSeek``).
850+
* Use ``HasX`` for protocols that have readable and/or writable attributes
851+
or getter/setter methods (e.g. ``HasItems``, ``HasFileno``).
853852

854853
.. _type-checker-error-suppression:
855854

856855
Type Checker Error Suppression Formats
857856
--------------------------------------
858857

859-
* Use mypy error codes for mypy-specific `# type: ignore` annotations, e.g. `# type: ignore[override]` for Liskov Substitution Principle violations.
860-
* Use pyright error codes for pyright-specific suppressions, e.g. `# pyright: ignore[reportGeneralTypeIssues]`.
861-
* If you need both on the same line, mypy's annotation needs to go first, e.g. `# type: ignore[override] # pyright: ignore[reportGeneralTypeIssues]`.
858+
* Use mypy error codes for mypy-specific ``# type: ignore`` annotations, e.g. ``# type: ignore[override]`` for Liskov Substitution Principle violations.
859+
* Use pyright error codes for pyright-specific suppressions, e.g. ``# pyright: ignore[reportGeneralTypeIssues]``.
860+
* If you need both on the same line, mypy's annotation needs to go first, e.g. ``# type: ignore[override] # pyright: ignore[reportGeneralTypeIssues]``.
862861

863862

864-
`@deprecated`
865-
-------------
863+
``@deprecated``
864+
---------------
866865

867-
The `@typing_extensions.deprecated` decorator (`@warnings.deprecated`
866+
The ``@typing_extensions.deprecated`` decorator (``@warnings.deprecated``
868867
since Python 3.13) can be used to mark deprecated functionality; see
869-
[PEP 702](https://peps.python.org/pep-0702/).
868+
:pep:`702`.
870869

871870
Keep the deprecation message concise, but try to mention the projected
872871
version when the functionality is to be removed, and a suggested
873872
replacement.
873+
874+
Docstrings
875+
----------
876+
877+
There are several tradeoffs around including docstrings in type stubs. Consider the intended purpose
878+
of your stubs when deciding whether to include docstrings in your project's stubs.
879+
880+
* They do not affect type checking results and will be ignored by type checkers.
881+
* Docstrings can improve certain IDE functionality, such as hover info.
882+
* Duplicating docstrings between source code and stubs requires extra work to keep them in sync.

0 commit comments

Comments
 (0)