Skip to content

Latest commit

 

History

History
145 lines (104 loc) · 4.09 KB

File metadata and controls

145 lines (104 loc) · 4.09 KB

S-CORE Python Basics

  • ✅ Makes development of Python code easier inside Bazel
  • ✅ Provides a Python virtualenv target
  • ✅ Provides S-CORE wide defaults for linting and formatting
  • ✅ Provides pytest with S-CORE wide defaults for pytest

How To: Integrate

In the consuming Bazel project, in your MODULE.bazel import the python basics

bazel_dep(name = "score_tooling", version = "1.0.4")

Python Virtualenv

The score_virtualenv rule creates a virtualenv for your IDE (syntax highlighting, formatting, linting etc).

load("@score_tooling//:defs.bzl", "score_virtualenv")

score_virtualenv(
    # optional: change target name
    name = "ide_support",

    # optional: change generated venv name
    venv_name = ".venv",

    # optional: add your own requirements
    # e.g. all_requirements comming from your pip installation via '@pip...
    reqs = []
)

You can create the virtualenv via the name you have defined above, e.g. bazel run //:ide_support.

Pytest

The score_py_pytest rule creates a pytest target.

Note: the pytest version is determined by the score_python_basics module. It is intentionally not possible to overwrite it.

load("@score_tooling//:defs.bzl", "score_py_pytest")

score_py_pytest(
    name = "test_my_first_check",
    srcs = [
        "test_my_first_check.py"
    ],
    plugins = [
        # Optionally specify pytest plugins, that will register their fixtures
    ],
    args = [
        # Specify optional arguments, ex:
        "--basetemp=/tmp/pytest",
    ],
    # Optionally provide config file that will override the default one
    # pytest.ini / pytest.toml / pyproject.toml are accepted
    pytest_config = "//my_pytest:my_pytest_ini",

    # Optionally provide tags the test should have, in order to allow for execution grouping
    tags = [
        "integration",
        #...
    ]
)

Basedpyright

Basedpyright provides enhanced type checking and Pylance features for Python development. It's included as part of the score_python_basics package.

Note: The basedpyright version is determined by the score_python_basics module's dependencies (see requirements.txt).

IDE Integration (VS Code)

To enable Basedpyright in your VS Code editor:

  1. Ensure the virtual environment is active: Make sure you have created and selected the Python interpreter from the virtual environment generated by score_virtualenv (see Python Virtualenv section and Development setup).

  2. Install the extension: Add the detachhead.basedpyright extension to your .vscode/extensions.json file:

    {
        "recommendations": [
            // ... other extensions
            "detachhead.basedpyright"
        ]
    }
  3. Define a root pyproject.toml that includes:

    [tool.basedpyright]
    extends = "bazel-bin/ide_support.runfiles/score_python_basics~/pyproject.toml"
    
    exclude = [
        "**/__pycache__",
        "**/.*",
        "**/bazel-*",
    ]
  4. Reload VS Code: Reload the window to activate the extension. Basedpyright will automatically use the selected Python interpreter from your virtual environment for type checking.

Development of score_python_basics

Setting up the development environment

To set up the development environment, you need to create a python virtual environment:

bazel run private:ide_support

Note: for now the virtualenv is created in the python_basics directory. VS Code will not detect it automatically. You need to set the interpreter (python_basics/.venv/bin/python) manually via ctrl+shift+p and "Python: Select Interpreter".

Updating pytest in score_python_basics

It uses the dependencies from requirements.txt. If you have added new dependencies, make sure to update the requirements.txt file like so:

# Add new dependencies:
bazel run //private:requirements.update

# Upgrade all dependencies:
bazel run //private:requirements.update -- --upgrade

Running tests

To run the tests of the pytest module use:

bazel test //...