misc_py312 API documentation

# misc_py312

Testing features that either are 3.12+ only or render slightly different on 3.12.

View Source

     1# mypy: ignore-errors
     2# https://github.com/python/mypy/issues/16607
     3"""
     4Testing features that either are 3.12+ only or render slightly different on 3.12.
     5"""
     6
     7from __future__ import annotations
     8
     9import typing
    10from typing import NamedTuple
    11
    12# Testing the new Python 3.12 `type` statement.
    13type MyType = int
    14"""A custom Python 3.12 type."""
    15
    16foo: MyType
    17"""A custom type instance."""
    18
    19
    20type MyTypeWithoutDocstring = int
    21
    22MyTypeClassic: typing.TypeAlias = int
    23"""A "classic" typing.TypeAlias."""
    24
    25# Testing a typing.NamedTuple
    26# we do not care very much about collections.namedtuple,
    27# the typing version is superior for documentation and a drop-in replacement.
    28
    29
    30class NamedTupleExample(NamedTuple):
    31    """
    32    An example for a typing.NamedTuple.
    33    """
    34
    35    name: str
    36    """Name of our example tuple."""
    37    id: int = 3

type MyType = int

A custom Python 3.12 type.

foo: MyType

A custom type instance.

type MyTypeWithoutDocstring = int
MyTypeClassic: TypeAlias = int

A "classic" typing.TypeAlias.

class NamedTupleExample(typing.NamedTuple): View Source

    31class NamedTupleExample(NamedTuple):
    32    """
    33    An example for a typing.NamedTuple.
    34    """
    35
    36    name: str
    37    """Name of our example tuple."""
    38    id: int = 3

An example for a typing.NamedTuple.

NamedTupleExample(name: str, id: int = 3)

Create new instance of NamedTupleExample(name, id)

name: str

Name of our example tuple.

id: int

Alias for field number 1