oconnor663 / blake3-py

Python bindings for the BLAKE3 cryptographic hash function
Other
148 stars 14 forks source link

Parent module of blake3.blake3.blake3 is builtins #38

Closed pawamoy closed 1 year ago

pawamoy commented 1 year ago

I guess it comes from your build system (maturin IIUC?), but blake3.blake3.blake3 (or the shorter blake3.blake3) says its parent module is builtins:

>>> from blake3 import blake3
>>> blake3.__module__
'builtins'

It's problematic because there's no blake3 object importable from builtins. It makes it impossible to inspect the object without special-casing it. For reference: https://github.com/mkdocstrings/mkdocstrings/issues/451#issuecomment-1353709610

Do you know if there's a way to compile the project so that its objects correctly report their parent modules, for example blake3.blake3 (and not builtins)?

oconnor663 commented 1 year ago

This is my first time looking at the __module__ attribute, so I don't have any good ideas off the top of my head. It looks like the pure C implementation has the same property, though, so maybe this is just a native module thing in general? You can test it like this:

$ python3 -m venv /tmp/tempenv
$ source /tmp/tempenv/bin/activate
(tempenv) $ cd blake3-py/c_impl
(tempenv) $ pip install .
...
(tempenv) $ python3
Python 3.10.8 (main, Nov  1 2022, 14:18:21) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from blake3 import blake3
>>> blake3.__module__
'builtins'

What are some other pure-C or pure-Rust modules we could look at for comparison?

pawamoy commented 1 year ago

Another project using maturin gives the same result: https://github.com/samuelcolvin/watchfiles.

$ python -m venv .venv
$ . .venv/bin/activate
$ pip install watchfiles
$ python -c 'from watchfiles.main import RustNotify; print(RustNotify.__module__)'
builtins
HacKanCuBa commented 1 year ago

what about Pydantic core?

$ python -c 'from pydantic_core import SchemaValidator; print(SchemaValidator.__module__)'
pydantic_core._pydantic_core

We could ask @samuelcolvin 👋

samuelcolvin commented 1 year ago

You just need to set module on the pyclass, see here.

oconnor663 commented 1 year ago

Thanks @samuelcolvin!

samuelcolvin commented 1 year ago

No problem 👍.

oconnor663 commented 1 year ago

Released as version 0.3.2. (After quite a bit of wrangling with an unreleated CI break...)

HacKanCuBa commented 1 year ago

Wow, this was fast!! I'm gonna update and test mkdocstrings. Thanks a lot everyone!

pawamoy commented 1 year ago

Yup, thanks for the quick release!!