Skip to content

Commit 6da5e9b

Browse files
authored
Slight improvement to Python typing (#153)
We also get ready to differentiate the goody name from the module name!
1 parent a74d148 commit 6da5e9b

File tree

6 files changed

+31
-25
lines changed

6 files changed

+31
-25
lines changed

tools/adventure-pack/goodies/python3/int.digits/__init__.py renamed to tools/adventure-pack/goodies/python3/src/int_digits/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
def set_up_adventure_pack():
2-
def digits(self: int, radix: int = 10):
1+
from typing import Generator
2+
3+
4+
def set_up_adventure_pack() -> None:
5+
def digits(self: int, radix: int = 10) -> Generator[int, None, None]:
36
if self < 0:
47
raise ValueError("Must invoke on a non-negative integer.")
58

tools/adventure-pack/goodies/python3/int.digits/test.py renamed to tools/adventure-pack/goodies/python3/src/int_digits/test.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import os
2-
3-
with open(os.path.join(os.path.dirname(__file__), "__init__.py"), "r") as f:
4-
module_code = f.read()
5-
exec(module_code)
6-
7-
8-
def test_base_10():
1+
def test_base_10() -> None:
92
assert list((123).digits(10)) == [3, 2, 1]
103
assert list((1337).digits(10)) == [7, 3, 3, 1]
114
assert list((42).digits(10)) == [2, 4]
@@ -29,7 +22,7 @@ def test_base_10():
2922
assert list((5).digits(10)) == [5]
3023

3124

32-
def test_base_2():
25+
def test_base_2() -> None:
3326
assert list((12).digits(2)) == [0, 0, 1, 1]
3427
assert list((2**10 - 1).digits(2)) == [1] * 10
3528
assert list((2**30).digits(2)) == ([0] * 30 + [1])

tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,13 +867,16 @@ fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b
867867
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
868868
`;
869869
870-
exports[`App can equip single goody: Python 3 int.digits 1`] = `
870+
exports[`App can equip single goody: Python 3 int_digits 1`] = `
871871
"########################## BEGIN ADVENTURE PACK CODE ###########################
872872
# Adventure Pack commit fake-commit-hash
873873
# Running at: https://example.com/
874874
875-
def set_up_adventure_pack():
876-
def digits(self: int, radix: int = 10):
875+
from typing import Generator
876+
877+
878+
def set_up_adventure_pack() -> None:
879+
def digits(self: int, radix: int = 10) -> Generator[int, None, None]:
877880
if self < 0:
878881
raise ValueError("Must invoke on a non-negative integer.")
879882

tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,12 @@ import gcd_int_int.gcd
530530
fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b"
531531
`;
532532
533-
exports[`App can render goody: Python 3 int.digits 1`] = `
534-
"def set_up_adventure_pack():
535-
def digits(self: int, radix: int = 10):
533+
exports[`App can render goody: Python 3 int_digits 1`] = `
534+
"from typing import Generator
535+
536+
537+
def set_up_adventure_pack() -> None:
538+
def digits(self: int, radix: int = 10) -> Generator[int, None, None]:
536539
if self < 0:
537540
raise ValueError("Must invoke on a non-negative integer.")
538541

tools/adventure-pack/src/scripts/package-goodies/python3/readBaseGoody.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ import { WritableDeep } from "type-fest";
44

55
import type { Python3Goody } from "../../../app/parsers/python3GoodyParser";
66

7-
export const GOODIES_DIRECTORY = path.join("goodies", "python3");
7+
export const GOODIES_DIRECTORY = path.join("goodies", "python3", "src");
88

99
export type Python3GoodyBase = Omit<WritableDeep<Python3Goody>, "importedBy">;
1010

11-
export async function readBaseGoody(name: string): Promise<Python3GoodyBase> {
11+
export async function readBaseGoody(
12+
moduleName: string,
13+
): Promise<Python3GoodyBase> {
1214
const code = await fsPromises.readFile(
13-
path.join(GOODIES_DIRECTORY, name, "__init__.py"),
15+
path.join(GOODIES_DIRECTORY, moduleName, "__init__.py"),
1416
"utf8",
1517
);
1618

1719
return {
1820
code,
1921
imports: [],
20-
name,
2122
language: "python3",
23+
name: moduleName,
2224
};
2325
}

tools/adventure-pack/src/scripts/package-goodies/python3/readGoodies.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ export async function readGoodies(): Promise<Record<string, Python3Goody>> {
1818
const baseGoodiesByName: Record<string, Python3GoodyBase> = {};
1919

2020
for (const entry of fileEntries) {
21-
if (!entry.isDirectory() || entry.name.startsWith(".")) {
22-
continue;
23-
}
21+
const moduleName = entry.name;
22+
invariant(
23+
entry.isDirectory(),
24+
`Found non-module ${JSON.stringify(moduleName)} in Python 3 goodies directory!`,
25+
);
2426

2527
// eslint-disable-next-line no-await-in-loop
26-
const baseGoody = await readBaseGoody(entry.name);
28+
const baseGoody = await readBaseGoody(moduleName);
2729
invariant(baseGoody.name === entry.name, "Mismatched goody name!");
2830
setIfNotHasOwnOrThrow(baseGoodiesByName, baseGoody.name, baseGoody);
2931
}

0 commit comments

Comments
 (0)