Skip to content

Commit

Permalink
fix: avoid warning for .post versions (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored Sep 27, 2024
1 parent c61cac8 commit 1ce7b23
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/pyright/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from . import node, _mureq as mureq
from .utils import env_to_bool, get_cache_dir, get_latest_version
from ._version import __pyright_version__
from ._version import __version__, __pyright_version__

ROOT_CACHE_DIR = get_cache_dir() / 'pyright-python'
DEFAULT_PACKAGE_JSON: dict[str, Any] = {
Expand All @@ -37,7 +37,7 @@ def install_pyright(args: tuple[object, ...], *, quiet: bool | None) -> Path:
if version == 'latest':
version = node.latest('pyright')
else:
if _should_warn_version(version, args=args, quiet=quiet):
if _should_warn_version(args=args, quiet=quiet):
print(
f'WARNING: there is a new pyright version available (v{version} -> v{get_latest_version()}).\n'
+ 'Please install the new version or set PYRIGHT_PYTHON_FORCE_VERSION to `latest`\n'
Expand Down Expand Up @@ -104,7 +104,6 @@ def _get_pylance_pyright_version(pylance_version: str) -> str:


def _should_warn_version(
version: str,
*,
args: tuple[object, ...],
quiet: bool | None,
Expand All @@ -125,11 +124,15 @@ def _should_warn_version(
if os.environ.get('PYRIGHT_PYTHON_PYLANCE_VERSION'):
return False

force_version = os.environ.get('PYRIGHT_PYTHON_FORCE_VERSION')
if force_version and force_version != __pyright_version__:
return True

# NOTE: there is an edge case here where a new pyright version has been released
# but we haven't made a new pyright-python release yet and the user has set
# PYRIGHT_PYTHON_FORCE_VERSION to the new pyright version.
# This should rarely happen as we make new releases very frequently after
# pyright does. Also in order to correctly compare versions we would need an additional
# dependency. As such this is an acceptable bug.
latest = get_latest_version()
return latest is not None and latest != version
return latest is not None and latest != __version__
79 changes: 79 additions & 0 deletions tests/test_version_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from __future__ import annotations

import pytest

from pyright._utils import _should_warn_version


@pytest.fixture(autouse=True)
def mock_setup(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv('PYRIGHT_PYTHON_IGNORE_WARNINGS', raising=False)
monkeypatch.delenv('PYRIGHT_PYTHON_PYLANCE_VERSION', raising=False)

def _get_latest_version() -> None:
raise RuntimeError('this should be overriden in tests')

monkeypatch.setattr('pyright._utils.get_latest_version', _get_latest_version)


def test_quiet_flag() -> None:
assert not _should_warn_version(args=(), quiet=True)


def test_outputjson_flag() -> None:
assert not _should_warn_version(args=('--outputjson',), quiet=None)


@pytest.mark.parametrize(
'env_value, expected',
[
('1', False),
('true', False),
('0', True),
('false', True),
],
)
def test_ignore_warnings_env_var(monkeypatch: pytest.MonkeyPatch, env_value: str, expected: bool) -> None:
monkeypatch.setenv('PYRIGHT_PYTHON_IGNORE_WARNINGS', env_value)

if expected is True:
monkeypatch.setattr('pyright._utils.get_latest_version', lambda: '1.0.1')
monkeypatch.setattr('pyright._utils.__version__', '1.0.0')
else:
monkeypatch.setattr('pyright._utils.get_latest_version', lambda: '1.0.0')
monkeypatch.setattr('pyright._utils.__version__', '1.0.0')

assert _should_warn_version(args=(), quiet=None) == expected


def test_pylance_version_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr('pyright._utils.get_latest_version', lambda: '1.0.1')
monkeypatch.setattr('pyright._utils.__version__', '1.0.0')
monkeypatch.setenv('PYRIGHT_PYTHON_PYLANCE_VERSION', '1.0.0')
assert not _should_warn_version(args=(), quiet=None)


def test_force_version_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr('pyright._utils.get_latest_version', lambda: '1.0.1')
monkeypatch.setattr('pyright._utils.__version__', '1.0.0')
monkeypatch.setenv('PYRIGHT_PYTHON_FORCE_VERSION', '0.9.9')
assert _should_warn_version(args=(), quiet=None)


def test_version_comparison(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr('pyright._utils.get_latest_version', lambda: '1.0.1')
monkeypatch.setattr('pyright._utils.__version__', '1.0.0')
assert _should_warn_version(args=(), quiet=None)

monkeypatch.setattr('pyright._utils.get_latest_version', lambda: '1.0.0')
monkeypatch.setattr('pyright._utils.__version__', '1.0.0')
assert not _should_warn_version(args=(), quiet=None)

monkeypatch.setattr('pyright._utils.get_latest_version', lambda: None)
assert not _should_warn_version(args=(), quiet=None)


def test_default_behavior(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr('pyright._utils.get_latest_version', lambda: '1.0.1')
monkeypatch.setattr('pyright._utils.__version__', '1.0.0')
assert _should_warn_version(args=(), quiet=None)

0 comments on commit 1ce7b23

Please sign in to comment.