Skip to content

Commit

Permalink
feat: add 'type hint for typeddict' recipe. (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-six authored May 23, 2024
1 parent c49f51a commit 84cb1c0
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 79 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@

- [Type Hint](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Basic Types](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_basic_type)
- [Union Types: `|`, ~~`typing.Union`~~, ~~`typing.Optional`~~](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_union)
- [Constants and Class Attributes: `typing.Final`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_constant)
- [`TypedDict`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_typeddict)
- [`namedtuple`: `typing.NamedTuple`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_namedtuple)
- [`itertools.chain`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_itertools_chain)
- [socket](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_socket)
- [Regex](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_regex)
- [Literal: `typing.Literal`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_literal)
- [Union Types: `|`, ~~`typing.Union`~~, ~~`typing.Optional`~~](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_union)
- [Any: `typing.Any` and `object`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_any)
- [Type objects](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_type)
- [Callable objects](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_callable)
- [Regex](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_regex)
- [socket](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_socket)
- [Constants and Class Attributes: `typing.Final`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_constant)
- [Class Variables: `typing.ClassVar`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_class_var)
- [Restricting Inheritance and Overriding: `@typing.final`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_inheritance)
- [`typing.NoReturn`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_noreturn)
Expand Down
1 change: 1 addition & 0 deletions cookbook/core/type_hint/type_hint.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ are handled by evaluating them in `globalns` and `localns` namespaces.
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 586 – Literal Types](https://peps.python.org/pep-0586/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
- [GitHub - `mypy`](https://github.com/python/mypy)
- [PyPI - `typing-extensions` package](https://pypi.org/project/typing-extensions/)
2 changes: 1 addition & 1 deletion cookbook/core/type_hint/type_hint_for_any.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ x3: tuple[Any, ...] # tuple of items with any type and any size

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)
10 changes: 1 addition & 9 deletions cookbook/core/type_hint/type_hint_for_basic_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@ def func(arg: int) -> int: ...

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)

## References

- [PEP 585](https://peps.python.org/pep-0585/ "PEP 585 - Type Hinting Generics In Standard Collections")
- [Python - `typing` module](https://docs.python.org/3/library/typing.html)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [PEP 526 - Syntax for Variable Annotations](https://peps.python.org/pep-0526/)
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
- [PyPI - `typing-extensions` package](https://pypi.org/project/typing-extensions/)
9 changes: 1 addition & 8 deletions cookbook/core/type_hint/type_hint_for_callable.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,9 @@ Callable[[...], ReturnType] # variable arguements

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)

## References

- [PEP 585 – Type Hinting Generics In Standard Collections](https://peps.python.org/pep-0585/)
- [Python - `typing` module](https://docs.python.org/3/library/typing.html)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [PEP 526 - Syntax for Variable Annotations](https://peps.python.org/pep-0526/)
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
11 changes: 2 additions & 9 deletions cookbook/core/type_hint/type_hint_for_class_var.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,8 @@ class C:

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)

## References

- [Python - `typing` module](https://docs.python.org/3/library/typing.html)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [PEP 526 - Syntax for Variable Annotations](https://peps.python.org/pep-0526/)
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
- [Python - `typing.ClassVar` module](https://docs.python.org/3/library/typing.html#typing.ClassVar)
12 changes: 2 additions & 10 deletions cookbook/core/type_hint/type_hint_for_constant.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,9 @@ class ImmutablePoint:

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)

## References

- [PEP 591 – Adding a final qualifier to typing](https://peps.python.org/pep-0591/)
- [Python - `typing` module](https://docs.python.org/3/library/typing.html)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [PEP 526 - Syntax for Variable Annotations](https://peps.python.org/pep-0526/)
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
- [PyPI - `typing-extensions` package](https://pypi.org/project/typing-extensions/)
- [Python - `typing.Final` module](https://docs.python.org/3/library/typing.html#typing.Final)
12 changes: 2 additions & 10 deletions cookbook/core/type_hint/type_hint_for_inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,9 @@ The method decorator version may be used with all of *instance methods*, *class

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)

## References

- [PEP 591 – Adding a final qualifier to typing](https://peps.python.org/pep-0591/)
- [Python - `typing` module](https://docs.python.org/3/library/typing.html)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [PEP 526 - Syntax for Variable Annotations](https://peps.python.org/pep-0526/)
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
- [PyPI - `typing-extensions` package](https://pypi.org/project/typing-extensions/)
- [Python - `typing.final` module](https://docs.python.org/3/library/typing.html#typing.final)
2 changes: 1 addition & 1 deletion cookbook/core/type_hint/type_hint_for_itertools_chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ itertools.chain(...) -> itertools.chain[int]: ...

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)
10 changes: 1 addition & 9 deletions cookbook/core/type_hint/type_hint_for_literal.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@ x: Literal[1, 2, True, False] # one of 1, 2, True, False

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)

## References

- [Python - `typing` module](https://docs.python.org/3/library/typing.html)
- [Python - `typing.Literal` module](https://docs.python.org/3/library/typing.html#typing.Literal)
- [PEP 586 – Literal Types](https://peps.python.org/pep-0586/)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [PEP 526 - Syntax for Variable Annotations](https://peps.python.org/pep-0526/)
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
7 changes: 0 additions & 7 deletions cookbook/core/type_hint/type_hint_for_namedtuple.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,3 @@ Point = namedtuple('Point', ['x', 'y'])

- [PEP 585 – Type Hinting Generics In Standard Collections](https://peps.python.org/pep-0585/)
- [Python - `typing` module](https://docs.python.org/3/library/typing.html)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [PEP 526 - Syntax for Variable Annotations](https://peps.python.org/pep-0526/)
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
2 changes: 1 addition & 1 deletion cookbook/core/type_hint/type_hint_for_noreturn.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def func(arg: int, arg2: str = 'a') -> NoReturn:

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)
2 changes: 1 addition & 1 deletion cookbook/core/type_hint/type_hint_for_regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ m: re.Match[bytes] = re.match(rb'xxx', b'xxx')

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)
2 changes: 1 addition & 1 deletion cookbook/core/type_hint/type_hint_for_socket.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ address_family: socket.AddressFamily = socket.AF_INET # or `socket.AF_INET6`

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)

## References

Expand Down
2 changes: 1 addition & 1 deletion cookbook/core/type_hint/type_hint_for_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ e: Type[BaseException]

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)
32 changes: 32 additions & 0 deletions cookbook/core/type_hint/type_hint_for_typeddict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Type Hint for `typing.TypedDict`

`NotRequired`: New in Python *3.11*.

## Recipes

```python
from typing import TypedDict, NotRequired

class MyDict(TypedDict):
key_a: int
key_b: float
label: NotRequired[str]


# functional syntax: alternative syntax
MyDict = TypedDict('MyDict', {'key_a': int, 'key_b': float, 'label': NotRequired})


d: MyDict = {}
```

## More Details

- [Type Hint](type_hint)

## References

- [Python - `typing.TypedDict` module](https://docs.python.org/3/library/typing.html#typing.TypedDict)
- [Python - `typing.NotRequired` module](https://docs.python.org/3/library/typing.html#typing.NotRequired)
- [PEP 655 – Marking individual `TypedDict` items as required or potentially-missing](https://peps.python.org/pep-0655/)
- [typing - `Required`/`NotRequired`](https://typing.readthedocs.io/en/latest/spec/typeddict.html#required-notrequired)
8 changes: 1 addition & 7 deletions cookbook/core/type_hint/type_hint_for_union.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,10 @@ issubclass(bool, (int, float))

## More Details

- [Type Hint](https://leven-cn.github.io/python-cookbook/cookbook/core/type_hint/type_hint)
- [Type Hint](type_hint)

## References

- [Python - `typing` module](https://docs.python.org/3/library/typing.html)
- [Python - `typing.Union` module](https://docs.python.org/3/library/typing.html#typing.Union)
- [Python - `typing.Optional` module](https://docs.python.org/3/library/typing.html#typing.Optional)
- [PEP 604 – Allow writing union types as `X | Y`](https://peps.python.org/pep-0604/)
- [PEP 526 - Syntax for Variable Annotations](https://peps.python.org/pep-0526/)
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)

0 comments on commit 84cb1c0

Please sign in to comment.