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.

Copy link
Collaborator

@hamdanal hamdanal left a comment

Choose a reason for hiding this comment

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

Thank you for working on this.
Stubgen tests are in this file https://github.com/python/mypy/blob/master/test-data/unit/stubgen.test. You can look at existing test cases for inspiration. You can test stubgen in --inspect-mode by including # flags: --inspect-mode as the first line of the test case.

Comment on lines -835 to +844
if types or static_properties or rw_properties or methods or ro_properties:
if (
types
or static_properties
or rw_properties
or methods
or ro_properties
or class_info.docstring
):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this related to the issue with Optional? I think this change (and the one below) are causing the CI failure.

JukkaL pushed a commit that referenced this pull request Jun 17, 2024
…o PEP 604 unions (#17386)

This Fixes 2 issues with invalid `Optional` (inspired by an error
reported in #17197):
- do not crash on empty `Optional`
- treat `Optional` with more than one index as an unknown type instead
of choosing the first type.

It also fixes PEP 604 unions not being recognized as type aliases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants