Skip to content

Add Doc from PEP 727: https://peps.python.org/pep-0727/ #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 16 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from typing_extensions import clear_overloads, get_overloads, overload
from typing_extensions import NamedTuple
from typing_extensions import override, deprecated, Buffer, TypeAliasType, TypeVar, get_protocol_members, is_protocol
from typing_extensions import doc, DocInfo
from _typed_dict_test_helper import Foo, FooGeneric, VeryAnnotated

# Flags used to mark tests that only apply after a specific
Expand Down Expand Up @@ -5895,5 +5896,20 @@ class MyAlias(TypeAliasType):
pass


class DocTests(BaseTestCase):
def test_annotation(self):

def hi(to: Annotated[str, doc("Who to say hi to")]) -> None: pass

hints = get_type_hints(hi, include_extras=True)
doc_info = hints["to"].__metadata__[0]
self.assertEqual(doc_info.documentation, "Who to say hi to")
self.assertIsInstance(doc_info, DocInfo)

def test_repr(self):
doc_info = doc("Who to say hi to")
self.assertEqual(repr(doc_info), "DocInfo('Who to say hi to')")


if __name__ == '__main__':
main()
40 changes: 40 additions & 0 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
'clear_overloads',
'dataclass_transform',
'deprecated',
'doc',
'DocInfo',
'get_overloads',
'final',
'get_args',
Expand Down Expand Up @@ -2809,6 +2811,44 @@ def get_protocol_members(tp: type, /) -> typing.FrozenSet[str]:
return frozenset(tp.__protocol_attrs__)
return frozenset(_get_protocol_attrs(tp))

if hasattr(typing, "doc"):
doc = typing.doc
DocInfo = typing.DocInfo
else:
class DocInfo:
"""Container for documentation information as returned by ``doc()``.

It is expected this class wouldn't be manually created but instead returned
by ``doc()`` inside of type annotations using ``Annotated``.

The ``documentation`` attribute contains the documentation string passed
to ``doc()``.
"""
def __init__(self, documentation: str):
self.documentation = documentation

def __repr__(self) -> str:
return f"DocInfo({self.documentation!r})"

def doc(documentation: str) -> DocInfo:
"""Define the documentation of a type annotation using ``Annotated``, to be
used in class attributes, function and method parameters, return values,
and variables.

The value should be a string literal to allow static tools like editors
to use it.

This complements docstrings.

It returns a class ``DocInfo`` instance containing the documentation value in
the ``documentation`` attribute.

Example::

>>> from typing_extensions import doc, Annotated
>>> def hi(to: Annotated[str, doc("Who to say hi to")]) -> None: ...
"""
return DocInfo(documentation)

# Aliases for items that have always been in typing.
# Explicitly assign these (rather than using `from typing import *` at the top),
Expand Down