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 and **type is not parsed properly for Python 2 type hints #1191

Closed
skasch opened this issue Apr 26, 2021 · 3 comments
Closed

*type and **type is not parsed properly for Python 2 type hints #1191

skasch opened this issue Apr 26, 2021 · 3 comments
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version waiting for user response Requires more information from user

Comments

@skasch
Copy link

skasch commented Apr 26, 2021

Environment data

  • Language Server version: 2021.4.2
  • OS and version: Ubuntu 16.04
  • Python version (& distribution if applicable, e.g. Anaconda): 2.7.12

Expected behaviour

*type and **type type hints are parsed properly by Pylance.

Actual behaviour

*type and **type type hints raise an error.

Logs

I did not manage to get any relevant log, even with Trace-level logging enabled.

Code Snippet / Additional information

def test_fun(*args, **kwargs):
    # type: (*int, **float) -> int
    return sum(args) + sum(round(kwarg) for kwarg in kwargs.values())

According to PEP-484, this should be the standard way to type hint * and ** arguments in older versions of Python. However, writing the type hint that way raises errors:

image

In the code, args is considered to have type Tuple[Any, ...] and kwargs is considered to have type Dict[str, Any].

However, it looks like using line-by-line type hints work:

def test_fun(
    *args,  # type: int
    **kwargs,  # type: float
):
    # type: (...) -> int
    return sum(args) + sum(round(kwarg) for kwarg in kwargs.values())

Here, args is considered to have type Tuple[int, ...], and kwargs is considered to have type Dict[str, float].

I also tried removing the * and **:

def test_fun(*args, **kwargs):
    # type: (int, float) -> int
    return sum(args) + sum(round(kwarg) for kwarg in kwargs.values())

In that case, Pylance does find the right types, args is considered to have type Tuple[int, ...] and kwargs to have type Dict[str, float], but this is inconsistent with the expected format (and I find it also harder to read).

@erictraut
Copy link
Contributor

Pylance is designed for Python 3 and doesn't generally support Python 2. It does have minimal support for function annotation comments with the goal of supporting type stubs for libraries that work with Python 2 and 3, but this support has many limitations.

I'm curious why you are still using function annotation comments in your code. Is it not feasible to switch to Python 3 syntax?

We don't plan to make any further significant investments in Python 2 function annotation comments. However, in this particular case, it's simple to eliminate the parsing error that you're seeing (just a one-line change), so I'll make that change in the parser. I'm not adding full semantic validation (e.g. verifying that * and ** parameter annotations line up with the corresponding *args and **kwargs parameters in the signature). That would require a much bigger investment.

The parser change will be included in the next release of pylance.

@judej judej added the waiting for user response Requires more information from user label Apr 26, 2021
@github-actions github-actions bot removed the triage label Apr 26, 2021
@judej judej added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Apr 26, 2021
@skasch
Copy link
Author

skasch commented Apr 27, 2021

Thank you very much for taking the time to fix that, despite it being a Python 2-specific issue!

Unfortunately, I'm currently working on a pretty large codebase running in Python 2, which is why I cannot use the Python 3 syntax. We are working on a transition to Python 3, but in the meantime, we try as much as possible to write Python 2 code that can also be interpreted as Python 3 code. Thanks to that, I've found that Pylance is really useful for me to analyze the code, despite it being Python 2 code. There are of course some limitations, and having to use type comments instead of type annotations is one of them.

@jakebailey
Copy link
Member

This issue has been fixed in version 2021.4.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202143-29-april-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version waiting for user response Requires more information from user
Projects
None yet
Development

No branches or pull requests

4 participants