Skip to content

fix(core): keep default_factory defaults in the generated Python signatures - #7856

Open
b84330808 wants to merge 1 commit into
openbq-org:developfrom
b84330808:bugfix/default-factory-signature-defaults
Open

b84330808 wants to merge 1 commit into
openbq-org:developfrom
b84330808:bugfix/default-factory-signature-defaults

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Pull Request the OpenBB Platform

Description

  • Summary of the change/ bug fix.

Four commands fail from the Python interface when called the way their examples and docstrings show:

obb.technical.realized_volatility_compare(data=data)
# OpenBBError: [Arg] models -> input: ... -> Missing required argument

obb.quantitative.attribution(data=returns, factors_data=factors, target="close")
# OpenBBError: [Arg] periods -> input: ... -> Missing required argument

quantitative.factors and quantitative.risk_decomposition fail the same way on periods. The same calls work through the REST API (POST /api/v1/technical/realized_volatility_compare with only data returns 200).

These four are the only command parameters in the repository declared with default_factory instead of default. When the package builder flattens a QueryParams model into the arguments of the generated method, it only reads field.default, which is PydanticUndefined for a default_factory field, so the argument is generated without a default. The flattening branch for other QueryParams annotations (the "Case 3" block in format_params) already handles default_factory; the plain QueryParams branch and the GET-model branch in _format_annotated_param don't.

This moves that logic into MethodDefinition.get_model_field_default and uses it in all three places. The generated signature now reads models: ... = ['std', 'parkinson', 'garman_klass', 'hodges_tompkins', 'rogers_satchell', 'yang_zhang'], and reference.json lists periods as optional with its default instead of "optional": false, "default": null.

Because periods is no longer required, it now comes after the other required arguments in the generated attribution, factors and risk_decomposition signatures.

How has this been tested?

  • Two new tests in test_method_definition_helpers.py (a plain QueryParams model and a GET pydantic model, each with a default_factory field). Both fail on develop (assert <class 'inspect._empty'> == ['std', 'parkinson']) and pass with the change.
  • The existing extensions/technical/integration/test_technical_python.py::test_realized_volatility_compare (it calls the command without models) fails on develop with the error above for both the daily and intraday fixture, and passes on this branch. The whole file: 136 passed. quantitative/integration/test_quantitative_python.py: 18 passed; test_quantitative_capm fails because mba.tuck.dartmouth.edu answered 404 for every page when I ran it, which this change doesn't touch.
  • Rebuilt the static package with openbb.build() on develop and on this branch and ran the four calls above: all four raise "Missing required argument" on develop and return results on this branch (770 / 24 / 18 / 18 rows on generated sample data).
  • openbb_platform/core/tests/app: 1293 passed, 49 skipped. test_credentials_helpers.py::test_load_picks_up_env_credentials fails in a full local run on both develop and this branch and passes when run alone.
  • ruff format --check and ruff check pass for the changed files; ty check reports the same diagnostics as on develop.

Checklist

  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have adhered to the GitFlow naming convention and my branch name is in the format of feature/feature-name or hotfix/hotfix-name.
  • I ensure that I am following the CONTRIBUTING guidelines.

…atures

The package builder only read `field.default` when it flattened a
QueryParams model into the arguments of a generated `obb` method, so a
field declared with `default_factory` became a required argument.
`obb.technical.realized_volatility_compare(data=...)` and
`obb.quantitative.attribution/factors/risk_decomposition(...)` failed with
"Missing required argument" for `models` / `periods`, although the REST API
and the docstrings treat them as optional.

Resolve the default the same way the flattening branch for other
QueryParams annotations already did, and use that helper in all three
places.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant