protocols
protocols
¶
Protocols for pytest fixtures.
This module defines the type protocols that describe the interfaces for various fixture functions. These protocols can be used as type hints in test functions to provide better type checking and IDE support.
Classes¶
FixturePath
¶
Bases: Protocol
A callable that constructs paths inside the fixtures directory.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], must_exist: bool = True) -> Path
¶
Construct a path to a fixture file.
This method creates a path to a fixture file within the fixtures directory.
It's used by the path_for_fixture
fixture to provide a consistent interface
for accessing test fixture files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
One or more path components (e.g., "data", "sample.json"). Can be strings or path-like objects. |
()
|
must_exist
|
bool
|
If True (default), raise FileNotFoundError if the file does not exist. |
True
|
Returns:
Type | Description |
---|---|
Path
|
The constructed fixture path. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If must_exist=True and the fixture file doesn't exist. |
Example
Using the FixturePath protocol in a test:
def test_fixture_path(path_for_fixture: FixturePath):
# Construct a path to a fixture file
data_path = path_for_fixture("data", "sample.json")
assert data_path.suffix == ".json"
# Check if a file exists without raising an error
optional_path = path_for_fixture("optional", "file.txt", must_exist=False)
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadFixture
¶
Bases: Protocol
A callable that reads and optionally deserializes fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8', mode: str = 'r', deserialize: Callable[[str | bytes], Any] = lambda x: x) -> Any
¶
Read and optionally deserialize a fixture file.
This method reads fixture files with customizable encoding, file mode, and deserialization. It's used by various read fixture functions to provide a consistent interface for accessing fixture file contents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
mode
|
str
|
File open mode (default: "r" for text mode). |
'r'
|
deserialize
|
Callable[[str | bytes], Any]
|
Function to process the file contents. Takes str or bytes and returns Any (default: identity). |
lambda x: x
|
Returns:
Type | Description |
---|---|
Any
|
The result of applying the deserialize function to the file contents. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
UnicodeDecodeError
|
If the file cannot be decoded with the specified encoding. |
OSError
|
If there's an error reading the file. |
Example
Using the ReadFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadJsonFixture
¶
Bases: Protocol
A callable that reads and parses JSON fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8') -> dict
¶
Read and parse a JSON fixture file.
This method reads JSON fixture files and automatically parses them into Python dictionaries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the JSON fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
Returns:
Type | Description |
---|---|
dict
|
The parsed JSON data as a Python dictionary. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
JSONDecodeError
|
If the file contains invalid JSON. |
Example
Using the ReadJsonFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadJsonlFixture
¶
Bases: Protocol
A callable that reads and parses JSONL (JSON Lines) fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8') -> Generator[dict, None, None]
¶
Read and parse a JSONL (JSON Lines) fixture file.
This method reads JSONL fixture files, where each line contains a separate JSON object. The result is a generator of dictionaries, one for each line in the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the JSONL fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
Returns:
Type | Description |
---|---|
None
|
A generator of dictionaries, one for each JSON object in the file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
JSONDecodeError
|
If any line contains invalid JSON. |
Example
Using the ReadJsonlFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadCsvFixture
¶
Bases: Protocol
A callable that reads and parses CSV fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8') -> Generator[list[str], None, None]
¶
Read and parse a CSV fixture file.
This method reads CSV fixture files using Python's built-in csv.reader. The result is a generator of lists, one for each row in the file. Each row is returned as a list of strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the CSV fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
Returns:
Type | Description |
---|---|
None
|
A generator of lists, one for each row in the CSV file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
Error
|
If the file contains malformed CSV data. |
Example
Using the ReadCsvFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadCsvDictFixture
¶
Bases: Protocol
A callable that reads and parses CSV fixture files as dictionaries.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8') -> Generator[dict[str, str], None, None]
¶
Read and parse a CSV fixture file as dictionaries.
This method reads CSV fixture files using Python's built-in csv.DictReader. The result is a generator of dictionaries, one for each row in the file. Each row is returned as a dictionary with column headers as keys and row values as string values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the CSV fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
Returns:
Type | Description |
---|---|
None
|
A generator of dictionaries, one for each row in the CSV file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the fixture file doesn't exist. |
Error
|
If the file contains malformed CSV data. |
Example
Using the ReadCsvDictFixture protocol in a test:
Source code in src/pytest_fixtures_fixtures/protocols.py
ReadYamlFixture
¶
Bases: Protocol
A callable that reads and parses YAML fixture files.
Source code in src/pytest_fixtures_fixtures/protocols.py
Functions¶
__call__(*fixture_name: str | os.PathLike[str], encoding: str = 'utf-8', unsafe_load: bool = False) -> Any
¶
Read and parse a YAML fixture file.
This method reads YAML fixture files and automatically parses them into Python objects (typically dictionaries). Requires the PyYAML library to be installed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fixture_name
|
str | PathLike[str]
|
Components of the YAML fixture file path. Can be strings or path-like objects. |
()
|
encoding
|
str
|
Text encoding to use when reading the file (default: "utf-8"). |
'utf-8'
|
unsafe_load
|
bool
|
If True, uses yaml.FullLoader instead of yaml.SafeLoader (default: False). WARNING: Only use unsafe_load=True with trusted YAML content. |
False
|
Returns:
Type | Description |
---|---|
Any
|
The parsed YAML data (typically a dictionary or list). |
Raises:
Type | Description |
---|---|
ImportError
|
If PyYAML is not installed. |
FileNotFoundError
|
If the fixture file doesn't exist. |
YAMLError
|
If the file contains invalid YAML syntax. |
Example
Using the ReadYamlFixture protocol in a test: