You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -93,6 +93,9 @@ Liskov substitutability or detecting problematic overloads.
93
93
It may be instructive to examine `typeshed <https://github.com/python/typeshed/>`__'s
94
94
`setup for testing stubs <https://github.com/python/typeshed/blob/main/tests/README.md>`__.
95
95
96
+
To suppress type errors in stubs, use ``# type: ignore`` comments. Refer to the :ref:`type-checker-error-suppression` section of the style guide for
97
+
error suppression formats specific to individual typecheckers.
98
+
96
99
..
97
100
TODO: consider adding examples and configurations for specific type checkers
98
101
@@ -113,18 +116,6 @@ Stub Content
113
116
This section documents best practices on what elements to include or
114
117
leave out of stub files.
115
118
116
-
Modules excluded from stubs
117
-
---------------------------
118
-
119
-
Not all modules should be included in stubs.
120
-
121
-
It is recommended to exclude:
122
-
123
-
1. 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
124
-
2. Modules that are not supposed to be imported, such as ``__main__.py``
125
-
3. Protected modules that start with a single ``_`` char. However, when needed protected modules can still be added (see :ref:`undocumented-objects` section below)
126
-
4. Tests
127
-
128
119
Public Interface
129
120
----------------
130
121
@@ -138,7 +129,17 @@ The following should always be included:
138
129
* All objects included in ``__all__`` (if present).
139
130
140
131
Other objects may be included if they are not prefixed with an underscore
141
-
or if they are being used in practice. (See the next section.)
132
+
or if they are being used in practice.
133
+
134
+
Modules excluded from stubs
135
+
---------------------------
136
+
137
+
The following should not be included in stubs:
138
+
139
+
1. 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
140
+
2. Modules that are not supposed to be imported, such as ``__main__.py``
141
+
3. Protected modules that start with a single ``_`` char. However, when needed protected modules can still be added (see :ref:`undocumented-objects` section below)
142
+
4. Tests
142
143
143
144
.. _undocumented-objects:
144
145
@@ -212,23 +213,28 @@ to use them freely to describe simple structural types.
212
213
Incomplete Stubs
213
214
----------------
214
215
216
+
When writing new stubs, it is not necessary to fully annotate all arguments,
217
+
return types, and fields. Some items may be left unannotated or
218
+
annotated with ``_typeshed.Incomplete`` (`documentation <https://github.com/python/typeshed/blob/main/stdlib/_typeshed/README.md>`_)::
219
+
220
+
from _typeshed import Incomplete
221
+
222
+
field: Incomplete # unannotated
223
+
224
+
def foo(x): ... # unannotated argument and return type
225
+
226
+
``_typeshed.Incomplete`` can also be used for partially known types::
* Use ``HasX`` for protocols that have readable and/or writable attributes
844
+
or getter/setter methods (e.g. ``HasItems``, ``HasFileno``).
845
+
846
+
.. _type-checker-error-suppression:
847
+
848
+
Type Checker Error Suppression Formats
849
+
--------------------------------------
850
+
851
+
* Use mypy error codes for mypy-specific ``# type: ignore`` annotations, e.g. ``# type: ignore[override]`` for Liskov Substitution Principle violations.
852
+
* Use pyright error codes for pyright-specific suppressions, e.g. ``# pyright: ignore[reportGeneralTypeIssues]``.
853
+
* If you need both on the same line, mypy's annotation needs to go first, e.g. ``# type: ignore[override] # pyright: ignore[reportGeneralTypeIssues]``.
854
+
855
+
856
+
``@deprecated``
857
+
---------------
858
+
859
+
The ``@typing_extensions.deprecated`` decorator (``@warnings.deprecated``
860
+
since Python 3.13) can be used to mark deprecated functionality; see
861
+
:pep:`702`.
862
+
863
+
Keep the deprecation message concise, but try to mention the projected
864
+
version when the functionality is to be removed, and a suggested
865
+
replacement.
866
+
867
+
Docstrings
868
+
----------
869
+
870
+
There are several tradeoffs around including docstrings in type stubs. Consider the intended purpose
871
+
of your stubs when deciding whether to include docstrings in your project's stubs.
872
+
873
+
* They do not affect type checking results and will be ignored by type checkers.
874
+
* Docstrings can improve certain IDE functionality, such as hover info.
875
+
* Duplicating docstrings between source code and stubs requires extra work to keep them in sync.
0 commit comments