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

Statements failed in pylance but passed in mypy #861

Closed
Char2sGu opened this issue Jan 21, 2021 · 4 comments
Closed

Statements failed in pylance but passed in mypy #861

Char2sGu opened this issue Jan 21, 2021 · 4 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

@Char2sGu
Copy link

Char2sGu commented Jan 21, 2021

Environment data

  • Language Server version: 2021.1.1
  • OS and version: Windows 10 Work Station 1909
  • Python version: Python 3.9.0 64-bit

Expected behaviour

Actual behaviour

from typing import Dict, List, Type


class BaseClass:
    ...


class SubClass(BaseClass):
    ...


# these are ok
cls: Type[BaseClass] = SubClass
cls_dict: Dict[str, Type[BaseClass]] = {'string': SubClass}
cls_list: List[Type[BaseClass]] = [SubClass]


class A:
    # these are ok
    cls: Type[BaseClass] = SubClass
    cls_dict: Dict[str, Type[BaseClass]] = {'string': SubClass}
    cls_list: List[Type[BaseClass]] = [SubClass]


class B(A):
    # this is ok
    cls = SubClass

    # but the following statements failed in pylance but passed in mypy

    cls_dict = {'string': SubClass}
    """
    (variable) cls_dict: dict[str, Type[SubClass]]
    "cls_dict" overrides symbol of same name in class "A"
    TypeVar "_VT@dict" is invariant
        "SubClass" is incompatible with "BaseClass"PylancereportIncompatibleVariableOverride
    test.py(11, 5): Overridden symbol
    """

    cls_list = [SubClass]
    """
    (variable) cls_list: list[Type[SubClass]]
    "cls_list" overrides symbol of same name in class "A"
    TypeVar "_T@list" is invariant
        "SubClass" is incompatible with "BaseClass"PylancereportIncompatibleVariableOverride
    test.py(12, 5): Overridden symbol
    """

Logs

Code Snippet / Additional information

@Char2sGu Char2sGu changed the title Type checking in classes has different results between pylance and mypy Statements failed in pylance but passed in mypy Jan 21, 2021
@judej
Copy link
Contributor

judej commented Jan 21, 2021

@erictraut type checking issue in class scope?

@judej judej added the needs investigation Could be an issue - needs investigation label Jan 21, 2021
@github-actions github-actions bot removed the triage label Jan 21, 2021
@erictraut
Copy link
Contributor

This looks like it's a bug in the reportIncompatibleVariableOverride diagnostic rule. I'll take a look.

@erictraut
Copy link
Contributor

The bug is particular to this specific case, which is why we haven't seen it before. It requires that:

  1. A base class has a class variable
  2. There is a type declared for that class variable
  3. A subclass has the same class variable but does not provide a declared type for it
  4. The subclass assigns a value to that class variable
  5. The inferred type of the assigned expression depends on the declared type of the class variable

I've address the problem in this PR: microsoft/pyright@c92c3ef.

It will be fixed in the next release.

@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 needs investigation Could be an issue - needs investigation labels Jan 21, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.1.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202113-27-january-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

4 participants