misc_py312

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

 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
11from typing import Optional
12from typing import TypedDict
13
14# Testing the new Python 3.12 `type` statement.
15type MyType = int
16"""A custom Python 3.12 type."""
17
18foo: MyType
19"""A custom type instance."""
20
21
22type MyTypeWithoutDocstring = int
23
24MyTypeClassic: typing.TypeAlias = int
25"""A "classic" typing.TypeAlias."""
26
27# Testing a typing.NamedTuple
28# we do not care very much about collections.namedtuple,
29# the typing version is superior for documentation and a drop-in replacement.
30
31
32class NamedTupleExample(NamedTuple):
33    """
34    An example for a typing.NamedTuple.
35    """
36
37    name: str
38    """Name of our example tuple."""
39    id: int = 3
40
41
42# Testing some edge cases in our inlined implementation of ForwardRef._evaluate in _eval_type.
43class Foo(TypedDict):
44    a: Optional[int]
45    """First attribute."""
46
47
48class Bar(Foo, total=False):
49    """A TypedDict subclass. Before 3.12, TypedDict botches __mro__."""
50
51    b: int
52    """Second attribute."""
53    c: str
54    # undocumented attribute
55
56
57class Baz(Bar):
58    """A TypedDict subsubclass."""
59
60    d: bool
61    """new attribute"""
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):
33class NamedTupleExample(NamedTuple):
34    """
35    An example for a typing.NamedTuple.
36    """
37
38    name: str
39    """Name of our example tuple."""
40    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

Inherited Members
builtins.tuple
index
count
class Foo(typing.TypedDict):
44class Foo(TypedDict):
45    a: Optional[int]
46    """First attribute."""
a: Optional[int]

First attribute.

Inherited Members
builtins.dict
get
setdefault
pop
popitem
keys
items
values
update
fromkeys
clear
copy
class Bar(Foo):
49class Bar(Foo, total=False):
50    """A TypedDict subclass. Before 3.12, TypedDict botches __mro__."""
51
52    b: int
53    """Second attribute."""
54    c: str
55    # undocumented attribute

A TypedDict subclass. Before 3.12, TypedDict botches __mro__.

b: int

Second attribute.

c: str
Inherited Members
Foo
a
builtins.dict
get
setdefault
pop
popitem
keys
items
values
update
fromkeys
clear
copy
class Baz(Bar):
58class Baz(Bar):
59    """A TypedDict subsubclass."""
60
61    d: bool
62    """new attribute"""

A TypedDict subsubclass.

d: bool

new attribute

Inherited Members
Bar
b
c
Foo
a
builtins.dict
get
setdefault
pop
popitem
keys
items
values
update
fromkeys
clear
copy