terrasnek icon indicating copy to clipboard operation
terrasnek copied to clipboard

return objects and types?

Open evilensky opened this issue 2 years ago • 3 comments

Hello. I wasn't sure where to ask so please do close this issue if it does not belong here.

Is it a project goal to at some point offer more "client side" support for validating input, and/or providing type hinted return objects?

evilensky avatar Nov 10 '22 14:11 evilensky

There are no plans to do either, currently. Do you have a specific example of inputs that you would like to see validated?

dahlke avatar Nov 11 '22 17:11 dahlke

All of them :)

I'm honestly not sure about a specific approach but I tried this with TypedDict and it is promising.

When everything is filled out "correctly" per the payload description at workspace vars api:

▶ mypy run_script.py
Success: no issues found in 1 source file

When the dict is not well formed:

run_script.py:17: error: Missing key "category" for TypedDict "PayloadDataAttributes"  [typeddict-item]
Found 1 error in 1 file (checked 1 source file)

Pyright in VSCode is also useful:

image

But I'm new to this and not sure if this will scale, or how to keep it in sync with TFC api changes. Something like this would be very powerful for ensuring payload correctness, but the goal would be to gain IDE completions as well on par with what go-tfe can provide :). I also have tried dataclasses

evilensky avatar Nov 19 '22 15:11 evilensky

Hi @dahlke . I've bundled a few misc fixes -- which were uncovered with the help of better type hinting -- as well as unit tests for workspaces.py and some structural type stubs in this branch

This results in no-error type hints in VSCode, PyCharm, as well as running mypy on mypy unit_test/test_workspaces.py --check-untyped-defs.

If this makes sense I'd like to continue typing out other endpoints.

Separately but intimately related is to create type definitions for payload objects for each endpoint. I will try class properties for type definitions in place of TypedDict next, it is significantly more flexible, I believe.

evilensky avatar Nov 21 '22 13:11 evilensky