8000 stubgen throws AssertionError for QuantLib · Issue #19031 · python/mypy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

stubgen throws AssertionError for QuantLib #19031

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

Closed
sweemer opened this issue May 3, 2025 · 7 comments
Closed

stubgen throws AssertionError for QuantLib #19031

sweemer opened this issue May 3, 2025 · 7 comments

Comments

@sweemer
Copy link
sweemer commented May 3, 2025

Hello, I am getting the error below when trying to create baseline stubs for the QuantLib package.

QuantLib's python bindings are generated using SWIG. Is this supported? I tried googling for any known limitations with SWIG but couldn't find any.

I'm very new to typeshed so apologies if I'm doing something silly.

$ python3 ./scripts/create_baseline_stubs.py QuantLib
Using detected package "QuantLib" for project "QuantLib"
Suggestion: Try again with --package argument if that's not what you wanted
Running stubgen: stubgen -o stubs/QuantLib -p QuantLib
Traceback (most recent call last):
  File "/usr/local/py-utils/bin/stubgen", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "mypy/stubgen.py", line 1989, in main
  File "mypy/stubgen.py", line 1813, in generate_stubs
  File "/usr/local/py-utils/venvs/mypy/lib/python3.12/site-packages/mypy/stubgenc.py", line 179, in generate_stub_for_c_module
    gen.generate_module()
  File "/usr/local/py-utils/venvs/mypy/lib/python3.12/site-packages/mypy/stubgenc.py", line 451, in generate_module
    self.generate_variable_stub(name, obj, output=variables)
  File "/usr/local/py-utils/venvs/mypy/lib/python3.12/site-packages/mypy/stubgenc.py", line 901, in generate_variable_stub
    type_str = self.strip_or_import(self.get_type_annotation(obj))
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/py-utils/venvs/mypy/lib/python3.12/site-packages/mypy/stubgenc.py", line 535, in get_type_annotation
    return self.get_type_fullname(type(obj))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/py-utils/venvs/mypy/lib/python3.12/site-packages/mypy/stubgenc.py", line 768, in get_type_fullname
    assert module_name is not None, typ
AssertionError: <class 'swigvarlink'>
Traceback (most recent call last):
  File "/workspaces/typeshed/./scripts/create_baseline_stubs.py", line 253, in <module>
    main()
  File "/workspaces/typeshed/./scripts/create_baseline_stubs.py", line 234, in main
    run_stubgen(package, stub_dir)
  File "/workspaces/typeshed/./scripts/create_baseline_stubs.py", line 57, in run_stubgen
    subprocess.run(["stubgen", "-o", output, "-p", package, "--export-less"], check=True)
  File "/home/codespace/.python/current/lib/python3.12/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['stubgen', '-o', 'stubs/QuantLib', '-p', 'QuantLib', '--export-less']' returned non-zero exit status 1.
@srittau
Copy link
Contributor
srittau commented May 5, 2025

This sounds like a bug in stubgen to me. Which version of stubgen/mypy are you using?

Also Cc @JelleZijlstra @AlexWaygood Could one of you move this to the mypy issue tracker?

@AlexWaygood
Copy link
Member

Also Cc @JelleZijlstra @AlexWaygood Could one of you move this to the mypy issue tracker?

I can't because I'm only a mypy triager, not a maintainer. @sobolevn, @JelleZijlstra or @hauntsaninja can!

@sobolevn sobolevn transferred this issue from python/typeshed May 5, 2025
@sobolevn
Copy link
Member
sobolevn commented May 5, 2025

done :)

@sobolevn sobolevn added bug mypy got something wrong topic-stubgen labels May 5, 2025
@sweemer
Copy link
Author
sweemer commented May 8, 2025

This sounds like a bug in stubgen to me. Which version of stubgen/mypy are you using?

I currently have version 1.15.0 of stubgen installed.

@sterliakov sterliakov added crash and removed bug mypy got something wrong labels May 9, 2025
@sterliakov
Copy link
Collaborator

Okay, not so bad: it's only one variable that doesn't play by the rules. Namely the problematic object is QuantLib._QuantLib.cvar. If you try from QuantLib._QuantLib import cvar, it has no __dict__, no __slots__, no __class__...

That variable contains all C-layer global variables. It'd be great to extract its actual attributes, but I don't see a way to do so yet. At least we can add an exception and emit typeshed._Incomplete as its type - that's still way better than crashing. I'm not familiar with Swig, is there any trivial way to handle cvar? (or is it really empty in QuantLib case?)

For anyone working on this, the reproducer is simply

cd "$(mktemp -d)"
uv venv
. .venv/bin/activate
uv pip install mypy QuantLib
mkdir stubs
stubgen -o stubs/QuantLib -p QuantLib

@sweemer
Copy link
Author
sweemer commented May 12, 2025

I'm also not that familiar with SWIG, but I found some documentation that calls cvar a "special Python object". As such, it makes sense to treat it as some incomplete or undefined type.

We could also post an issue on the SWIG repo to ask for clarification on cvar's intended type.

@JelleZijlstra
Copy link
Member

We should patch stubgen regardless, it shouldn't crash regardless of what weird things people do to objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0