nanobind icon indicating copy to clipboard operation
nanobind copied to clipboard

Enum generation sphinx parsing error

Open jdumas opened this issue 1 year ago • 3 comments

Hi,

Since #195, I am having issue generating Sphinx documentation with nanobind-stubgen + sphinx-autoapi. As an example, the following code:

     nb::enum_<NormalWeightingType>(m, "NormalWeightingType")
        .value("Uniform", NormalWeightingType::Uniform)
        .value("CornerTriangleArea", NormalWeightingType::CornerTriangleArea)
        .value("Angle", NormalWeightingType::Angle);

Now produces the following .pyi stub code after running it through nanobind-stubgen:

class NormalWeightingType:
   """
   <attribute '__doc__' of 'NormalWeightingType' objects>
   """

   @entries: dict

   Angle: NormalWeightingType

   CornerTriangleArea: NormalWeightingType

   Uniform: NormalWeightingType

   def __init__(*args, **kwargs):
       """
       Initialize self.  See help(type(self)) for accurate signature.
       """
       ...

Which fails when being parsed by sphinx-autoapi:

[AutoAPI] Reading files... [ 40%] /opt/homebrew/Caskroom/miniconda/base/envs/lagrange-ci-python/lib/python3.11/site-packages/lagrange/lagrange/core.pyi
Extension error (autoapi.extension):
Handler <function run_autoapi at 0x109484ea0> for event 'builder-inited' threw an exception (exception: Parsing Python code failed:
invalid syntax (<unknown>, line 269))

Now I am not sure whether this is an issue with nanobind, nanobind-stubgen, or sphinx-autoapi. But since PR #195 introduced the breaking change for me, I'm posting an issue here. I cannot find any documentation on this @entries: dict decorator, so I'm not sure what it is supposed to achieve, or why it is not parsed correctly by Sphinx. Any insight would be appreciated, thanks!

jdumas avatar Dec 09 '23 17:12 jdumas

cc @oremanj, who authored these changes.

wjakob avatar Dec 09 '23 21:12 wjakob

@entries is not a decorator here; it's an attribute with an unusual name (so that it can't conflict with any valid identifier, which might be an enumerator of the enum). It is nanobind-specific, so the appropriate place to handle it is in nanobind-stubgen; the correct behavior would be to simply ignore it and not include it in the generated stub file.

oremanj avatar Dec 15 '23 07:12 oremanj

Alright I gave a shot at fixing the nanobind-stubgen code. PR is in https://github.com/cansik/nanobind-stubgen/pull/9 if you guys want to take a look.

jdumas avatar Dec 15 '23 18:12 jdumas

Closing this issue as the nanobind-stubgen PR was merged.

wjakob avatar Feb 01 '24 13:02 wjakob