Skip to content

Configuration

funcsort.config

Configuration loading for funcsort.

Configuration is read with confkit from a dedicated funcsort.toml (preferred) or, as a fallback, the [tool.funcsort] table of pyproject.toml. Both files use the same [tool.funcsort] section and keys. The values are resolved into a single immutable :class:Settings value object that drives the engine.

Every config value is declared exactly once, as a :class:Settings field created with :func:option. The field carries its confkit data type; :func:load_settings turns the fields into confkit Config descriptors on a tool.funcsort container, so adding a value never means touching a parallel schema, a docstring or the loader::

sort_module: bool = option(True)

followed by its attribute docstring. Only a matching command-line flag, if the value should have one, lives elsewhere (:mod:funcsort.main).

_CONFIG_FILENAMES module-attribute

_CONFIG_FILENAMES = ('funcsort.toml', 'pyproject.toml')

_SECTION module-attribute

_SECTION = 'tool.funcsort'

_DATA_TYPE module-attribute

_DATA_TYPE = 'confkit_data_type'

Field-metadata key holding the confkit data type an :func:option reads with.

Settings dataclass

Settings(
    groups=option(GroupList()),
    method_type_order=option(MethodTypeOrder()),
    exclude=option(StringTuple()),
    sort_module=option(True),
    respect_dependencies=option(True),
    function_placement=option(FunctionPlacement.AFTER_CLASSES),
)

Resolved funcsort configuration that drives the sorter.

Each field is read from the [tool.funcsort] key of the same name.

groups class-attribute instance-attribute

groups = option(GroupList())

Ordered groups; output order and membership rules for members.

method_type_order class-attribute instance-attribute

method_type_order = option(MethodTypeOrder())

Secondary ordering of method types within each group.

exclude class-attribute instance-attribute

exclude = option(StringTuple())

Glob patterns of files/directories to skip.

sort_module class-attribute instance-attribute

sort_module = option(True)

Whether module-level functions are sorted or not.

respect_dependencies class-attribute instance-attribute

respect_dependencies = option(True)

Whether ordering keeps a definition ahead of anything that reads it at import time (decorators, parameter defaults, assignment values).

function_placement class-attribute instance-attribute

function_placement = option(FunctionPlacement.AFTER_CLASSES)

Where module-level functions go relative to module-level classes.

option

option(default: BaseDataType[T]) -> T
option(default: T) -> T
option(default)

Declare one config value as a :class:Settings field.

default is anything confkit's Config accepts: a plain value (True, a StrEnum member, ...) or a custom :class:~confkit.BaseDataType. The field's own default is what confkit reads back for an unset option, so a missing config file and an empty [tool.funcsort] table resolve identically.

load_settings

load_settings()

Load and resolve configuration into a :class:Settings.

Falls back to built-in defaults when no config file or section is present. An invalid value is reported and replaced by that option's default.

find_config_file

find_config_file()

Find funcsort.toml (preferred) or pyproject.toml in cwd or a parent.

_data_type

_data_type(entry)

Return the confkit data type an :func:option field was declared with.

_unset_value

_unset_value(data_type)

Return what confkit reads back for an option that is absent from the file.

funcsort.config_types

Custom confkit data types for funcsort.

These subclass confkit's public :class:~confkit.BaseDataType so a config value is read straight into the domain type the engine consumes (list[Group], list[MethodKind]) instead of a raw TOML structure that needs a second, hand-written resolution step.

confkit's TOML parser stores a value natively only when str(native) == str(value) and always hands convert the stringified form on read. TOML arrays and tables stringify as Python literals, so every type here parses its input with :func:ast.literal_eval and serialises its default back to such a literal.

A malformed value raises :class:ValueError from convert; the loader reports it and falls back to the option's default, so a broken config never silently drops members.

This module depends only on confkit's public API and never modifies the confkit package.

DEFAULT_METHOD_TYPE_ORDER module-attribute

DEFAULT_METHOD_TYPE_ORDER = (MethodKind.INSTANCE, MethodKind.CLASS, MethodKind.STATIC)

_LiteralValue

Bases: BaseDataType[T]

A confkit value stored as a native TOML structure and read back from its literal.

__str__

__str__()

Return a Python literal so confkit stores the value as a native TOML structure.

convert

convert(value)

Parse a stored value (native, or its stringified literal) into the domain type.

_literal

_literal()

Return the TOML-representable form of the current value.

_parse abstractmethod

_parse(raw)

Build the domain value from a parsed literal (or an unparseable string).

StringTuple

StringTuple(default=())

Bases: _LiteralValue[tuple[str, ...]]

A TOML array of strings, read as a tuple (a bare comma-separated string also works).

Initialise with a default tuple (empty when omitted).

_parse

_parse(raw)

Normalise a list, a single scalar or a comma-separated string into a tuple.

__str__

__str__()

Return a Python literal so confkit stores the value as a native TOML structure.

convert

convert(value)

Parse a stored value (native, or its stringified literal) into the domain type.

_literal

_literal()

Return the TOML-representable form of the current value.

MethodTypeOrder

MethodTypeOrder(default=DEFAULT_METHOD_TYPE_ORDER)

Bases: _LiteralValue[list[MethodKind]]

The secondary ordering of method types, e.g. ["instance", "class", "static"].

Initialise with a default order (instance -> class -> static when omitted).

_literal

_literal()

_parse

_parse(raw)

Parse every entry as a :class:MethodKind; an unknown entry raises ValueError.

__str__

__str__()

Return a Python literal so confkit stores the value as a native TOML structure.

convert

convert(value)

Parse a stored value (native, or its stringified literal) into the domain type.

GroupList

GroupList()

Bases: _LiteralValue[list[Group]]

The [[tool.funcsort.groups]] array of tables, read as compiled :class:Group objects.

An empty array means "no custom groups", which resolves to :func:default_groups.

Initialise with no custom groups; only this empty default is ever serialised.

_parse

_parse(raw)

Build each group table; any malformed entry raises ValueError.

__str__

__str__()

Return a Python literal so confkit stores the value as a native TOML structure.

convert

convert(value)

Parse a stored value (native, or its stringified literal) into the domain type.

_literal

_literal()

Return the TOML-representable form of the current value.

_build_group

_build_group(entry)

Build a single :class:Group from a raw config table.

_as_tokens

_as_tokens(value)

Normalise a string-or-list config value into a list of strings.

_parse_enum_set

_parse_enum_set(value, enum, default)

Parse a string/list config value into a frozenset of enum members.

None or an "any" token resolves to default (typically "no filter").