• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 19015773527

02 Nov 2025 05:33PM UTC coverage: 17.872% (-62.4%) from 80.3%
19015773527

Pull #22816

github

web-flow
Merge a12d75757 into 6c024e162
Pull Request #22816: Update Pants internal Python to 3.14

4 of 5 new or added lines in 3 files covered. (80.0%)

28452 existing lines in 683 files now uncovered.

9831 of 55007 relevant lines covered (17.87%)

0.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/python/pants/bsp/context.py
1
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
UNCOV
3
from __future__ import annotations
×
4

UNCOV
5
import threading
×
UNCOV
6
from collections.abc import Callable
×
UNCOV
7
from pathlib import Path
×
8

UNCOV
9
from pants.bsp.spec.lifecycle import InitializeBuildParams
×
UNCOV
10
from pants.bsp.spec.notification import BSPNotification
×
11

12
# Wrapper type to provide BSP rules with the ability to interact with the BSP protocol driver.
13
#
14
# Note: Due to limitations in the engine's API regarding what values can be part of a query for a union rule,
15
# this class is stored in SessionValues. See https://github.com/pantsbuild/pants/issues/12934.
16
#
17
# Concurrency: This method can be invoked from multiple threads (for each individual request). The protocol
18
# driver protects against multiple threads trying to call `initialize_connection` by only allowing
19
# the thread processing the `build/initialize` RPC to proceed; all other requests return an error _before_
20
# they enter the engine (and thus would ever have a chance to access this context).
21
#
22
# Thus, while this class can mutate due to initialization, it is immutable after it has been initialized and
23
# is thus compatible with use in the engine.
UNCOV
24
from pants.util.dirutil import safe_mkdtemp
×
25

26

UNCOV
27
class BSPContext:
×
28
    """Wrapper type to provide BSP rules with the ability to interact with the BSP protocol
29
    driver."""
30

UNCOV
31
    def __init__(self) -> None:
×
32
        """Initialize the context with an empty client params.
33

34
        This is the "connection uninitialized" state.
35
        """
UNCOV
36
        self._lock = threading.Lock()
×
UNCOV
37
        self._client_params: InitializeBuildParams | None = None
×
UNCOV
38
        self._notify_client: Callable[[BSPNotification], None] | None = None
×
UNCOV
39
        self.tempdir: Path = Path(safe_mkdtemp(prefix="bsp"))
×
40

UNCOV
41
    @property
×
UNCOV
42
    def is_connection_initialized(self):
×
UNCOV
43
        with self._lock:
×
UNCOV
44
            return self._client_params is not None
×
45

UNCOV
46
    @property
×
UNCOV
47
    def client_params(self) -> InitializeBuildParams:
×
48
        with self._lock:
×
49
            if self._client_params is None:
×
50
                raise AssertionError(
×
51
                    "Attempt to access BSP context on an uninitialized connection."
52
                )
53
            return self._client_params
×
54

UNCOV
55
    def initialize_connection(
×
56
        self, client_params: InitializeBuildParams, notify_client: Callable[[BSPNotification], None]
57
    ) -> None:
UNCOV
58
        with self._lock:
×
UNCOV
59
            if self._client_params is not None:
×
60
                raise AssertionError(
×
61
                    "Attempted to set new BSP client parameters on an already-initialized connection."
62
                )
UNCOV
63
            self._client_params = client_params
×
UNCOV
64
            self._notify_client = notify_client
×
65

UNCOV
66
    def notify_client(self, notification: BSPNotification) -> None:
×
67
        if not self.is_connection_initialized:
×
68
            return
×
69
        assert self._notify_client is not None
×
70
        self._notify_client(notification)
×
71

UNCOV
72
    def __hash__(self):
×
UNCOV
73
        return hash(self._client_params)
×
74

UNCOV
75
    def __eq__(self, other):
×
76
        if isinstance(other, BSPContext):
×
77
            return NotImplemented
×
78
        return self._client_params == other._client_params
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc