dropbox / pyannotate

Auto-generate PEP-484 annotations
Apache License 2.0
1.43k stars 59 forks source link

PyAnnotate ignores existing type annotations for partially annotated functions #62

Closed elvslv closed 6 years ago

elvslv commented 6 years ago

If a function's arguments are annotated, but return type is missing, for example:

def foo(
    arg1,  # type: Optional[int]
    arg2,  # type: Optional[str]
):
    return "result"

PyAnnotate ignores existing annotations, and adds a separate one:

def foo(
    arg1,  # type: Optional[int]
    arg2,  # type: Optional[str]
):
    # type: (None, None) -> str
    return "result"

Moreover, if we have more arguments,

def foo(
    argument1,  # type: Optional[int]
    argument2,  # type: Optional[str]
    argument3,  # type: Optional[str]
    argument4,  # type: Optional[str]
    argument5,  # type: Optional[str]
    argument6,  # type: Optional[str]
):
    return "result"

is annotated as

def foo(
    argument1,  # type: None  # type: Optional[int]
    argument2,  # type: None  # type: Optional[str]
    argument3,  # type: None  # type: Optional[str]
    argument4,  # type: None  # type: Optional[str]
    argument5,  # type: None  # type: Optional[str]
    argument6,  # type: None  # type: Optional[str]
    ):
    # type: (...) -> str
    return "result"
gvanrossum commented 6 years ago

Thanks for reporting this! Looks like the check for existing annotations only looks for short-form annotations (at the top of the block) and doesn't know about long-form (per arg) annotations.