Skip to content

Commit d2d599f

Browse files
rchen152srittaucarljmJelleZijlstrapre-commit-ci[bot]
authored
Merge the specification portion of the "Type Stubs" doc into the spec (#1815)
* Type Stubs: move intro and syntax to spec. * Moves some content into spec, deletes duplicate spec content. * Adds placeholders for where remaining content will be moved. It will be split between the spec and the writing_stubs doc. * Move "Stub Contents" to writing_stubs. Moves the section on what to include in stubs with editorial changes only - e.g., changing "type stub" to "stub file" to match surrounding terminology. * Moves "Style Guide" to writing_stubs doc. Moves the style guide with a few updates: * Editorial changes like tweaking code examples to use recommended style. * Attribute declarations can use assignments when needed to convey extra information - e.g., final attributes and enum members. * Tweaks a reference to the double-underscore convention for positional-only arguments to note that it's historical. * Change :ref:`stubs` occurrences to :ref:`stub-files`. * Move a few "Supported Constructs" sections into spec. The only substantive change is an update to "Comments" to note that many formats of error suppression comments are allowed. * Migrated "Imports" and "Module Level Attributes" supported constructs. Added `x: Final = <literal>` to module-level attributes section. No other changes. * Migrate "Enums" supported construct section. Replaces outdated info with a link to the enums section of the spec. * Move "Classes" and "Decorators" supported constructs sections. Added an example of an inner class to "Classes." Removed mention of asyncio.coroutine from "Decorators" as it's very deprecated. Added functions decorated with `@dataclass_transform` to list of decorators that type checkers should recognize. * Port "Functions and Methods" supported constructs section. Removed mention of individual supported/unsupported features from this section. Where we state that all typing features from the latest released version are supported, added a caveat with a link to the typeshed feature tracker. * Adds an "Import Conventions" section (deduplication). * Remove missed reference to deleted stubs doc. * Fix broken refs. * Formatting fix to docs/guides/writing_stubs.rst Co-authored-by: Sebastian Rittau <[email protected]> * Reword paragraph on argument names to be more concise. Co-authored-by: Sebastian Rittau <[email protected]> * Wording clarification in docs/guides/writing_stubs.rst Co-authored-by: Sebastian Rittau <[email protected]> * Modernize code example in docs/guides/writing_stubs.rst Co-authored-by: Sebastian Rittau <[email protected]> * Typo fix in docs/spec/distributing.rst Co-authored-by: Sebastian Rittau <[email protected]> * Typo fix in docs/spec/distributing.rst Co-authored-by: Sebastian Rittau <[email protected]> * Grammar fixes * Property deleters should also be understood. * Address future notes. * Fix silly formatting error. * Undelete stubs.rst. * Address review comments. * Reword some sections to focus on type checker behavior. * Move or remove some sections that fit better in the writing_stubs guide or are redundant. * Address more review comments. Cuts redundant sections, moves things to writing_stubs, tries out a new "these should be supported fully" and "these should be supported to at least the described minimum" structure. * Remove no-longer-needed anchor. * And add back an accidentally deleted newline... * Slight rewording of Decorators advice. * Address more reviewer comments. Moves "Library Interface" into spec, further trims redundant text. * Remove extra colon. * Revert changes to wrting_stubs that should be made separately. * Delete unnecessary sentence. Co-authored-by: Carl Meyer <[email protected]> * Clarify some language and terminology. * Address reviewer feedback. * Formatting * Drop "annotation contexts." * Add "in annotation expressions" to newer syntax explanation. * Update docs/spec/distributing.rst Co-authored-by: Jelle Zijlstra <[email protected]> * Reword syntax section. * Update docs/spec/distributing.rst Co-authored-by: Jelle Zijlstra <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks --------- Co-authored-by: Sebastian Rittau <[email protected]> Co-authored-by: Carl Meyer <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e74d960 commit d2d599f

File tree

7 files changed

+209
-548
lines changed

7 files changed

+209
-548
lines changed

docs/guides/libraries.rst

+3-46
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ library:
4343

4444
Inline type annotations simply refers to the use of annotations within your
4545
``.py`` files. In contrast, with type stub files, type information lives in
46-
separate ``.pyi`` files; see :ref:`stubs` and :ref:`writing_stubs` for more
46+
separate ``.pyi`` files; see :ref:`stub-files` and :ref:`writing_stubs` for more
4747
details.
4848

4949
We recommend using the inline type annotations approach, since it has the
@@ -193,51 +193,8 @@ How much of my library needs types?
193193
A "py.typed" library should aim to be type complete so that type
194194
checking and inspection can work to their full extent. Here we say that a
195195
library is “type complete” if all of the symbols
196-
that comprise its interface have type annotations that refer to types
197-
that are fully known. Private symbols are exempt.
198-
199-
Library interface (public and private symbols)
200-
----------------------------------------------
201-
202-
If a ``py.typed`` module is present, a type checker will treat all modules
203-
within that package (i.e. all files that end in ``.py`` or ``.pyi``) as
204-
importable unless the file name begins with an underscore. These modules
205-
comprise the supported interface for the library.
206-
207-
Each module exposes a set of symbols. Some of these symbols are
208-
considered "private” — implementation details that are not part of the
209-
library’s interface. Type checkers can use the following rules
210-
to determine which symbols are visible outside of the package.
211-
212-
- Symbols whose names begin with an underscore (but are not dunder
213-
names) are considered private.
214-
- Imported symbols are considered private by default. If they use the
215-
``import A as A`` (a redundant module alias), ``from X import A as A`` (a
216-
redundant symbol alias), or ``from . import A`` forms, symbol ``A`` is
217-
not private unless the name begins with an underscore. If a file
218-
``__init__.py`` uses form ``from .A import X``, symbol ``A`` is treated
219-
likewise. If a wildcard import (of the form ``from X import *``) is
220-
used, all symbols referenced by the wildcard are not private.
221-
- A module can expose an ``__all__`` symbol at the module level that
222-
provides a list of names that are considered part of the interface.
223-
This overrides all other rules above, allowing imported symbols or
224-
symbols whose names begin with an underscore to be included in the
225-
interface.
226-
- Local variables within a function (including nested functions) are
227-
always considered private.
228-
229-
The following idioms are supported for defining the values contained
230-
within ``__all__``. These restrictions allow type checkers to statically
231-
determine the value of ``__all__``.
232-
233-
- ``__all__ = ('a', b')``
234-
- ``__all__ = ['a', b']``
235-
- ``__all__ += ['a', b']``
236-
- ``__all__ += submodule.__all__``
237-
- ``__all__.extend(['a', b'])``
238-
- ``__all__.extend(submodule.__all__)``
239-
- ``__all__.append('a')``
240-
- ``__all__.remove('a')``
196+
that comprise its :ref:`interface <library-interface>` have type annotations
197+
that refer to types that are fully known. Private symbols are exempt.
241198

242199
Type Completeness
243200
-----------------

docs/guides/writing_stubs.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Writing and Maintaining Stub Files
55
**********************************
66

77
Stub files are a means of providing type information for Python modules.
8-
For a full reference, refer to :ref:`stubs`.
8+
For a full reference, refer to :ref:`stub-files`.
99

1010
Maintaining stubs can be a little cumbersome because they are separated from the
1111
implementation. This page lists some tools that make writing and maintaining

docs/reference/index.rst

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Type System Reference
88

99
generics
1010
protocols
11-
stubs
1211
best_practices
1312
quality
1413
typing Module Documentation <https://docs.python.org/3/library/typing.html>

0 commit comments

Comments
 (0)