Skip to content

Add jsonnet stubs #14253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add jsonnet stubs #14253

wants to merge 2 commits into from

Conversation

Tatsh
Copy link
Contributor

@Tatsh Tatsh commented Jun 10, 2025

Python API

_jsonnet.c

I used discrete types rather than abstract. Tuple is strictly required but I do not know if PyDict_* API can accept Mapping. I assume no so it's left as dict here. Same for list vs Collection when using the PyList_* API.

This comment has been minimized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be changed into a single top-level stub file: _jsonnet/__init__.pyi -> _jsonnet.pyi

Comment on lines 13 to 19
ext_vars: dict[str, str] | None = None,
ext_codes: dict[str, str] | None = None,
tla_var: dict[str, str] | None = None,
tla_codes: dict[str, str] | None = None,
max_trace: int = 20,
import_callback: Callable[[str, str], tuple[str, object | None]] | None = None,
native_callbacks: dict[str, tuple[tuple[str, ...], Callable[..., object]]] | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like None isn't a valid argument for at least some of these. To indicate that a parameter is optional without specifying a default value, you can use parameter: annotation = ...

Copy link
Contributor Author

@Tatsh Tatsh Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, import_callback and the numeric args cannot be none.

For jpathdir, it gets type validated by calling Py(Unicode|String|List)_Check(), so passing None does work and is the same as not passing anything (in C the type is Py_None vs NULL respectively).

The dict args get passed to PyDict_Next which returns 0 immediately if the op argument is not of type dict.

This works without any surprises:

>>> _jsonnet.evaluate_snippet('', '{}', ext_vars=None, ext_codes=None, tla_vars=None, tla_codes=None, jpathdir=None, native_callbacks=None)
'{ }\n'

Values that are supposed to be callables are always checked:

>>> _jsonnet.evaluate_snippet('', '{}', ext_codes=None, tla_vars=None, native_callbacks={'s': ((), None)})
Traceback (most recent call last):
  File "<python-input-17>", line 1, in <module>
    _jsonnet.evaluate_snippet('', '{}', ext_codes=None, tla_vars=None, native_callbacks={'s': ((), None)})
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: native callback must be callable

>>> _jsonnet.evaluate_snippet('', '{}', ext_codes=None, tla_vars=None, import_callback=None)
Traceback (most recent call last):
  File "<python-input-14>", line 1, in <module>
    _jsonnet.evaluate_snippet('', '{}', ext_codes=None, tla_vars=None, import_callback=None)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: import_callback must be callable

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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.

2 participants