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

Pylance doesn't resolve TypeVar type correctly #1165

Closed
smeznaric opened this issue Apr 16, 2021 · 6 comments
Closed

Pylance doesn't resolve TypeVar type correctly #1165

smeznaric opened this issue Apr 16, 2021 · 6 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

@smeznaric
Copy link

smeznaric commented Apr 16, 2021

Environment data

  • Language Server version: PyLance 2021.4.1
  • OS and version: CentOS Linux release 7.5.1804
  • Python version (& distribution if applicable, e.g. Anaconda): 3.6.3

Code Snippet / Additional information

I am using basic type checking (not strict).

from typing import TypeVar
import pandas as pd

DfOrSer = TypeVar("DfOrSer", pd.Series, pd.DataFrame)

def f(x: DfOrSer) -> DfOrSer:
    if isinstance(x, pd.DataFrame):
        return 2 * x
    else:
        assert isinstance(x, pd.Series)
        return 3 * x

Expected behaviour

I would expect no type issues with the above.

Actual behaviour

Both 2 * x and 3 * x are underlined.

2 * x says: Expression of type "DataFrame" cannot be assigned to return type "DfOrSer@f"
Type "DataFrame" cannot be assigned to type "DfOrSer@f" Pylance(reportGeneralTypeIssues)

3 * x says: Expression of type "Series[Unknown]" cannot be assigned to return type "DfOrSer@f"
Type "Series[Unknown]" cannot be assigned to type "DfOrSer@f" Pylance(reportGeneralTypeIssues)

I feel that generally the type checker catches too many false positives, which is reducing its utility in coding.

@erictraut
Copy link
Contributor

Thanks for posting this sample. I'll look into it in more detail.

If you have other instances where you feel a false positive is generated — especially in "basic" type checking mode (as opposed to "strict" mode), please file bugs on those, and we'll look at them too.

@smeznaric
Copy link
Author

Will do, thanks!

@erictraut
Copy link
Contributor

Thanks again for the bug report.

This was a tricky bug in pyright, the type checker that powers pylance. It involved three of the most complex piece of code in the type checker: logic to handle constrained TypeVars, type narrowing for "isinstance" calls, and binary operations (in this case, a __rmul__ operation).

I've fixed the bug and beefed up the unit tests in this area. The fix will be in the next release of pylance.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Apr 17, 2021
@smeznaric
Copy link
Author

Brilliant, thanks!

@erictraut erictraut reopened this Apr 17, 2021
@erictraut
Copy link
Contributor

We'll leave the bug open until the next release.

@jakebailey
Copy link
Member

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

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

3 participants