pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Goto definition goes to stub files instead of actual definitions

Open jahan01 opened this issue 6 years ago • 42 comments

Expected behaviour

Goto definition goes to actual code even if types are defined in a stub file

Actual behaviour

Goto definition goes to stub files instead of actual code

Code Snippet / Additional information

from http import HTTPStatus

print(HTTPStatus.GATEWAY_TIMEOUT)

Try goto definition (F12) on either HTTPStatus in line 1 or GATEWAY_TIMEOUT in line 3

jahan01 avatar Jul 07 '20 05:07 jahan01

Duplicate https://github.com/microsoft/pyright/issues/795

tekumara avatar Jul 08 '20 13:07 tekumara

Goto definition on HTTPStatus results include both the .py and .pyi, which is currently the expected result. As discussed in the linked topic on pyright, we have no control on which of the multiple results appears first.

Goto definition on GATEWAY_TIMEOUT results only include the .pyi, which is a bug, it should be able to find it in sources as well but it doesn't right now.

huguesv avatar Aug 06 '20 18:08 huguesv

As a suggestion, if you couldn't control the order of displayed results, it would be nice to have an option turn off displaying *.pyi files in all the results as whole or only display results from *.pyi files when there are no *.py files.

jahan01 avatar Aug 10 '20 06:08 jahan01

@jahan01 Thanks for the suggestion. We're looking into solutions and will update here soon.

savannahostrowski avatar Aug 10 '20 18:08 savannahostrowski

Could we have two separate commands like Go To Declaration/Go To Definition for C/C++ in Visual Studio?

  • Go To Declaration goes to .h files for C/C++, for Python we can go to the .pyi files
  • Go To Definition goes to .cpp files for C++, for Python we can go to the .py files
  • If one of the .pyi file and .py file is missing, the other command could still go to the existing file

gousaiyang avatar Aug 23 '20 17:08 gousaiyang

hello, is this a bug in below gif? websocket_firefox

chaleaoch avatar Sep 04 '20 14:09 chaleaoch

At the moment, that is expected behavior.

jakebailey avatar Sep 04 '20 16:09 jakebailey

I am trying to annotate tqdm. So I did the following:

  1. Created a file typings/tqdm/__init__.pyi with:
from typing import Any, Generic, Iterable, Iterator, TypeVar
T = TypeVar('T')
class tqdm(Generic[T]):
	def __init__(self, iterable: Iterable[T], *args: Any, **kwargs: Any): ...
	def __iter__(self) -> Iterator[T]: ...
  1. Added "python.analysis.stubPath": "${workspaceFolder}/typings" to .vscode/settings.json

Now when I click on tqdm -> "Go to Definition" on another piece of code (like `from tqdm import tqdm') it jumps straight to the stub file. Also if I try to see the references on "Go to References" the source code is not listed (it was before adding the stub file). Selecting Peek -> "Peek Definition" only display the stub file as well.

I would expect it to show the source code (or at least give an option to it) when clicking "Go to Definition"

vinicius-cleves avatar Sep 19 '20 14:09 vinicius-cleves

This is a major bug. Navigation into stdlib and 3rd party libraries is basically broken. .pyi files are an implementation detail of a typechecker, please don't put them above the real code.

nnemkin avatar Oct 18 '20 10:10 nnemkin

Anybody thought there is an ambiguous meaning between goto definition and goto stubs? My temp solution is to remove .pyi files but is not very smart.

ClericPy avatar Oct 20 '20 01:10 ClericPy

Well alright. Thanks @jakebailey . I was looking thoroughly but didn't use the word "stub" just .pyi ... 😄 So, what about WIP? How is this supposed to work in the future? Is there a plan? is there a blog post about this? anything?

For me this is simply broken atm and it was fine in the past. There could be another shortcut like Shift+Ctrl+Click to Goto the stub. Actual definition should always be first!, no?

I mean I wanna be fair here: You're doing awesome work already and when doing progress things need to break here and there but I'd at least wanna know how to backup.

ewerybody avatar Oct 27 '20 18:10 ewerybody

That default preference for pyi drives me insane as it is killing the entire code browsing experience for any library that happens to keep type hints outside the source files. I do not want to browse the stubs when the source files exist and we clearly need and option to tell it to respect that preference.

Our only luck is that very few libraries decided to put keep type hints in pyi files.

ssbarnea avatar Nov 20 '20 11:11 ssbarnea

This is a sound solution, your rarely are interested in definition or type stubs at the same time. There should be a separate option to go to either of them.

Could we have two separate commands like Go To Declaration/Go To Definition for C/C++ in Visual Studio?

  • Go To Declaration goes to .h files for C/C++, for Python we can go to the .pyi files
  • Go To Definition goes to .cpp files for C++, for Python we can go to the .py files
  • If one of the .pyi file and .py file is missing, the other command could still go to the existing file

pszynk avatar Nov 23 '20 01:11 pszynk

My C/++ years precede the existence of vscode so I do not know what is current behavior on Cmd+click as I would wish to be Go To Definition by default.

There is a slight issue with having two options because what if you do not have a definition and you only have access to the declaration? in that case I suppose it should send you to the only one available

Apparently the UI is bit confused about what is a definition and declaration as I right click on the method and choose "Go To Definition" and instead it opens the in-line preview of the declaration:

This was happened accesing "Go To Definition":

ssbarnea avatar Nov 23 '20 09:11 ssbarnea

Also having trouble with this. Previously i at least got both the stub and implementation, but now I only get the stub when using F12...

b99andla avatar Nov 24 '20 12:11 b99andla

I am also experiencing this issue. My current solution involves renaming my /typings folder whenver I need goto impl. I've had it occasionally recognize both the pyi stub and the library .py implementation, but I have no way of consistently reproducing that behavior. Seems a bit odd that a stub would completely obfuscate the implementation.

RileyMShea avatar Nov 27 '20 17:11 RileyMShea

Started happening to me today. No workaround for this at least?

aleuyX avatar Dec 03 '20 19:12 aleuyX

We are currently investigating solutions to address this.

savannahostrowski avatar Dec 03 '20 19:12 savannahostrowski

I believe a solution here would be the approach Pycharm takes. Providing a clickable widget in the gutter if a corresponding .py or .pyi is available. See screenshots below.

image

image

93Akkord avatar Jan 13 '21 18:01 93Akkord

@savannahostrowski any new news on this?

maestro4 avatar Feb 01 '21 16:02 maestro4