Skip to content

Improve stubgen - #1899

Merged
JukkaL merged 8 commits into
python:masterfrom
tharvik:improve_stubgen
Sep 7, 2016
Merged

JukkaL merged 8 commits into
python:masterfrom
tharvik:improve_stubgen

Conversation

@tharvik

@tharvik tharvik commented Jul 18, 2016 •

Copy link
Copy Markdown
Contributor

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

JukkaL commented Sep 7, 2016

Copy link
Copy Markdown
Collaborator

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

tharvik commented Sep 7, 2016

Copy link
Copy Markdown
Contributor Author

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

@JukkaL

JukkaL commented Sep 7, 2016

Copy link
Copy Markdown
Collaborator

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

tharvik commented Sep 7, 2016

Copy link
Copy Markdown
Contributor Author

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

@tharvik

tharvik commented Sep 7, 2016

Copy link
Copy Markdown
Contributor Author

Also added a test case about it.

@JukkaL

JukkaL commented Sep 7, 2016

Copy link
Copy Markdown
Collaborator

Thanks!

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

gvanrossum commented Sep 7, 2016 via email

Copy link
Copy Markdown
Member

@JukkaL

JukkaL commented Sep 7, 2016

Copy link
Copy Markdown
Collaborator

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

@gvanrossum

gvanrossum commented Sep 7, 2016 via email

Copy link
Copy Markdown
Member

@JukkaL

JukkaL commented Sep 7, 2016

Copy link
Copy Markdown
Collaborator

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