-
Notifications
You must be signed in to change notification settings - Fork 676
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
Use type hints in Python 3.x to infer variable types if possible #82
Comments
Is there any mechanism in Intellisense where we could give type hints to help Intellisense give us member variables for something? |
For example, can PTVS type hint like Pydev does? http://www.pydev.org/manual_adv_type_hints.html |
@zooba this would be an amazing upgrade to our workflow if PTVS supported type hinting in Python. |
@theonewolf That's great to hear. I don't suppose you'd be able to suggest how they would help you? I have a few ideas and have not yet figured out which would be most helpful:
"All of the above" is potentially where we'll end up, but the default setting is very important for this as probably 99% of users won't even realise it can be changed. I'm very keen to find out what benefits people hope to obtain from adding type hints so we can make an informed decision here, and given we already have one of the best type inferencing engines available for Python, we need to be able to leverage both that and explicit type hints. |
@gnychis PTVS does have special handling for x = get_some_value()
assert isinstance(x, MyType) We will treat if False:
x = MyType()
x = get_some_value() (This works because we retain all values in a given variable regardless of conditions or sequencing. Only Currently, we have not invented - and have no plans to invent - our own meta-language for type hinting. |
@zooba basically I want Intellisense for everything. Sometimes the type of a variable is not known. I would want to be able to use type hints to fill in such gaps. Flying blind gets annoying 😞 All four of your bullet points make sense for me as well. Ultimately I'd want Mypy-style static type-checking capabilities down the road. |
@theonewolf I don't think we preserve enough information for it to be generic, unfortunately - dictionaries in particular only keep a very limited amount of precise key-value mappings based on the key value before switching to using the key's type (probably The best approach right now is to assert at the start of your function (we should support |
Gotcha, thanks @zooba. I think I was able to get it to work for |
@zooba do you (plan to) support the types from docstrings? |
@denfromufa No specific plans, but we could do. We already do a limited form of this when scanning |
@zooba, type hinting is good (in fact looks like borrowed from TypeScript), but who is going to write it (like DefinitelyTyped) for numpy, scipy, sympy, pandas, scikit-learn, matplotlib, etc. in Py 2/3 compatible manner? |
Here is a good example:
|
I'd suggest following PEP 484 for how to style type hints. I agree with @denfromufa that this functionality is extremely nice to have. Our company started out using PTVS, but switched over to PyCharm for its support of type hints. We can now type hint like:
Not just that, but now these type hints are used directly by PyCharm's linter to throw us warnings if types don't match. PTVS has amazing intellisense support for IronPython, but after we switched to PythonNet so that we could use Python 3.x, we lost intellisense to C# in PTVS (since it does not directly support PythonNet), and the additional support of type hints just led us towards PyCharm. Just some feedback from our experience! We'd love to see PythonNet support in PTVS and type hints. This speeds up our development. |
Yep, this whole item was started because of PEP 484, so that's definitely on the planning table. Processing doc strings is fine if you require a specific format, but we shouldn't be the ones to choose that winner. We've already got some heuristics that work reasonably well for many of the common formats, but we obviously can't be perfect for freeform text. The NumFocus projects have largely aligned, but there's also Google's format and ReST, which as been around forever, not to mention a few other entirely reasonable ways of documenting functions. We'd rather inject no information into analysis than incorrect information, so using doc strings here is potentially problematic. So type hints are likely to show up; doc strings are a possibility. |
Agree with you @zooba ... with no standard on docstrings, I find type hinting to be far better off with PEP 484. Docstrings should at best just provide information about the function in the intellisense bubble. |
@zooba mypy seems to be working on support for docstring parsing for type annotations, so you could potentially get both done using their parser: In general, why can't you use external parser for docstrings, like napoleon and sphinx? |
@denfromufa That's a good idea. Currently we have a docstring parser for C extensions that I'd like to also use for Python files, but to be efficient it has to run as part of the completion DB refresh. This means that we can't really use it on open projects. But it could be designed to be swapped out for other parsers fairly easily. I'm doing some experimental restructuring of our analysis engine (how we handle/track files, etc.) that I could work this into. It's my analysis-context branch if you want to follow along (note that it doesn't build yet, and there are basically just thousands of changes :) ). |
Any update on the support for docstrings? |
Hi All: What is the status of this please? Thank you |
We are still very keen to do this, but we don't have our roadmap planned out for the next release yet. Right now, still focused on getting our current release out. |
Hi zooba: So ... still not clear. Thank you, |
@EdStone You can type them in, and you should see completions from the |
Hi zooba: Thank you for replying. Unfortunately using asserts significantly clutters up code so its virtually useless. Will you implement anything in the short-term that will allow me to provide type hints in separate files / skeletons (ex. in *.pyi files in some sort of parallel directory structure) in such a way that PTVS will pick up these skeletons and adjust autocomplete / intellisense accordingly. In other words, replicate what PyCharm did with skeletons. Thanks |
@EdStone That will be part of our implementation for type hints, so it won't come any sooner than they do. Currently you can write easy to analyse code in if False:
def my_func(a, b):
return int()
my_func(int(), int()) We'll pick these up and merge them with any other definitions, so it will work about the same as we expect type hints will. The obvious downsides are all the things that type hints set out to fix, so I'm fully aware this is a workaround rather than a good solution. But it may help you get by while we work on it. |
Hi zooba: Thank you for replying. Yes, this approach is hacky so not sure if usable. My main issues is that PTVS does not properly process external packages (ex. numpy, pandas, etc.), not my own code. That's why I was asking about skeletons. |
answer pls? |
Keen to see this as well. Love using VS for my python as an old time supporter of MS dev products (since VB5). Don't want to have to jump ship to pycharm or similiar. |
Even though Python ignores type hints at runtime and they can be pretty much any expression, we should use them to infer variable types in a function if we possibly can.
See PEP 484
(Migrated from https://pytools.codeplex.com/workitem/394)
The text was updated successfully, but these errors were encountered: