davidhalter / jedi

Awesome autocompletion, static analysis and refactoring library for python
http://jedi.readthedocs.io
Other
5.73k stars 503 forks source link

Any type inference error #2001

Closed WutingjiaX closed 3 weeks ago

WutingjiaX commented 4 weeks ago

the code is simple,to infer the type of result image but the result is bool instead of any image

compare to pycharm: image image Is this a bug?

davidhalter commented 3 weeks ago

Sorry what? You even blurred parts. You have to write a way better issue than this, sorry. You cannot just send screenshots without any real work. Please take half an hour to write up a clear issue. I'm closing, but happy to reopen if you write a better issue.

And: I don't want to see any screenshots, please write text.

WutingjiaX commented 3 weeks ago

ok,the code is simple:

import jedi

kwargs = {
    "code": "from dataclasses import dataclass, field\n\n\n@dataclass\nclass struct_5e93e57ca36a6716689ba1d83a962784:\n    output: any\n\n@dataclass\nclass struct_0c7779220cb333fccdfe255ddf9ac695:\n    outputs: struct_5e93e57ca36a6716689ba1d83a962784 = field(default_factory=struct_5e93e57ca36a6716689ba1d83a962784)\n\n\nStart = struct_0c7779220cb333fccdfe255ddf9ac695()\n\n\nresult = Start.outputs.output\n",
    "path": "/Users/bytedance/Library/Application Support/JetBrains/PyCharm2023.3/light-edit/test11.py",
    "environment": None,
    "project": jedi.Project(path="/Users/bytedance/Library/Application Support/JetBrains/PyCharm2023.3/light-edit/"),
}

code_position = {
    "column": 1,
    "line": 16
}

definitions = jedi.Script(**kwargs).infer(**code_position)
print(definitions[0].full_name)

the output is builtins.bool instead of any:

<Name full_name='builtins.bool', description='instance bool'>

@davidhalter by the way I have no permission to reopen this issue

davidhalter commented 3 weeks ago

I cannot read the code like this. Please post it in a short amount way. According to your Github history you are probably very new to posting issues. This is of course fine. But you have to put yourself into the shoes of the maintainer: How can you post an issue that causes minimum amount of work for the other side? Here for example, I still have no idea what the issue is. I can of course count the characters to 16 to count where your cursor is. But why do I have to do that? Why is there no description? At this point I just assume you are very lazy and are wasting my time. Sorry if this is not the case.

WutingjiaX commented 3 weeks ago

sorry,after fomatter the code like follows:

from dataclasses import dataclass, field

@dataclass
class struct_5e93e57ca36a6716689ba1d83a962784:
    output: any

@dataclass
class struct_0c7779220cb333fccdfe255ddf9ac695:
    outputs: struct_5e93e57ca36a6716689ba1d83a962784 = field(default_factory=struct_5e93e57ca36a6716689ba1d83a962784)

Start = struct_0c7779220cb333fccdfe255ddf9ac695()

result = Start.outputs.output

the cursor over word "result", Its actual type should be any,but jedi inferred that bool。 I actually have a description of the problem at the top, and I thought you had read it. I'm really sorry @aivarannamaa

WutingjiaX commented 3 weeks ago

I have identified where this bug is located。

The definition of any Type is:

def any(*args, **kwargs): # real signature unknown
    """
    Return True if bool(x) is True for any x in the iterable.

    If the iterable is empty, return False.
    """
    pass

It has a return type bool

So,when the jedi infers annotation it should have some mechanism to stop continuing recursive types(For example, in method infer_annotations, it is determined that)。Perhaps it's a built-in type whitelist or something else @aivarannamaa

davidhalter commented 3 weeks ago

I actually have a description of the problem at the top, and I thought you had read it. I'm really sorry

No you don't have a description at the top. You have a few very confusing screenshots. A good report looks like this: https://github.com/davidhalter/jedi/issues/1691 In that particular issue I can use the code the user provided and check it. This is not always easily possible, but I'm pretty sure in your case it would have been possible.

At this point I still do not know what any is. You should also try to create a minimal reproducible example (please read https://stackoverflow.com/help/minimal-reproducible-example). For example I do not want to read something like struct_0c7779220cb333fccdfe255ddf9ac695. This is incredibly confusing. Just use short names, so I can understand like foo and bar or apple and pear.

[any] It has a return type bool

Jedi is aimed at autocompletion. For this reason it uses docstrings like Return True if bool(x) is True for any x in the iterable. and thinks because the docstring has True or bool in it (I'm not quite sure what triggers it), its return type will be a bool. I won't change that. However you can probably just use an annotation like def any(*args, **kwargs) -> Any to make your intentions clear, which is probably important anyway, because the docstring says something else.