8000 Use new Python 3.12 type alias syntax · mjpieters/adventofcode@f9bf213 · GitHub
[go: up one dir, main page]

Skip to content

Commit f9bf213

Browse files
committed
Use new Python 3.12 type alias syntax
1 parent e5ec0a9 commit f9bf213

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

2023/Day 08.ipynb

Lines changed: 9 additions & 3 deletions
< 8000 td data-grid-cell-id="diff-6f878abd41fe5eec55bbcbb56102b9a774afb850c8401aaa943fc6751e8ea92e-128-128-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">128
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"from dataclasses import dataclass\n",
2222
"from itertools import cycle\n",
2323
"\n",
24-
"Step: t.TypeAlias = t.Literal[\"L\", \"R\"]\n",
24+
"type Step = t.Literal[\"L\", \"R\"]\n",
2525
"\n",
2626
"\n",
2727
"class DesertNode(t.NamedTuple):\n",
@@ -127,10 +127,16 @@
127127
"source": [
128
"from math import lcm\n",
129129
"\n",
130+
"# help type checkers understand that the iterable passed to `all()` won't contain None\n",
131+
"# values\n",
132+
"_T = t.TypeVar(\"_T\")\n",
133+
"_AllNotNone = t.Callable[[t.Iterable[_T | None]], t.TypeGuard[t.Iterable[_T]]]\n",
134+
"_all_non_zero_int: _AllNotNone[t.Any] = t.cast(_AllNotNone[t.Any], all)\n",
135+
"\n",
130136
"\n",
131137
"class GhostlyDesertMap(DesertMap):\n",
132138
" @property\n",
133-
" def starting_points(self) -> tuple[DesertMap, ...]:\n",
139+
" def starting_points(self) -> tuple[DesertNode, ...]:\n",
134140
" return tuple(node for node in self.nodes.values() if node.name[-1] == \"A\")\n",
135141
"\n",
136142
" def from_as_to_zs(self, steps: t.Iterable[Step]) -> int:\n",
@@ -141,7 +147,7 @@
141147
" for i, node in enumerate(nodes):\n",
142148
" if not loops[i] and node.name[-1] == \"Z\":\n",
143149
" loops[i] = s\n",
144-
" if all(loops):\n",
150+
" if _all_non_zero_int(loops):\n",
145151
" return lcm(*loops)\n",
146152
"\n",
147153
" raise AssertionError(\"Goal never reached\")\n",

2023/Day 10.ipynb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
"from enum import Enum\n",
2828
"from itertools import count\n",
2929
"\n",
30-
"PipeChar: t.TypeAlias = t.Literal[\"S\", \"|\", \"-\", \"L\", \"J\", \"7\", \"F\"]\n",
31-
"MapChar: t.TypeAlias = t.Literal[\".\"] | PipeChar\n",
32-
"Connections: t.TypeAlias = tuple[()] | tuple[PipeChar, PipeChar, PipeChar]\n",
33-
"PosAndChar: t.TypeAlias = tuple[complex, MapChar]\n",
30+
"type PipeChar = t.Literal[\"S\", \"|\", \"-\", \"L\", \"J\", \"7\", \"F\"]\n",
31+
"type MapChar = t.Literal[\".\"] | PipeChar\n",
32+
"type Connections = tuple[()] | tuple[PipeChar, PipeChar, PipeChar]\n",
33+
"type PosAndChar = tuple[complex, MapChar]\n",
3434
"_impassible = ()\n",
3535
"_n_conn = (\"|\", \"7\", \"F\", \"S\")\n",
3636
"_s_conn = (\"|\", \"L\", \"J\", \"S\")\n",
@@ -52,6 +52,8 @@
5252
" connected_to: tuple[Connections, Connections, Connections, Connections]\n",
5353
"\n",
5454
" else:\n",
55+
" # hide the extended new method from pyright, it'll otherwise\n",
56+
" # be confused about PipePiece(...) calls later on.\n",
5557
"\n",
5658
" def __new__(\n",
5759
" cls,\n",

0 commit comments

Comments
 (0)
0