-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/finishing typeddict inputs (#95)
Why === We got pretty close to having TypedDicts for river-python inputs before, but had to roll back due to a protocol mismatch. Trying again, and also adding some tests to confirm that at the very least the Pydantic models can decode was was encoded by the TypedDict encoders. It's not a perfect science, but it should be good enough to start building more confidence as we make additional progress. ### The reason for "janky" tests There's a bit of a chicken-and-egg situation when trying to test code generation at runtime. We have three options: - write pytest handlers where each invocation runs the codegen with a temp target (like the shell script does here), writes a static file for each text into that directory, then executes a new python into that directory. The challenge with this is that it would suck to write or maintain. - write pytest handlers which runs the codegen with unique module name targets (like `gen1`, `gen2`, `gen3`, one for each codegen run necessary) and carefully juggle the imports to make sure we don't try to import something that's not there yet. This _might_ be the best option, but I'm not convinced about the ergonomics at the moment. It might be OK though, with highly targeted `.gitignore`'s. - maintain a bespoke test runner, optimize for writing and maintaining these tests, and just acknowledge that we are doing something obscure and difficult. I definitely wrote the tests here in a way that would give some coverage and also provide confidence, while intentionally deferring the above decision so we can keep making progress. in the meantime. What changed ============ - Added some janky tests for comparing the encoding of both models - Fixed many bugs in the TypedDict codegen and encoders Test plan ========= ``` $ bash scripts/parity.sh Using /tmp/river-codegen-parity.bAZ Starting... Verified ```
- Loading branch information
1 parent
62b236e
commit ca9d552
Showing
5 changed files
with
326 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# parity.sh: Generate Pydantic and TypedDict models and check for deep equality. | ||
# This script expects that ai-infra is cloned alongside river-python. | ||
|
||
set -e | ||
|
||
scripts="$(dirname "$0")" | ||
cd "${scripts}/.." | ||
|
||
root="$(mktemp -d --tmpdir 'river-codegen-parity.XXX')" | ||
mkdir "$root/src" | ||
|
||
echo "Using $root" >&2 | ||
|
||
function cleanup { | ||
if [ -z "${DEBUG}" ]; then | ||
echo "Cleaning up..." >&2 | ||
rm -rfv "${root}" >&2 | ||
fi | ||
} | ||
trap "cleanup" 0 2 3 15 | ||
|
||
gen() { | ||
fname="$1"; shift | ||
name="$1"; shift | ||
poetry run python -m replit_river.codegen \ | ||
client \ | ||
--output "${root}/src/${fname}" \ | ||
--client-name "${name}" \ | ||
../ai-infra/pkgs/pid2_client/src/schema/schema.json \ | ||
"$@" | ||
} | ||
|
||
gen tyd.py Pid2TypedDict --typed-dict-inputs | ||
gen pyd.py Pid2Pydantic | ||
|
||
PYTHONPATH="${root}/src:${scripts}" | ||
poetry run bash -c "MYPYPATH='$PYTHONPATH' mypy -m parity.check_parity" | ||
poetry run bash -c "PYTHONPATH='$PYTHONPATH' python -m parity.check_parity" |
Oops, something went wrong.