dropbox / pyannotate

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

Add caller func name into type_info.json to make it clear which function is doing wrong #116

Closed ghost closed 2 years ago

ghost commented 3 years ago

What this pr does

Why I do this

I think this is really necessary in a kind of big project.

Let's think if you have a code like this.

def main():
    print(gcd(15, 10))
    print(gcd(24, 10))
    print(gcd("a", "b"))

def gcd(a, b):
    return a, b

This generates type_info like below.

[
    {
        "path": "gcd.py",
        "line": 1,
        "func_name": "main",
        "type_comments": [
            "() -> None"
        ],
        "samples": 1
    },
    {
        "path": "gcd.py",
        "line": 6,
        "func_name": "gcd",
        "type_comments": [
            "(int, int) -> Tuple[int, int]",
            "(str, str) -> Tuple[str, str]"
        ],
        "samples": 3
    }
]

Then, you would come up with the idea like "this gcd function must accept only int. Which function uses in wrong way...?".

However, you can't see any information about that. So you need to dig yourself which code was wrong. I spent the most of time on this while using pyannotate.

Now, this pr will show which code screwed up. This will generate type_info like below

[
    {
        "path": "gcd.py",
        "line": 1,
        "func_name": "main",
        "type_comments": [
            "() -> None"
        ],
        "caller_name": [
            ["__main__ : 7"]
        ],
        "samples": 1
    },
    {
        "path": "gcd.py",
        "line": 6,
        "func_name": "gcd",
        "type_comments": [
            "(int, int) -> Tuple[int, int]",
            "(str, str) -> Tuple[str, str]"
        ],
        "caller_name": [
            [
                "gcd.main : 2",
                "gcd.main : 3"
            ],
            ["gcd.main : 4"]
        ],
        "samples": 3
    }
]

As you see, you can understand (str, str) is called by "gcd.main : 4" easily.

remarks

I submitted Contributor License Agreement.

ghost commented 3 years ago

Fix https://github.com/dropbox/pyannotate/issues/115

ghost commented 3 years ago

@gvanrossum

Sorry if I shouldn't mention you since you are not in dropbox now, but I just love to hear reviews of this pr.

I really love this project but this doesn't seem to be reviewed actively.

gvanrossum commented 3 years ago

You're going to have to find a new maintainer, sorry.

CLAassistant commented 2 years ago

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.