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 "int" cannot be assigned to type "Comparable" #2346

Closed
desktable opened this issue Feb 7, 2022 · 2 comments
Closed

Type "int" cannot be assigned to type "Comparable" #2346

desktable opened this issue Feb 7, 2022 · 2 comments

Comments

@desktable
Copy link

desktable commented Feb 7, 2022

Environment data

  • Language Server version: 2022.2.0
  • OS and version: linux x64 (Ubuntu 20.04)
  • Python version (and distribution if applicable, e.g. Anaconda): 3.9.7
  • python.analysis.indexing: null
  • python.analysis.typeCheckingMode: basic

Expected behaviour

No error.

Actual behaviour

Pylance reports the following errors on the line func(a, b):

(variable) a: Literal[1]
Argument of type "Literal[1]" cannot be assigned to parameter "x" of type "CT@_func" in function "_func"
  Type "int" cannot be assigned to type "Comparable"Pylance[reportGeneralTypeIssues](https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules)
(variable) b: Literal[2]
Argument of type "Literal[2]" cannot be assigned to parameter "y" of type "CT@func" in function "func"
  Type "int" cannot be assigned to type "Comparable"Pylance[reportGeneralTypeIssues](https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules)

Logs

[Info  - 9:26:47 AM] (3321373) Pylance language server 2022.2.0 (pyright 65946fc7) starting
[Info  - 9:26:47 AM] (3321373) Server root directory: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist
[Info  - 9:26:47 AM] (3321373) No pyproject.toml file found.
[Info  - 9:26:47 AM] (3321373) Setting pythonPath for service "<default>": "/home/desktable/.pyenv/versions/3.9.7/bin/python"
[Warn  - 9:26:47 AM] (3321373) stubPath typings is not a valid directory.
[Info  - 9:26:47 AM] (3321373) Assuming Python version 3.9
[Info  - 9:26:47 AM] (3321373) Assuming Python platform Linux
[Info  - 9:26:47 AM] (3321373) Search paths for <default>
[Info  - 9:26:47 AM] (3321373)   /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib
[Info  - 9:26:47 AM] (3321373)   typings
[Info  - 9:26:47 AM] (3321373)   /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stubs/...
[Info  - 9:26:47 AM] (3321373)   /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/bundled/stubs
[Info  - 9:26:47 AM] (3321373)   /home/desktable/.pyenv/versions/3.9.7/lib/python3.9
[Info  - 9:26:47 AM] (3321373)   /home/desktable/.pyenv/versions/3.9.7/lib/python3.9/lib-dynload
[Info  - 9:26:47 AM] (3321373)   /home/desktable/.pyenv/versions/3.9.7/lib/python3.9/site-packages
[Info  - 9:26:47 AM] (3321373) Adding fs watcher for library directories:
 /home/desktable/.pyenv/versions/3.9.7/lib/python3.9
