typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Upstream ujson type stubs to ujson

Open MarcoGorelli opened this issue 8 months ago • 3 comments

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! https://github.com/ultrajson/ultrajson/issues/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

MarcoGorelli avatar Jun 10 '25 08:06 MarcoGorelli

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

Avasam avatar Jun 11 '25 06:06 Avasam

looks like this got merged 🥳 https://github.com/ultrajson/ultrajson/pull/674

is the process to wait until a ujson release, and then the stubs can presumably be removed from typeshed?

MarcoGorelli avatar Jun 12 '25 19:06 MarcoGorelli

Pretty much - see Third-party library removal policy for the full process

brianschubert avatar Jun 12 '25 19:06 brianschubert

Today I learned that "ujson" is "UltraJSON", not "MicroJSON". Anyway, I think this issue is complete. stubsabot will notify us, once a new version with type stubs gets released.

srittau avatar Jul 07 '25 14:07 srittau

ujson v5.11.0 was just released with type hints 🚀 https://github.com/ultrajson/ultrajson/releases/tag/5.11.0

Thanks @MarcoGorelli for leading this effort!

Automation should take it from here: stubsabot should open a PR tonight to mark the stubs as obsolete, then another in 6 months to remove them.

brianschubert avatar Aug 20 '25 13:08 brianschubert