binarylane / binarylane-cli

Command-line interface to BinaryLane API
MIT License
15 stars 3 forks source link

refactor: make scripts/generate.py perform required fixups for openapi-python-client lazy imports #40

Closed nats closed 1 year ago

nats commented 1 year ago

This PR will enable removal of the enable_lazy_imports changes from our openapi-python-client fork, by providing the required alterations to generated code in scripts/generate.py.

In v0.12.0 upstream openapi-python-client changed the generated code to use ForwardRef type annotations in various parts of the generated code. This was a problem for us because our formatter module needs to resolve those types at runtime as it walks the response object, so the solution we used at the time was to add a new enable_lazy_imports class attribute to Property and use its value to determine whether to change the behaviour of the Property hierarchy back to normal ('eager') imports.

While it would be possible to feed the relevant types to typing.get_type_hints() via its localns parameter; since we do not know ahead of time which particular types are required we would have to import all types from models, which is a pretty significant performance hit for CLI usage.

By performing the relevant fixups in generate.py, we avoid that while bringing us a step closer to switching back to the upstream openapi-python-client.

nats commented 1 year ago

How to test:

  1. Starting with main, run poetry run task generate and verify via git diff there are no changes.
  2. Run poetry env path to see where your venv is , in that directory edit lib/python3.10/site-packages/openapi_python_client/parser/properties/property.py find the line enable_lazy_imports = False and change it to True.
  3. run poetry run task generate and verify via git diff that the generated lib/ now contains forward references.
  4. Change enable_lazy_imports to False, switch to this branch refactor/eager-imports, run poetry run task generate and verify git diff there are no changes.
  5. Change enable_lazy_imports to True, run poetry run task generate and verify via git diff there are no changes.