Skip to content

Improve stubgen #1899

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

Merged
merged 8 commits into from
Sep 7, 2016
Merged

Improve stubgen #1899

merged 8 commits into from
Sep 7, 2016

Conversation

tharvik
Copy link
Contributor

@tharvik tharvik commented Jul 18, 2016

Add some type defaults to generated stubs.

For example

CONST = 1

class A:
    B = ''
    def __init__(self):
        self.c = None

get typed as

from typing import Any, Optional

CONST = ...  # type: int

class A:
    B = ...  # type: str
    c = ...  # type: Optional[Any]
    def __init__(self) -> None: ...

Do not support ~complex cases like

a, b = 1, 2

@tharvik tharvik force-pushed the improve_stubgen branch 2 times, most recently from cae1030 to 6c0cd9c Compare July 18, 2016 16:29
@JukkaL
Copy link
Collaborator

JukkaL commented Sep 7, 2016

Looks good! One thing might need some tweaking. We probably shouldn't infer Optional[...] for x in the example below, since None is sometimes used as the default value for mutable instance variables, as x = [] would not be right in the class body. In this example we should probably infer Any as the type of x, at least if x is also assigned to in __init__:

class A:
    x = None
    def __init__(self):
        self.x = []

None initializers in __init__ can result in Optional[Any], since there is no reason not to initialize with [], for example. This is only relevant for class bodies.

@tharvik
Copy link
Contributor Author

tharvik commented Sep 7, 2016

I've gone the easy way and simply infer Any for the None cases.

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 7, 2016

It would still be nice to infer Optional[Any] for functions arguments with a default None value. Would it be difficult to special case function arguments this way?

@tharvik
Copy link
Contributor Author

tharvik commented Sep 7, 2016

Okay, added, not very pretty but a nicer way (not special case) would require some structural changes.

@tharvik
Copy link
Contributor Author

tharvik commented Sep 7, 2016

Also added a test case about it.

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 7, 2016

Thanks!

@JukkaL JukkaL merged commit d9c1062 into python:master Sep 7, 2016
@gvanrossum
Copy link
Member

gvanrossum commented Sep 7, 2016 via email

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 7, 2016

Yeah, it would be nice to generate : Any annotations for arguments without defaults.

@gvanrossum
Copy link
Member

gvanrossum commented Sep 7, 2016 via email

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 7, 2016

That would be nice as well, but stubgen can't do that yet. I can create an issue for that. Also, for type checked code stubgen could run type checking to infer types, but it wouldn't be desirable for unannotated modules as this could result in type errors or even crashes.

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.

3 participants