From 14ced05536bcff7dd73a51f4a34b30836642cbc2 Mon Sep 17 00:00:00 2001 From: Miorel-Lucian Palii Date: Mon, 8 Jul 2024 23:39:56 -0700 Subject: [PATCH] Slight improvement to Python typing We also get ready to differentiate the goody name from the module name! --- .../{int.digits => src/int_digits}/__init__.py | 7 +++++-- .../python3/{int.digits => src/int_digits}/test.py | 11 ++--------- .../app/__tests__/__snapshots__/equip-test.ts.snap | 9 ++++++--- .../app/__tests__/__snapshots__/render-test.ts.snap | 9 ++++++--- .../scripts/package-goodies/python3/readBaseGoody.ts | 10 ++++++---- .../scripts/package-goodies/python3/readGoodies.ts | 10 ++++++---- 6 files changed, 31 insertions(+), 25 deletions(-) rename tools/adventure-pack/goodies/python3/{int.digits => src/int_digits}/__init__.py (75%) rename tools/adventure-pack/goodies/python3/{int.digits => src/int_digits}/test.py (78%) diff --git a/tools/adventure-pack/goodies/python3/int.digits/__init__.py b/tools/adventure-pack/goodies/python3/src/int_digits/__init__.py similarity index 75% rename from tools/adventure-pack/goodies/python3/int.digits/__init__.py rename to tools/adventure-pack/goodies/python3/src/int_digits/__init__.py index 132f8d85..8b1af821 100644 --- a/tools/adventure-pack/goodies/python3/int.digits/__init__.py +++ b/tools/adventure-pack/goodies/python3/src/int_digits/__init__.py @@ -1,5 +1,8 @@ -def set_up_adventure_pack(): - def digits(self: int, radix: int = 10): +from typing import Generator + + +def set_up_adventure_pack() -> None: + def digits(self: int, radix: int = 10) -> Generator[int, None, None]: if self < 0: raise ValueError("Must invoke on a non-negative integer.") diff --git a/tools/adventure-pack/goodies/python3/int.digits/test.py b/tools/adventure-pack/goodies/python3/src/int_digits/test.py similarity index 78% rename from tools/adventure-pack/goodies/python3/int.digits/test.py rename to tools/adventure-pack/goodies/python3/src/int_digits/test.py index f75e57fc..470c85b6 100644 --- a/tools/adventure-pack/goodies/python3/int.digits/test.py +++ b/tools/adventure-pack/goodies/python3/src/int_digits/test.py @@ -1,11 +1,4 @@ -import os - -with open(os.path.join(os.path.dirname(__file__), "__init__.py"), "r") as f: - module_code = f.read() -exec(module_code) - - -def test_base_10(): +def test_base_10() -> None: assert list((123).digits(10)) == [3, 2, 1] assert list((1337).digits(10)) == [7, 3, 3, 1] assert list((42).digits(10)) == [2, 4] @@ -29,7 +22,7 @@ def test_base_10(): assert list((5).digits(10)) == [5] -def test_base_2(): +def test_base_2() -> None: assert list((12).digits(2)) == [0, 0, 1, 1] assert list((2**10 - 1).digits(2)) == [1] * 10 assert list((2**30).digits(2)) == ([0] * 30 + [1]) diff --git a/tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap b/tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap index eb00a221..012c56bb 100644 --- a/tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap +++ b/tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap @@ -867,13 +867,16 @@ fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b /////////////////////////// END ADVENTURE PACK CODE ////////////////////////////" `; -exports[`App can equip single goody: Python 3 int.digits 1`] = ` +exports[`App can equip single goody: Python 3 int_digits 1`] = ` "########################## BEGIN ADVENTURE PACK CODE ########################### # Adventure Pack commit fake-commit-hash # Running at: https://example.com/ -def set_up_adventure_pack(): - def digits(self: int, radix: int = 10): +from typing import Generator + + +def set_up_adventure_pack() -> None: + def digits(self: int, radix: int = 10) -> Generator[int, None, None]: if self < 0: raise ValueError("Must invoke on a non-negative integer.") diff --git a/tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap b/tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap index 3c8848b1..e672efcb 100644 --- a/tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap +++ b/tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap @@ -530,9 +530,12 @@ import gcd_int_int.gcd fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b" `; -exports[`App can render goody: Python 3 int.digits 1`] = ` -"def set_up_adventure_pack(): - def digits(self: int, radix: int = 10): +exports[`App can render goody: Python 3 int_digits 1`] = ` +"from typing import Generator + + +def set_up_adventure_pack() -> None: + def digits(self: int, radix: int = 10) -> Generator[int, None, None]: if self < 0: raise ValueError("Must invoke on a non-negative integer.") diff --git a/tools/adventure-pack/src/scripts/package-goodies/python3/readBaseGoody.ts b/tools/adventure-pack/src/scripts/package-goodies/python3/readBaseGoody.ts index f032df28..25efb0b2 100644 --- a/tools/adventure-pack/src/scripts/package-goodies/python3/readBaseGoody.ts +++ b/tools/adventure-pack/src/scripts/package-goodies/python3/readBaseGoody.ts @@ -4,20 +4,22 @@ import { WritableDeep } from "type-fest"; import type { Python3Goody } from "../../../app/parsers/python3GoodyParser"; -export const GOODIES_DIRECTORY = path.join("goodies", "python3"); +export const GOODIES_DIRECTORY = path.join("goodies", "python3", "src"); export type Python3GoodyBase = Omit, "importedBy">; -export async function readBaseGoody(name: string): Promise { +export async function readBaseGoody( + moduleName: string, +): Promise { const code = await fsPromises.readFile( - path.join(GOODIES_DIRECTORY, name, "__init__.py"), + path.join(GOODIES_DIRECTORY, moduleName, "__init__.py"), "utf8", ); return { code, imports: [], - name, language: "python3", + name: moduleName, }; } diff --git a/tools/adventure-pack/src/scripts/package-goodies/python3/readGoodies.ts b/tools/adventure-pack/src/scripts/package-goodies/python3/readGoodies.ts index 0531f2bd..371cf4ab 100644 --- a/tools/adventure-pack/src/scripts/package-goodies/python3/readGoodies.ts +++ b/tools/adventure-pack/src/scripts/package-goodies/python3/readGoodies.ts @@ -18,12 +18,14 @@ export async function readGoodies(): Promise> { const baseGoodiesByName: Record = {}; for (const entry of fileEntries) { - if (!entry.isDirectory() || entry.name.startsWith(".")) { - continue; - } + const moduleName = entry.name; + invariant( + entry.isDirectory(), + `Found non-module ${JSON.stringify(moduleName)} in Python 3 goodies directory!`, + ); // eslint-disable-next-line no-await-in-loop - const baseGoody = await readBaseGoody(entry.name); + const baseGoody = await readBaseGoody(moduleName); invariant(baseGoody.name === entry.name, "Mismatched goody name!"); setIfNotHasOwnOrThrow(baseGoodiesByName, baseGoody.name, baseGoody); }