Coverage for collectra/resources/base.py: 100.00%

15 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-13 04:26 +0000

1"""The base class for pipeline resources.""" 

2 

3import re 

4 

5from pydantic import BaseModel, ConfigDict, field_validator 

6 

7from collectra.commons import PositionalFields 

8 

9__all__ = ["Resource"] 

10 

11_NAME = re.compile(r"[A-Za-z0-9_-]{1,64}") 

12 

13 

14class Resource(PositionalFields, BaseModel): 

15 """A named helper declared in ``pipeline.yaml`` and used by tasks. 

16 

17 Unlike tasks and artefacts, a resource is not a node in the pipeline graph: 

18 the pipeline just keeps it by name, and a task refers to it by that name 

19 (for example ``tools: [genera_lookup]``). The name is also what an LLM sees 

20 when the resource is offered as a tool, so it must be a plain identifier. 

21 """ 

22 

23 model_config = ConfigDict(arbitrary_types_allowed=True, extra="forbid") 

24 

25 name: str 

26 description: str = "" 

27 

28 @field_validator("name") 

29 @classmethod 

30 def _name_is_a_plain_identifier(cls, value: str) -> str: 

31 if not _NAME.fullmatch(value): 

32 raise ValueError( 

33 f"name {value!r} must be 1-64 letters, digits, underscores or hyphens" 

34 ) 

35 return value