diff --git a/qtpy/cli.py b/qtpy/cli.py index 0901bfd3..2fb57716 100644 --- a/qtpy/cli.py +++ b/qtpy/cli.py @@ -41,14 +41,18 @@ def generate_pyright_config_json(): """Generate Pyright config to be used in `pyrightconfig.json`.""" apis_active = get_api_status() - return json.dumps({ "defineConstant": {name.upper(): is_active for name, is_active in apis_active.items()}}) + return json.dumps({ + "defineConstant": {name.upper(): is_active for name, is_active in apis_active.items()} + }) def generate_pyright_config_toml(): """Generate a Pyright config to be used in `pyproject.toml`.""" apis_active = get_api_status() - return "[tool.pyright.defineConstant]\n" + "\n".join(f"{name.upper()} = {is_active}" for name, is_active in apis_active.items()) + return "[tool.pyright.defineConstant]\n" + "\n".join( + f"{name.upper()} = {str(is_active).lower()}" for name, is_active in apis_active.items() + ) def print_mypy_args(): diff --git a/qtpy/tests/test_cli.py b/qtpy/tests/test_cli.py index 6b0f0381..6b3efe3f 100644 --- a/qtpy/tests/test_cli.py +++ b/qtpy/tests/test_cli.py @@ -2,6 +2,7 @@ import subprocess import sys +import textwrap import pytest @@ -75,3 +76,64 @@ def test_cli_mypy_args(): assert False, 'No valid API to test' assert output.stdout.strip() == expected.strip() + +def test_cli_pyright_config(): + output = subprocess.run( + [sys.executable, '-m', 'qtpy', 'pyright-config'], + capture_output=True, + check=True, + encoding='utf-8', + ) + + if qtpy.PYQT5: + expected = textwrap.dedent(""" + pyrightconfig.json: + {"defineConstant": {"PYQT5": true, "PYSIDE2": false, "PYQT6": false, "PYSIDE6": false}} + + pyproject.toml: + [tool.pyright.defineConstant] + PYQT5 = true + PYSIDE2 = false + PYQT6 = false + PYSIDE6 = false + """) + elif qtpy.PYSIDE2: + expected = textwrap.dedent(""" + pyrightconfig.json: + {"defineConstant": {"PYQT5": false, "PYSIDE2": true, "PYQT6": false, "PYSIDE6": false}} + + pyproject.toml: + [tool.pyright.defineConstant] + PYQT5 = false + PYSIDE2 = true + PYQT6 = false + PYSIDE6 = false + """) + elif qtpy.PYQT6: + expected = textwrap.dedent(""" + pyrightconfig.json: + {"defineConstant": {"PYQT5": false, "PYSIDE2": false, "PYQT6": true, "PYSIDE6": false}} + + pyproject.toml: + [tool.pyright.defineConstant] + PYQT5 = false + PYSIDE2 = false + PYQT6 = true + PYSIDE6 = false + """) + elif qtpy.PYSIDE6: + expected = textwrap.dedent(""" + pyrightconfig.json: + {"defineConstant": {"PYQT5": false, "PYSIDE2": false, "PYQT6": false, "PYSIDE6": true}} + + pyproject.toml: + [tool.pyright.defineConstant] + PYQT5 = false + PYSIDE2 = false + PYQT6 = false + PYSIDE6 = true + """) + else: + assert False, 'No valid API to test' + + assert output.stdout.strip() == expected.strip()