Skip to content
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

Fix TypeError when cls.__module__ is None #770

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

iFreilicht
Copy link
Contributor

@iFreilicht iFreilicht commented Jan 28, 2025

Trying to use pdoc -o instead of pdoc in my project caused a long stacktrace with this error at the end:

File "/Users/feuh/.../.venv/lib/python3.12/site-packages/pdoc/doc.py", line 640, in _var_annotations
    _safe_getattr(cls, "__module__", "") + "." + cls.__qualname__
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

This was caused by one class' __module__ attribute being None.

I could not find the reason why that happens and why it happens only when using -o, but this change ensures it won't be a problem again.

Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think changing the behavior of _safe_getattr is sensible as that is supposed to mirror getattr. Can we do an or "" at the callsite please?

Trying to use `pdoc -o` instead of `pdoc` in my project caused a long
stacktrace with this error at the end:

    File "/Users/feuh/.../.venv/lib/python3.12/site-packages/pdoc/doc.py", line 640, in _var_annotations
        _safe_getattr(cls, "__module__", "") + "." + cls.__qualname__
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

This was caused by one class' `__module__` attribute being `None`.

I could not find the reason why that happens and why it happens only
when using `-o`, but this change ensures it won't happen again.
@iFreilicht iFreilicht force-pushed the fix-typeerror-when-module-is-none branch from 8b029a5 to 627fbc4 Compare January 30, 2025 08:55
@iFreilicht
Copy link
Contributor Author

Yeah I see what you mean. I was debating between the two solutions myself. Fixed!

Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mhils mhils enabled auto-merge (squash) January 30, 2025 09:08
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

Successfully merging this pull request may close these issues.

2 participants