diff --git a/src/pyright/_utils.py b/src/pyright/_utils.py index cfa60dd..e43b4b7 100644 --- a/src/pyright/_utils.py +++ b/src/pyright/_utils.py @@ -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] = { @@ -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' @@ -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, @@ -125,6 +124,10 @@ 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. @@ -132,4 +135,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__ diff --git a/tests/test_version_warning.py b/tests/test_version_warning.py new file mode 100644 index 0000000..4a7f980 --- /dev/null +++ b/tests/test_version_warning.py @@ -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)