<#19816 PyRight giving false positives, using wron...
# github-notifications
c
#19816 PyRight giving false positives, using wrong Python version and/or needs to use newer version of PyRight? Issue created by olivercoleman-switchdin I'm using the
pants.backend.experimental.python.typecheck.pyright
backend for type checking. Part of the motivation for this is so that type checking is consistent between the IDE (VS Code) and via the build system. However I'm getting errors from the PyRight backend that are wrong and not consistent with what VS Code is showing. The input file is:
Copy code
from enum import StrEnum
from typing import TypeVar

import numpy as np
import static_frame as sf
from attrs import Attribute, field, frozen, validators
from static_frame.core.index_base import IndexBase

class TestEnum(StrEnum):
    Foo="Bar"

@frozen
class AttrsFrame:
    pass

def _check_series_type(series_or_index: sf.Series | IndexBase, data_type: type | np.dtype, field_name: str = ""):
    pass

IndexType = TypeVar("IndexType", bound=IndexBase)  # pylint: disable=invalid-name

def index_field_factory(
    index_class: type[IndexType] = sf.Index, index_type: type | np.dtype | None = None
) -> IndexType:
    validator = [validators.instance_of(index_class)]
    if index_type is not None:
        def _index_type_validator(instance: AttrsFrame, attribute: Attribute, value: IndexType):
            _check_series_type(value, index_type)
        validator.append(_index_type_validator)
    return field(validator=validator)
The output from
pants check ::
is:
Copy code
02:59:06.20 [ERROR] Completed: Typecheck using Pyright - pyright - pyright failed (exit code 1).
Loading configuration file at /tmp/pants-sandbox-yBqex3/pyrightconfig.json
Unable to get Python version from interpreter
Assuming Python platform Linux
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Searching for source files
Found 2 source files
pyright 1.1.274
/tmp/pants-sandbox-yBqex3/src/foo.py
  /tmp/pants-sandbox-yBqex3/src/foo.py:1:18 - error: "StrEnum" is unknown import symbol (reportGeneralTypeIssues)
  /tmp/pants-sandbox-yBqex3/src/foo.py:28:26 - error: Argument of type "(instance: AttrsFrame, attribute: Attribute[Unknown], value: IndexType@index_field_factory) -> None" cannot be assigned to parameter "__object" of type "(Any, Attribute[IndexType@index_field_factory | Index], IndexType@index_field_factory | Index) -> Any" in function "append"
    Type "(instance: AttrsFrame, attribute: Attribute[Unknown], value: IndexType@index_field_factory) -> None" cannot be assigned to type "(Any, Attribute[IndexType@index_field_factory | Index], IndexType@index_field_factory | Index) -> Any"
      Parameter 3: type "IndexType@index_field_factory | Index" cannot be assigned to type "IndexType@index_field_factory"
        Type "IndexType@index_field_factory | Index" cannot be assigned to type "IndexType@index_field_factory" (reportGeneralTypeIssues)
  /tmp/pants-sandbox-yBqex3/src/foo.py:29:12 - error: Expression of type "IndexType@index_field_factory | Index" cannot be assigned to return type "IndexType@index_field_factory"
    Type "IndexType@index_field_factory | Index" cannot be assigned to type "IndexType@index_field_factory" (reportGeneralTypeIssues)
3 errors, 0 warnings, 0 informations 
Completed in 0.913sec

stubPath /tmp/pants-sandbox-yBqex3/typings is not a valid directory.
The
StrEnum
issue may be because it's using the wrong version of Python?
StrEnum
was introduced in Python 3.11. Perhaps the other issue is a result of VS Code using the latest version of PyRight (1.1.326) whereas the PyRight backend for Pants is using 1.1.274? Pants version 2.17 OS Linux (in a docker contained based on
python:3.11.5-slim-bookworm
"Minimal" repo to reproduce: https://github.com/olivercoleman-switchdin/pants_test pantsbuild/pants