Skip to content
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

Default values in functions are always Unknown #2064

Closed
thesadru opened this issue Nov 10, 2021 · 7 comments
Closed

Default values in functions are always Unknown #2064

thesadru opened this issue Nov 10, 2021 · 7 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@thesadru
Copy link

thesadru commented Nov 10, 2021

Environment data

  • Language Server version: 2021.11.0
  • OS and version: Windows 10
  • Python version: 3.9.6

Expected behavior

Default values should be properly detected

Actual behavior

If a default value in a function contains a call statement then the callable will always be Unknown.

Static type-checking however still works.

Logs

Background analysis message: getDiagnosticsForRange
Background analysis message: resumeAnalysis
Background analysis message: getDiagnosticsForRange
Background analysis message: markFilesDirty
Background analysis message: analyze
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
Background analysis message: markFilesDirty
Background analysis message: analyze
Background analysis message: invalidateAndForceReanalysis
Background analysis message: invalidateAndForceReanalysis
Background analysis message: markFilesDirty
[Info  - 19:21:51] Searching for source files
[Info  - 19:21:51] Found 1 source file
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
Background analysis message: analyze
[BG(1)] analyzing: c:\Users\D\code\random\test.py ...
[BG(1)]   parsing: c:\Users\D\code\random\test.py (75ms)
[BG(1)]   parsing: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 0ms] (38ms)
[BG(1)]   binding: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\builtins.pyi (28ms)
[BG(1)]   binding: c:\Users\D\code\random\test.py (1ms)
[BG(1)]   checking: c:\Users\D\code\random\test.py ...
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\__init__.py [fs read 1ms] (4ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\__init__.py (0ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\applications.py [fs read 1ms] (49ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\applications.py (3ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\applications.py [fs read 1ms] (8ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\applications.py (1ms)
[BG(1)]     parsing: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 1ms] (3ms)
[BG(1)]     binding: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\typing_extensions.pyi (2ms)
[BG(1)]     parsing: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\typing.pyi [fs read 1ms] (9ms)
[BG(1)]     binding: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\typing.pyi (5ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\routing.py [fs read 1ms] (28ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\routing.py (5ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\params.py [fs read 1ms] (7ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\params.py (2ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\responses.py [fs read 1ms] (17ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\responses.py (7ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\datastructures.py [fs read 1ms] (3ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\datastructures.py (1ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\middleware\__init__.py [fs read 0ms] (4ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\middleware\__init__.py (0ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\requests.py [fs read 3ms] (12ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\starlette\requests.py (3ms)
[BG(1)]     parsing: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\collections\abc.pyi [fs read 1ms] (2ms)
[BG(1)]     binding: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\collections\abc.pyi ...
[BG(1)]       parsing: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\_collections_abc.pyi [fs read 1ms] (1ms)
[BG(1)]       binding: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\_collections_abc.pyi (1ms)
[BG(1)]     binding: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\collections\abc.pyi (2ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\param_functions.py [fs read 0ms] (6ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\fastapi\param_functions.py (1ms)
[BG(1)]     parsing: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\pydantic\fields.py [fs read 1ms] (19ms)
[BG(1)]     binding: c:\Users\D\AppData\Roaming\Python\Python39\site-packages\pydantic\fields.py (13ms)
[BG(1)]   checking: c:\Users\D\code\random\test.py (254ms)
[BG(1)] analyzing: c:\Users\D\code\random\test.py (397ms)
Background analysis message: resumeAnalysis
Background analysis message: markFilesDirty
Background analysis message: markFilesDirty
Background analysis message: markFilesDirty
Background analysis message: analyze
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
[FG] parsing: c:\Users\D\code\random\test.py (83ms)
[FG] parsing: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 0ms] (44ms)
[FG] binding: c:\Users\D\.vscode\extensions\ms-python.vscode-pylance-2021.11.0\dist\typeshed-fallback\stdlib\builtins.pyi (15ms)
[FG] binding: c:\Users\D\code\random\test.py (0ms)

Code Snippet / Additional information

Defaults are always shown as Unknown (not even autocomplete works)
image

Pylance knows about the variables
image

Type-checking still occurs
image

@jakebailey
Copy link
Member

This seems like a bug in the hover provider; hovering works in the context outside of the default values, but not when inside a default value (and everything else works).

@jakebailey jakebailey added bug Something isn't working needs investigation Could be an issue - needs investigation labels Nov 10, 2021
@github-actions github-actions bot removed the triage label Nov 10, 2021
@jakebailey
Copy link
Member

Just to quick test, if you remove the decorator from the function, do you see that hover works?

@erictraut
Copy link
Contributor

Which version of fastapi do you have installed? I just installed the latest published version (0.70.0), and I'm seeing a different behavior than you.

Here's the code that I typed in from your screen shot, for reference:

import fastapi

app = fastapi.FastAPI()

@app.route("/echo")
async def echo(text: str = fastapi.Query()):
    return {"message": text}

And here's what I see in the hover text.

Screen Shot 2021-11-10 at 12 57 42 PM

Unrelated to the issue you're reporting, I'm curious whether it's really your intent to use a function call inside of a default argument expression. That's generally a very dangerous thing to do because of the way default arguments work in Python. They are evaluated only once at the time the function definition is processed, not once for each time the function is called like in other languages.

@thesadru
Copy link
Author

Just to quick test, if you remove the decorator from the function, do you see that hover works?

The issue persists no matter the decorator. I have also tried it with other libraries which use the same kind of behavior (typer and disnake). Both have the same problem.

@thesadru
Copy link
Author

thesadru commented Nov 11, 2021

Which version of fastapi do you have installed? I just installed the latest published version (0.70.0), and I'm seeing a different behavior than you.

Also 0.70.0. this issue is not even specific to fastapi

And here's what I see in the hover text.

Screen Shot 2021-11-10 at 12 57 42 PM

At times the hover also works for me. However only when I have just typed the default value and haven't provided any arguments. After I do things like type arguments and then click elsewhere the hover always stops recognizing anything in the default value.
To add to this, when trying to go to the definition or declaration of anything in the default value it will always say it could not be found.

Unrelated to the issue you're reporting, I'm curious whether it's really your intent to use a function call inside of a default argument expression. That's generally a very dangerous thing to do because of the way default arguments work in Python. They are evaluated only once at the time the function definition is processed, not once for each time the function is called like in other languages.

Yes I am completely aware of the risk. The default value being shared is actually ideal in my case. Recently a lot of libraries have been using this kind of syntax as a sort of parameter descriptor. I simply chose fastapi as it's the most known library with this syntax.

@erictraut
Copy link
Contributor

Thanks for the additional context. I was able to repro the problem and find the underlying cause. This will be fixed in the next release.

@erictraut erictraut added fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Nov 11, 2021
@heejaechang
Copy link
Contributor

fixed in 2021.11.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants