Skip to content

Commit

Permalink
fix: avoid warning for .post versions
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie committed Sep 27, 2024
1 parent c61cac8 commit 594b9a7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
7 changes: 3 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 Down Expand Up @@ -132,4 +131,4 @@ def _should_warn_version(
# 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__
70 changes: 70 additions & 0 deletions tests/test_version_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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.setenv('PYRIGHT_PYTHON_PYLANCE_VERSION', '1.0.0')
assert not _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 594b9a7

Please sign in to comment.