-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Python type parameter lists (#366)
Sphinx has partial support for type parameter lists: they are supported by the Python domain in signatures, but are not supported by autodoc. This adds the following support: - Sphinx Python domain for type parameter fields in docstrings, with sphinx.ext.napoleon support as well. - Support for type parameters as Sphinx objects, with cross-linking, like the existing support for function parameters as Sphinx objects. - Support in apigen for PEP 695 type parameters, and for displaying pre-PEP 695 separately-defined TypeVar types as PEP 695 type parameters. Co-authored-by: Brendan <[email protected]>
- Loading branch information
Showing
14 changed files
with
920 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import collections.abc | ||
from typing import ( | ||
TypeVar, | ||
Generic, | ||
overload, | ||
Iterable, | ||
Optional, | ||
KeysView, | ||
ValuesView, | ||
ItemsView, | ||
Iterator, | ||
Union, | ||
) | ||
|
||
K = TypeVar("K") | ||
V = TypeVar("V") | ||
T = TypeVar("T") | ||
U = TypeVar("U") | ||
|
||
|
||
class Map(Generic[K, V]): | ||
"""Maps keys of type :py:param:`.K` to values of type :py:param:`.V`. | ||
Type parameters: | ||
K: | ||
Key type. | ||
V: | ||
Mapped value type. | ||
Group: | ||
type-param | ||
""" | ||
|
||
@overload | ||
def __init__(self): ... | ||
|
||
@overload | ||
def __init__(self, items: collections.abc.Mapping[K, V]): ... | ||
|
||
@overload | ||
def __init__(self, items: Iterable[tuple[K, V]]): ... | ||
|
||
def __init__(self, items): | ||
"""Construct from the specified items.""" | ||
... | ||
|
||
def clear(self): | ||
"""Clear the map.""" | ||
... | ||
|
||
def keys(self) -> KeysView[K]: | ||
"""Return a dynamic view of the keys.""" | ||
... | ||
|
||
def items(self) -> ItemsView[K, V]: | ||
"""Return a dynamic view of the items.""" | ||
... | ||
|
||
def values(self) -> ValuesView[V]: | ||
"""Return a dynamic view of the values.""" | ||
... | ||
|
||
@overload | ||
def get(self, key: K) -> Optional[V]: ... | ||
|
||
@overload | ||
def get(self, key: K, default: V) -> V: ... | ||
|
||
@overload | ||
def get(self, key: K, default: T) -> Union[V, T]: ... | ||
|
||
def get(self, key: K, default=None): | ||
"""Return the mapped value, or the specified default.""" | ||
... | ||
|
||
def __len__(self) -> int: | ||
"""Return the number of items in the map.""" | ||
... | ||
|
||
def __contains__(self, key: K) -> bool: | ||
"""Check if the map contains :py:param:`.key`.""" | ||
... | ||
|
||
def __getitem__(self, key: K) -> V: | ||
"""Return the value associated with :py:param:`.key`. | ||
Raises: | ||
KeyError: if :py:param:`.key` is not present. | ||
""" | ||
... | ||
|
||
def __setitem__(self, key: K, value: V): | ||
"""Set the value associated with the specified key.""" | ||
... | ||
|
||
def __delitem__(self, key: K): | ||
"""Remove the value associated with the specified key. | ||
Raises: | ||
KeyError: if :py:param:`.key` is not present. | ||
""" | ||
... | ||
|
||
def __iter__(self) -> Iterator[K]: | ||
"""Iterate over the keys.""" | ||
... | ||
|
||
|
||
class Derived(Map[int, U], Generic[U]): | ||
"""Map from integer keys to arbitrary values. | ||
Type parameters: | ||
U: Mapped value type. | ||
Group: | ||
type-param | ||
""" | ||
|
||
pass | ||
|
||
|
||
__all__ = ["Map", "Derived"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.