/home/desktable/.pyenv/versions/3.9.7/lib/python3.9/lib-dynload
/home/desktable/.pyenv/versions/3.9.7/lib/python3.9/site-packages
[Warn  - 9:26:48 AM] (3321373) Exception received when installing file system watcher: TypeError [ERR_FEATURE_UNAVAILABLE_ON_PLATFORM]: The feature watch recursively is unavailable on the current platform, which is being used to run Node.js
[Warn  - 9:26:48 AM] (3321373) Exception received when installing file system watcher: TypeError [ERR_FEATURE_UNAVAILABLE_ON_PLATFORM]: The feature watch recursively is unavailable on the current platform, which is being used to run Node.js
[Warn  - 9:26:48 AM] (3321373) Exception received when installing file system watcher: TypeError [ERR_FEATURE_UNAVAILABLE_ON_PLATFORM]: The feature watch recursively is unavailable on the current platform, which is being used to run Node.js
[Warn  - 9:26:48 AM] (3321373) Exception received when installing file system watcher: TypeError [ERR_FEATURE_UNAVAILABLE_ON_PLATFORM]: The feature watch recursively is unavailable on the current platform, which is being used to run Node.js
[Info  - 9:26:48 AM] (3321373) Searching for source files
[Info  - 9:26:48 AM] (3321373) No source files found.
(3321373) [IDX(FG)] index libraries  (index) ...
(3321373) [IDX(FG)]   read stdlib indices (33ms)
(3321373) [IDX(FG)] index libraries  (index) [succeed] (33ms)
[Info  - 9:26:48 AM] (3321373) Background analysis(1) root directory: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist
[Info  - 9:26:48 AM] (3321373) Background analysis(1) started
(3321373) Background analysis message: setConfigOptions
(3321373) Background analysis message: setImportResolver
(3321373) Background analysis message: ensurePartialStubPackages
(3321373) Background analysis message: setTrackedFiles
(3321373) Background analysis message: markAllFilesDirty
(3321373) Background analysis message: setFileOpened
(3321373) [FG] parsing: /home/desktable/play_comparable.py (18ms)
(3321373) [FG] parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 2ms] (104ms)
(3321373) [FG] binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/builtins.pyi (31ms)
(3321373) [FG] binding: /home/desktable/play_comparable.py (0ms)
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: analyze
(3321373) [BG(1)] analyzing: /home/desktable/play_comparable.py ...
(3321373) [BG(1)]   parsing: /home/desktable/play_comparable.py (51ms)
(3321373) [BG(1)]   parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 3ms] (111ms)
(3321373) [BG(1)]   binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/builtins.pyi (31ms)
(3321373) [BG(1)]   binding: /home/desktable/play_comparable.py (0ms)
(3321373) [BG(1)]   checking: /home/desktable/play_comparable.py ...
(3321373) [BG(1)]     parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 1ms] (7ms)
(3321373) [BG(1)]     binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/typing_extensions.pyi (2ms)
(3321373) [BG(1)]     parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/typing.pyi [fs read 0ms] (19ms)
(3321373) [BG(1)]     binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/typing.pyi (9ms)
(3321373) [BG(1)]     parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (11ms)
(3321373) [BG(1)]     binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (1ms)
(3321373) [BG(1)]     parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/abc.pyi [fs read 0ms] (1ms)
(3321373) [BG(1)]     binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/abc.pyi (0ms)
(3321373) [BG(1)]   checking: /home/desktable/play_comparable.py (87ms)
(3321373) [BG(1)] analyzing: /home/desktable/play_comparable.py (282ms)
(3321373) Background analysis message: resumeAnalysis
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: setFileOpened
(3321373) Background analysis message: markFilesDirty
(3321373) [FG] parsing: /home/desktable/play_comparable.py (14ms)
(3321373) [FG] binding: /home/desktable/play_comparable.py (1ms)
(3321373) Background analysis message: analyze
(3321373) [BG(1)] analyzing: /home/desktable/play_comparable.py ...
(3321373) [BG(1)]   parsing: /home/desktable/play_comparable.py (2ms)
(3321373) [BG(1)]   binding: /home/desktable/play_comparable.py (1ms)
(3321373) [BG(1)]   checking: /home/desktable/play_comparable.py (12ms)
(3321373) [BG(1)] analyzing: /home/desktable/play_comparable.py (15ms)
(3321373) Background analysis message: resumeAnalysis
(3321373) [FG] parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 1ms] (22ms)
(3321373) [FG] binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/typing_extensions.pyi (3ms)
(3321373) [FG] parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/typing.pyi [fs read 0ms] (48ms)
(3321373) [FG] binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/typing.pyi (11ms)
(3321373) [FG] parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (15ms)
(3321373) [FG] binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (3ms)
(3321373) [FG] parsing: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/abc.pyi [fs read 1ms] (4ms)
(3321373) [FG] binding: /home/desktable/.vscode-server/extensions/ms-python.vscode-pylance-2022.2.0/dist/typeshed-fallback/stdlib/abc.pyi (0ms)
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: setFileOpened
(3321373) Background analysis message: markFilesDirty
(3321373) [FG] parsing: /home/desktable/play_comparable.py (2ms)
(3321373) [FG] binding: /home/desktable/play_comparable.py (1ms)
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: analyze
(3321373) [BG(1)] analyzing: /home/desktable/play_comparable.py ...
(3321373) [BG(1)]   parsing: /home/desktable/play_comparable.py (3ms)
(3321373) [BG(1)]   binding: /home/desktable/play_comparable.py (1ms)
(3321373) [BG(1)]   checking: /home/desktable/play_comparable.py (22ms)
(3321373) [BG(1)] analyzing: /home/desktable/play_comparable.py (26ms)
(3321373) Background analysis message: resumeAnalysis
(3321373) Background analysis message: getDiagnosticsForRange
(3321373) Background analysis message: getDiagnosticsForRange

Code Snippet / Additional information

from abc import abstractmethod
from typing import Protocol, TypeVar

CT = TypeVar("CT", bound="Comparable")


class Comparable(Protocol):
    """Generic comparable class"""

    @abstractmethod
    def __lt__(self: CT, other: CT) -> bool:
        ...


def func(x: CT, y: CT) -> bool:
    return x < y

a: int = 1
b: int = 2
func(a, b)  # pylance (incorrectly??) reports type issues on this line
@github-actions github-actions bot added the triage label Feb 7, 2022
@desktable desktable changed the title Pylance incorrectly states: Type "int" cannot be assigned to type "Comparable" Type "int" cannot be assigned to type "Comparable" Feb 7, 2022
@erictraut
Copy link
Contributor

erictraut commented Feb 7, 2022

The problem is that your protocol class defines a method __lt__ that has a parameter named other, and that parameter can be specified by keyword. However, the int class defines this same parameter as positional-only.

If func were to call this method with a keyword argument, it would crash at runtime.

def func(x: CT, y: CT) -> bool:
    return x.__lt__(other=y) # Crash if `int` is passed

If you attempt to assign an int value to a simple variable that is annotated with `Comparable, you'll see a more detailed error message that describes the problem.

x: Comparable = a
Expression of type "Literal[1]" cannot be assigned to declared type "Comparable"
  "Literal[1]" is incompatible with protocol "Comparable"
    "__lt__" is an incompatible type
      Type "(__x: int) -> bool" cannot be assigned to type "(other: int) -> bool"
        Position-only parameter mismatch; expected 1 but received 0

You can fix this by doing one of the following.

    # This uses the "old style" way of specifying a position-only parameter
    # by using a double underscore.
    @abstractmethod
    def __lt__(self: CT, __other: CT) -> bool:
        ...

or

    # This uses the newer mechanism introduced in PEP 570.
    @abstractmethod
    def __lt__(self: CT, other: CT, /) -> bool:
        ...

@desktable
Copy link
Author

desktable commented Feb 7, 2022

Thank you for the quick response! Confirming that adding "/" to the __lt__ definition fixes the issue.

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

3 participants