Skip to content

Settings

Settings is an immutable dataclass that controls the formatting behavior. It can be constructed directly, loaded from pyproject.toml, or created from a dictionary.

Class Reference

behave_format.config.settings.Settings dataclass

Immutable formatter settings.

Attributes:

Name Type Description
indent int

Number of spaces for indentation (default 2).

sort_tags bool

Sort tags alphabetically (default True).

sort_features bool

Sort features by name (default False).

sort_scenarios bool

Sort scenarios by name (default False).

line_length int

Maximum line length for reference (default 120).

Source code in behave_format/config/settings.py
@dataclass(frozen=True)
class Settings:
    """Immutable formatter settings.

    Attributes:
        indent: Number of spaces for indentation (default 2).
        sort_tags: Sort tags alphabetically (default True).
        sort_features: Sort features by name (default False).
        sort_scenarios: Sort scenarios by name (default False).
        line_length: Maximum line length for reference (default 120).
    """

    indent: int = 2
    sort_tags: bool = True
    sort_features: bool = False
    sort_scenarios: bool = False
    line_length: int = 120

    @classmethod
    def from_pyproject(cls, path: str = "pyproject.toml") -> Settings:
        """Load settings from a ``pyproject.toml`` file.

        Args:
            path: Path to the ``pyproject.toml`` file.

        Returns:
            A Settings instance. If the file or section is missing,
            defaults are returned.
        """
        from pathlib import Path

        p = Path(path)
        if not p.exists():
            return cls()

        with p.open("rb") as f:
            data = tomllib.load(f)

        section = data.get("tool", {}).get("behave-format", {})
        return cls(
            indent=section.get("indent", 2),
            sort_tags=section.get("sort_tags", True),
            sort_features=section.get("sort_features", False),
            sort_scenarios=section.get("sort_scenarios", False),
            line_length=section.get("line_length", 120),
        )

    @classmethod
    def from_dict(cls, data: dict) -> Settings:
        """Create settings from a dictionary.

        Args:
            data: Dictionary with optional keys: indent, sort_tags,
                sort_features, sort_scenarios, line_length.

        Returns:
            A Settings instance.
        """
        return cls(
            indent=data.get("indent", 2),
            sort_tags=data.get("sort_tags", True),
            sort_features=data.get("sort_features", False),
            sort_scenarios=data.get("sort_scenarios", False),
            line_length=data.get("line_length", 120),
        )

    def with_indent(self, indent: int) -> Settings:
        """Return a new Settings with the given indent.

        Args:
            indent: Number of spaces for indentation.

        Returns:
            A new Settings instance with the updated indent.
        """
        return replace(self, indent=indent)

from_pyproject classmethod

from_pyproject(path: str = 'pyproject.toml') -> Settings

Load settings from a pyproject.toml file.

Parameters:

Name Type Description Default
path str

Path to the pyproject.toml file.

'pyproject.toml'

Returns:

Type Description
Settings

A Settings instance. If the file or section is missing,

Settings

defaults are returned.

Source code in behave_format/config/settings.py
@classmethod
def from_pyproject(cls, path: str = "pyproject.toml") -> Settings:
    """Load settings from a ``pyproject.toml`` file.

    Args:
        path: Path to the ``pyproject.toml`` file.

    Returns:
        A Settings instance. If the file or section is missing,
        defaults are returned.
    """
    from pathlib import Path

    p = Path(path)
    if not p.exists():
        return cls()

    with p.open("rb") as f:
        data = tomllib.load(f)

    section = data.get("tool", {}).get("behave-format", {})
    return cls(
        indent=section.get("indent", 2),
        sort_tags=section.get("sort_tags", True),
        sort_features=section.get("sort_features", False),
        sort_scenarios=section.get("sort_scenarios", False),
        line_length=section.get("line_length", 120),
    )

from_dict classmethod

from_dict(data: dict) -> Settings

Create settings from a dictionary.

Parameters:

Name Type Description Default
data dict

Dictionary with optional keys: indent, sort_tags, sort_features, sort_scenarios, line_length.

required

Returns:

Type Description
Settings

A Settings instance.

Source code in behave_format/config/settings.py
@classmethod
def from_dict(cls, data: dict) -> Settings:
    """Create settings from a dictionary.

    Args:
        data: Dictionary with optional keys: indent, sort_tags,
            sort_features, sort_scenarios, line_length.

    Returns:
        A Settings instance.
    """
    return cls(
        indent=data.get("indent", 2),
        sort_tags=data.get("sort_tags", True),
        sort_features=data.get("sort_features", False),
        sort_scenarios=data.get("sort_scenarios", False),
        line_length=data.get("line_length", 120),
    )

with_indent

with_indent(indent: int) -> Settings

Return a new Settings with the given indent.

Parameters:

Name Type Description Default
indent int

Number of spaces for indentation.

required

Returns:

Type Description
Settings

A new Settings instance with the updated indent.

Source code in behave_format/config/settings.py
def with_indent(self, indent: int) -> Settings:
    """Return a new Settings with the given indent.

    Args:
        indent: Number of spaces for indentation.

    Returns:
        A new Settings instance with the updated indent.
    """
    return replace(self, indent=indent)

Configuration Options

Option Type Default Description
indent int 2 Number of spaces for indentation.
sort_tags bool True Sort tags alphabetically.
sort_features bool False Sort features by name.
sort_scenarios bool False Sort scenarios by name.
line_length int 120 Maximum line length for reference.

Loading from pyproject.toml

from behave_format import Settings

settings = Settings.from_pyproject("pyproject.toml")

The [tool.behave-format] section in pyproject.toml:

[tool.behave-format]
indent = 4
sort_tags = true
sort_features = false
sort_scenarios = false
line_length = 120

Creating from a Dictionary

from behave_format import Settings

settings = Settings.from_dict({"indent": 4, "sort_tags": False})

Immutability

Settings is a frozen dataclass. Once created, its attributes cannot be modified:

settings = Settings()
settings.indent = 4  # raises AttributeError