✕
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions docs/guides/writing_stubs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ annotated with ``_typeshed.Incomplete`` (`documentation <https://github.com/pyth

``_typeshed.Incomplete`` can also be used for partially known types::

def foo(x: Incomplete | None = None) -> list[Incomplete]: ...
def foo(x: Incomplete | None) -> list[Incomplete]: ...

Partial stubs can be useful, especially for larger packages, but they should
follow the following guidelines:
Expand Down Expand Up @@ -647,6 +647,18 @@ No::
def foo(x: X = X()) -> None: ...
def bar(y: str = None) -> None: ...

*Exception:* If ``X`` is ``Incomplete`` and has a ``None`` default, leave the
argument unannotated.

Yes::

def incomplete1(z: Incomplete | None) -> None: ...
def incomplete2(z=None) -> None: ...

No::

def incomplete(z: Incomplete | None = None): ...

Comment on lines +650 to +661

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree with this, see #2387 (comment). So can we leave this out? The other bits I'm fine with, so if you want we can discuss this further in a separate PR for this part.

Do not annotate ``self`` and ``cls`` in method definitions unless an explicit
annotation is needed to constrain or relax the receiver type.

Expand Down Expand Up @@ -690,12 +702,10 @@ No::
def to_int3(x: str) -> int: pass

Avoid invariant collection types (``list``, ``dict``) for function parameters,
in favor of covariant types like ``Mapping`` or ``Sequence``.
in favor of covariant protocols and other types like ``Iterable`` or ``Mapping``.

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

Use ``float`` instead of ``int | float`` for parameter annotations. See :pep:`484` for more details.

Language Features
-----------------

Expand Down
Loading