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 handling of typing.Optional in stubgen #17197

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

WesleyTheGeolien
Copy link

When running stubgen on my local repository I noticed a couple of errors occurring notably around the handling of typing.Union / typing.Optional when --inspect-mode was being used.

I am running Python 3.10.14 on Linux.

I put together a mre:

from typing import Optional

def func0(left: int, right: Optional[int]) -> int:
    """Add 2 numbers together"""
    if right:
        return left + right
    return left

def func1(left: int, right: Optional[int] = None) -> int:
    """Add 2 numbers together"""
    if right:
        return left + right
    return left

I put this in mre.py then ran:

stubgen ./mre.py --include-docstrings --inspect-mode

I would get:

TypeError: compile() arg 1 must be a string, bytes or AST object

calling get_annotations(arg) fixed this as the object being passed through was <class 'int'> and not the string version

return f"{t.args[0].accept(self)} | None"
IndexError: tuple index out of range

this was because Optional[int] was becoming typing.Optional and not typing.Optional[int] I updated handling to pass the [int] through (this may be incorrect though)

@WesleyTheGeolien
Copy link
Author

I am sorry in advance, I have not created any tests as I am unsure how your test suite is setup.

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.

None yet

1 participant