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

stubgen cli flag to emit `Any` type for untyped function arguments. #4766

Closed
mgilson-argo opened this Issue Mar 20, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@mgilson-argo

mgilson-argo commented Mar 20, 2018

This is a feature request for stubgen.

In my use-case, I am executing mypy --strict --ignore-missing-imports on my package's source files and I use stubgen to generate stubs for my package so that other scripts that import my package can type check against it. I wrote something like:

class _MyPrivateClass(third_party_util.TheirClass):
    ...

In this case mypy complains because I'm inheriting from something from a third party library that has no stubs. My first inclination was to simply generate stubs for the third party library, but they aren't annotated and the stubs generated by stubgen seem to actively suppress writing Any annotations in function arguments.

e.g.:

# stub_test.py
import typing

class Foo:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
# stub_test.pyi
# Stubs for stub_test (Python 3.6)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.

from typing import Any

class Foo:
    a: Any = ...
    b: Any = ...
    c: Any = ...
    def __init__(self, a, b, c) -> None: ...

For the purposes of checking against this, it'd be really helpful if stubgen actually made the signature of Foo.__init__ to be def __init__(self, a: Any, b: Any, c: Any) -> None .... As implemented currently, I think that this will cause warnings to be generated by mypy --strict for anything that relies on these stubs (I think --disallow-untyped-calls is the culprit) whereas the proposed solution would allow that mypy flag to pass and then I can assert that (at the very least), the code under my control has type annotations for all the functions.

I'm not sure if this behavior should be accepted outright, or hidden behind a commandline argument, but it feels like there should be a way to generate stubs where everything is typed (even if the type is Any).

@gvanrossum

This comment has been minimized.

Member

gvanrossum commented Mar 20, 2018

Have you tested your assumption that this makes any difference (e.g. by manually adding some Any annotations to a generated stub)? An argument without a type is assumed to have type Any, and a missing return type is likewise assumed to be Any.

If you have a concrete (small) example where adding the Any annotations makes a difference please show it.

@mgilson-argo

This comment has been minimized.

mgilson-argo commented Mar 20, 2018

Sure, stubs aside, the following code behaves differently depending on which __init__ line you have commented out:

"""Quick test.

This is quick_test.py

`mypy --strict quick_test.py` will behave differently depending on which `__init__` you comment out.
"""
from typing import Any

class Foo:
    def __init__(self, a, b, c) -> None:
    # def __init__(self, a: Any, b: Any, c: Any) -> None:
        self.a = a
        self.b = b
        self.c = c

def do_something() -> None:
    Foo(10, 12, 13)

I'm not actually sure what flag in mypy is tickling this warning though:

$ mypy --strict --allow-untyped-calls --allow-untyped-defs --allow-incomplete-defs quick_test.py 
$ mypy --strict --allow-incomplete-defs quick_test.py 
quick_test.py:10: error: Function is missing a type annotation for one or more arguments
$ mypy --strict --allow-untyped-calls quick_test.py 
quick_test.py:10: error: Function is missing a type annotation for one or more arguments
$ mypy --strict --allow-untyped-defs quick_test.py 
quick_test.py:10: error: Function is missing a type annotation for one or more arguments
$ mypy --version
mypy 0.570
@gvanrossum

This comment has been minimized.

Member

gvanrossum commented Mar 20, 2018

@mgilson-argo

This comment has been minimized.

mgilson-argo commented Mar 20, 2018

I might be able to come up with a PR. Do we need to decide on whether to toggle this behind a flag, or do we just add Any every time it was inferred?

@gvanrossum

This comment has been minimized.

Member

gvanrossum commented Mar 20, 2018

mgilson pushed a commit to mgilson/mypy that referenced this issue Mar 21, 2018

mgilson pushed a commit to mgilson/mypy that referenced this issue Mar 21, 2018

msullivan added a commit that referenced this issue Apr 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment