fix Record and Layout typing
[nmigen-type-annotations.git] / nmigen / hdl / rec.pyi
1 import enum
2 from typing import List, Union, Tuple, Any, Dict, Optional, Generator
3 from .ast import Signal, Value
4
5 __all__ = ["Direction", "DIR_NONE", "DIR_FANOUT",
6 "DIR_FANIN", "Layout", "Record"]
7
8
9 class Direction(enum.Enum):
10 NONE = enum.auto()
11 FANOUT = enum.auto()
12 FANIN = enum.auto()
13
14
15 DIR_NONE = Direction.NONE
16 DIR_FANOUT = Direction.FANOUT
17 DIR_FANIN = Direction.FANIN
18
19 # recursive types are not yet supported by mypy, manually recurse a few times
20 LayoutInputFields0 = Union['Layout',
21 List[Union[Tuple[str,
22 Union[int,
23 Tuple[int, bool],
24 Any]],
25 Tuple[str,
26 Union[int,
27 Tuple[int, bool]],
28 Direction]]]]
29
30 LayoutInputFields1 = Union['Layout',
31 List[Union[Tuple[str,
32 Union[int,
33 Tuple[int, bool],
34 LayoutInputFields0]],
35 Tuple[str,
36 Union[int,
37 Tuple[int, bool]],
38 Direction]]]]
39
40 LayoutInputFields2 = Union['Layout',
41 List[Union[Tuple[str,
42 Union[int,
43 Tuple[int, bool],
44 LayoutInputFields1]],
45 Tuple[str,
46 Union[int,
47 Tuple[int, bool]],
48 Direction]]]]
49
50 LayoutInputFields3 = Union['Layout',
51 List[Union[Tuple[str,
52 Union[int,
53 Tuple[int, bool],
54 LayoutInputFields2]],
55 Tuple[str,
56 Union[int,
57 Tuple[int, bool]],
58 Direction]]]]
59
60 LayoutInputFields = Union['Layout',
61 List[Union[Tuple[str,
62 Union[int,
63 Tuple[int, bool],
64 LayoutInputFields3]],
65 Tuple[str,
66 Union[int,
67 Tuple[int, bool]],
68 Direction]]]]
69
70 LayoutFieldShape = Union[int, Tuple[int, bool], 'Layout']
71
72
73 class Layout:
74 fields: Dict[str, Tuple[LayoutFieldShape, Direction]]
75
76 @staticmethod
77 def wrap(obj: LayoutInputFields) -> 'Layout':
78 ...
79
80 def __init__(self, fields: LayoutInputFields) -> None:
81 ...
82
83 def __getitem__(self, name: str) -> Tuple[LayoutFieldShape, Direction]:
84 ...
85
86 def __iter__(self) -> Generator[Tuple[str, LayoutFieldShape, Direction],
87 None,
88 None]:
89 ...
90
91
92 class Record(Value):
93 layout: Layout
94 fields: Dict[str, Union[Record, Signal]]
95
96 def __init__(self, layout: LayoutInputFields, name: Optional[str] = None):
97 ...
98
99 def shape(self) -> Tuple[int, bool]:
100 ...
101
102 def __getattr__(self, name: str) -> Union[Record, Signal]:
103 ...
104
105 def __getitem__(self,
106 name: Union[slice, int, str]) -> Union[Record, Signal]:
107 ...