google-deepmind / google-deepmind/tree

Add type hints to public APIs

Open
#84 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1k
Forks
73
PR merge metrics
No merged PRs in 30d

Description

Hello,

I'd like to propose a feature - to add type hints to public functions.

The most basic version of this would be to type hint return types in terms of `TypeVar` of input types, like:
```python
S = TypeVar("S") # type of a structure
def map_structure(func, *structures: S, **kwargs) -> S:
...
```

This could be taken further by making structure generic over it's item types, allowing to type hint functions like `flatten`:
```python
Item = TypeVar("Item")
Node = Union["Structure[Item]", Item]

Structure = Union[
Node,
Sequence['Node'],
Mapping[Any, 'Node'],
'AnyNamedTuple',
]

def flatten(structure: Structure[Item]) -> list[Item]:
...
```

Overall, this should help users avoid preventable bugs and have more readable code. Thanks for considering :)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.