Skip to content

Upstream ujson type stubs to ujson #14257

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
MarcoGorelli opened this issue Jun 10, 2025 · 1 comment
Open

Upstream ujson type stubs to ujson #14257

MarcoGorelli opened this issue Jun 10, 2025 · 1 comment

Comments

@MarcoGorelli
Copy link

MarcoGorelli commented Jun 10, 2025

typeshed currently hosts stubs for ujson: https://github.com/python/typeshed/tree/main/stubs/ujson

I asked ujson if they'd be happy to inline the stubs, and it looks like they would! ultrajson/ultrajson#671

I've given this a go here: https://github.com/MarcoGorelli/ultrajson/tree/inline-stubs

Could I please ask that someone help me out with where to place the py.typed file? I'm aware of where to place it in a Python project, but in ujson it's all C extensions

I've tried to just put it in the root, but then, in a separate virtual environment, if I do

$ uv pip uninstall ujson
Uninstalled 1 package in 12ms
 - ujson==5.10.0
$ uv pip install git+https://github.com/MarcoGorelli/ultrajson.git --no-cache
    Updated https://github.com/MarcoGorelli/ultrajson.git (2ea54f6a1d1b8e2bc3a27c491a63238cf244f087)
Resolved 1 package in 5.34s
      Built ujson @ git+https://github.com/MarcoGorelli/ultrajson.git@2ea54f6a1d1b8e2bc3a27c491a63238cf244f087
Prepared 1 package in 7.42s
Installed 1 package in 3ms
 + ujson==0.1.dev969 (from git+https://github.com/MarcoGorelli/ultrajson.git@2ea54f6a1d1b8e2bc3a27c491a63238cf244f087)

and then make a file t.py with

import ujson
ujson.dumps([{"key": "value"}, 81, True], ensure_ascii = 42)
ujson.loads("""[{"key": "value"}, 81, true]""")

and run mypy t.py, then I see

$ mypy t.py
t.py:1: error: Library stubs not installed for "ujson"  [import-untyped]
t.py:1: note: Hint: "python3 -m pip install types-ujson"
t.py:1: note: (or run "mypy --install-types" to install all missing stub packages)
t.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

Any help towards this would be appreciated 🙏 Ultimately, the benefit to typeshed would be fewer stubs to maintain here

@Avasam
Copy link
Collaborator

Avasam commented Jun 11, 2025

A py.typed file has to be shipped as part of a package.

So it would be ujson/__init__.pyi + ujson/py.typed. Not ujson.pyi. If ujson doesn't ship as a package, this would require changes to the distribution to make it so.

Alternatively: ujson-stubs/__init__.pyi will automatically be treated as a stub for a ujson module. No need for a py.typed. It's what typeshed does.

See https://typing.python.org/en/latest/spec/distributing.html#packaging-type-information

Also note that setuptools doesn't include type information (py.typed and .pyi) by default before v69: https://setuptools.pypa.io/en/latest/history.html#v69-0-0

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

No branches or pull requests

2 participants