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

Type hints for return values #990

Closed
aaltat opened this issue Nov 2, 2017 · 9 comments
Closed

Type hints for return values #990

aaltat opened this issue Nov 2, 2017 · 9 comments

Comments

@aaltat
Copy link

aaltat commented Nov 2, 2017

If have method which returns an object which Jedi is not able to resolve, is it possible define the type of the returned object in the method which returns it. Example if cache where I can return example WebDriver instances, I know that this works:

import selenium.webdriver.remote.webdriver.WebDriver


class MyClass(object):

    def get_driver(self, alias):
        return Cache().get_library_instance(alias)

    def function(self):
        driver = self.ger_driver('foobar')  # type: selenium.webdriver.remote.webdriver.WebDriver
        driver. | # complete here works fine. 

But if I use the get_driver method in multiple places in my code, in each place, I must add comment: # type: selenium.webdriver.remote.webdriver.WebDriver comment. Which is quite annoying to remember to copy/paste. Also if the import is quite long, then it easily exceeds the maximum line length. Is there a an way to define the return type in the method which actually returning the object. I did try this:

    def get_driver(self, alias):
        """Get WebDriver instance

        :return: selenium.webdriver.remote.webdriver.WebDriver
        :rtype: selenium.webdriver.remote.webdriver.WebDriver
        """
        return Cache().get_library_instance(alias)

and this:

def get_driver(self, alias):
        return Cache().get_library_instance(alias)  # type: selenium.webdriver.remote.webdriver.WebDriver

but the completion did not work. Would there be a way to get the completion working by defining something in the method side which is returning something?

@etam
Copy link

etam commented Nov 9, 2017

Probably related, and if so, then it looks like a regression. I tried the examples in https://jedi.readthedocs.io/en/latest/docs/usage.html#tab-completion-in-the-python-shell and they don't work.

@davidhalter
Copy link
Owner

Which examples? The only test example works for me in IPython (which is powered by Jedi). It doesn't work for me in the shell though. It's probably some setup fail. IMO it's not related. You may open a new issue.

@etam
Copy link

etam commented Mar 7, 2018

I just found that openSUSE provides it's own startup scripts for python and that might be the reason for my problems.

davidhalter added a commit that referenced this issue Mar 10, 2018
This issue was raised in #990. The completer was never used in Python3.4+,
because it was overwritten by Python's completer. Oddly enough it has always
worked in Python2.7/3.3.

The documentation was also slightly modified. os.path.join was always a
complex beast.
@davidhalter
Copy link
Owner

@etam I realized you are right. Please test again with current master. Tab completion in the Python shell did really not work in 3.4+. I tested it in 2.7 and 3.3 a long time ago and don't really use it much. Unfortunately it's not something I can test with regression tests. So it might just fail again in Python 4.x or whatever.

Please note that this has nothing to do with the original issue.

@etam
Copy link

etam commented Mar 12, 2018

@davidhalter It works, thanks!

@CoreyCole
Copy link

Any update on the original issue here? I see this is marked as a discussion, is it not a regression? Jedi is the upstream dependency for vscode's python language service, so a lot of people could benefit from a fix/feature here.

Right now the workaround is we need to cmd+click the function to go to its definition and see the return type, instead of getting it from the intellisense.

@davidhalter
Copy link
Owner

There's no update. I'm working on typeshed (#1096). Once that's done, this might or might not be fixed, because it involves so many type hints.

@davidhalter
Copy link
Owner

@aaltat I just realized that there might be an easier fix for your problem: Why don't you use a return annotation?

If it really needs to work with Python 2 code you can just use:

def get_driver(self, alias):
    # type: (str) -> selenium.webdriver.remote.webdriver.WebDriver
    return Cache().get_library_instance(alias)

I actually always thought that your issue was that it was not working at all. However now that I was actually reading your issue exactly (sorry - stupid me), I realized that this was probably your issue.

The above is what PEP 484 suggest for your problem: https://www.python.org/dev/peps/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code

Please let me know if this doesn't solve your problem. I will reopen if it's not good enough.

@aaltat
Copy link
Author

aaltat commented Dec 16, 2018

Ok, did not see that such thing exists. Thanks for the tip.

Repository owner deleted a comment May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants