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
Copy file name to clipboardExpand all lines: docs/guides/writing_stubs.rst
+14-32Lines changed: 14 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,10 +93,8 @@ 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 on stubs:
97
-
* use mypy error codes for mypy-specific `# type: ignore` annotations, e.g. `# type: ignore[override]` for Liskov Substitution Principle violations.
98
-
* use pyright error codes for pyright-specific suppressions, e.g. `# pyright: ignore[reportGeneralTypeIssues]`. Pyright is configured to discard `# type: ignore` annotations.
99
-
* If you need both on the same line, mypy's annotation needs to go first, e.g. `# type: ignore[override] # pyright: ignore[reportGeneralTypeIssues]`.
96
+
To suppress type errors on stubs, use a `# type: ignore` comment. Refer to the style guide for
97
+
error suppression formats specific to individual typecheckers.
100
98
101
99
..
102
100
TODO: consider adding examples and configurations for specific type checkers
@@ -538,7 +536,7 @@ No::
538
536
539
537
class Foo: # leave only one empty line above
540
538
x: int
541
-
class MyError(Exception): ...
539
+
class MyError(Exception): ... # leave an empty line between the classes
542
540
543
541
Module Level Attributes
544
542
-----------------------
@@ -802,37 +800,21 @@ into any of those categories, use your best judgement.
802
800
* Use `HasX` for protocols that have readable and/or writable attributes
803
801
or getter/setter methods (e.g. `HasItems`, `HasFileno`).
804
802
803
+
Type Checker Error Suppression formats
804
+
--------------------------------------
805
+
806
+
* Use mypy error codes for mypy-specific `# type: ignore` annotations, e.g. `# type: ignore[override]` for Liskov Substitution Principle violations.
807
+
* Use pyright error codes for pyright-specific suppressions, e.g. `# pyright: ignore[reportGeneralTypeIssues]`. Pyright is configured to discard `# type: ignore` annotations.
808
+
* If you need both on the same line, mypy's annotation needs to go first, e.g. `# type: ignore[override] # pyright: ignore[reportGeneralTypeIssues]`.
809
+
810
+
805
811
`@deprecated`
806
812
-------------
807
813
808
814
The `@typing_extensions.deprecated` decorator (`@warnings.deprecated`
809
815
since Python 3.13) can be used to mark deprecated functionality; see
810
816
[PEP 702](https://peps.python.org/pep-0702/).
811
817
812
-
A few guidelines for how to use it:
813
-
814
-
* In the standard library, apply the decorator only in Python versions
815
-
where an appropriate replacement for the deprecated functionality
816
-
exists. If in doubt, apply the decorator only on versions where the
817
-
functionality has been explicitly deprecated, either through runtime
818
-
warnings or in the documentation. Use `if sys.version_info` checks to
819
-
apply the decorator only to some versions.
820
-
* Keep the deprecation message concise, but try to mention the projected
821
-
version when the functionality is to be removed, and a suggested
822
-
replacement.
823
-
824
-
Imports
825
-
-------
826
-
827
-
Imports in stubs are considered private (not part of the exported API)
828
-
unless:
829
-
* they use the form ``from library import name as name`` (sic, using explicit ``as`` even if the name stays the same); or
830
-
* they use the form ``from library import *`` which means all names from that library are exported.
831
-
832
-
Forward References
833
-
------------------
834
-
835
-
Stub files support forward references natively. In other words, the
836
-
order of class declarations and type aliases does not matter in
837
-
a stub file. Unlike regular Python files, you can use the name of the class within its own
838
-
body without writing it as a comment.
818
+
Keep the deprecation message concise, but try to mention the projected
819
+
version when the functionality is to be removed, and a suggested
0 commit comments