Groups¶
funcsort.groups ¶
Generic, configuration-driven sorting model for funcsort.
This module is the pure engine: it knows nothing about libcst or TOML. It defines
the value objects (:class:Group, :class:Member, :class:Classification) and the
:func:classify placement rule that drive the sorter, plus the built-in
:func:default_groups used when no configuration is supplied.
A :class:Group is matched against a member's name by regular expression
(first-match-wins down an ordered list), optionally narrowed by member kind, method
type, scope and decorators. Users replace the default group list via configuration to
take full control of what is sorted and in which order.
DEFAULT_CREATIONAL_DUNDERS
module-attribute
¶
DEFAULT_CREATIONAL_DUNDERS = (
"__new__",
"__init__",
"__init_subclass__",
"__post_init__",
"__set_name__",
)
Scope ¶
FunctionPlacement ¶
Bases: StrEnum
Where module-level functions go relative to module-level classes.
AFTER_CLASSES
class-attribute
instance-attribute
¶
AFTER_CLASSES = 'after-classes'
Every module-level function sits below every module-level class.
BEFORE_CLASSES
class-attribute
instance-attribute
¶
BEFORE_CLASSES = 'before-classes'
Every module-level function sits above every module-level class.
INTERLEAVED
class-attribute
instance-attribute
¶
INTERLEAVED = 'interleaved'
Classes stay where they are and functions fill the gaps around them in group order, so public functions can rise above the classes while private ones sink below them.
MemberKind ¶
MethodKind ¶
Member
dataclass
¶
Member(index, node, kind, name, method_type, scope, decorators=())
A single sortable statement within a class or module body.
Attributes:
| Name | Type | Description |
|---|---|---|
index |
int
|
Original position within the body (used to minimise movement). |
node |
object
|
The underlying libcst node (kept opaque to this module). |
kind |
MemberKind
|
Whether the member is a function or an assignment. |
name |
str
|
The function name or the assignment's target name. |
method_type |
MethodKind
|
Binding of a function; :attr: |
scope |
Scope
|
Whether the member lives in a class or module body. |
decorators |
tuple[str, ...]
|
Normalised, dotted decorator names applied to the member. |
Group
dataclass
¶
Group(
name,
matchers,
kinds=(lambda: frozenset({MemberKind.FUNCTION}))(),
types=None,
scopes=None,
decorators=None,
)
An ordered bucket members are placed into, matched by name regex.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable identifier (used in diagnostics and bucket keys). |
matchers |
tuple[re.Pattern[str], ...]
|
Pre-compiled patterns; a member matches if any one searches its name. |
kinds |
frozenset[MemberKind]
|
Member kinds this group accepts (defaults to functions only). |
types |
frozenset[MethodKind] | None
|
Method types this group accepts; |
scopes |
frozenset[Scope] | None
|
Scopes this group accepts; |
decorators |
tuple[re.Pattern[str], ...] | None
|
If set, the member must carry a decorator whose name matches one of
these patterns; |
kinds
class-attribute
instance-attribute
¶
kinds = field(default_factory=lambda: frozenset({MemberKind.FUNCTION}))
targets_assignments ¶
targets_assignments()
Return whether this group can ever match an assignment member.
_matches_decorator ¶
_matches_decorator(member)
Return whether any of the member's decorators match this group's filter.
Classification
dataclass
¶
Classification(member, group)
The result of placing a member into a group.
Using a value object instead of an Optional[str] keeps call sites explicit
and lets the placement result grow fields without churn.
compile_matcher ¶
compile_matcher(token)
Compile a configured match/decorator token.
A bare Python identifier is treated as an exact-name match; anything else is a regex.
default_groups ¶
default_groups(creational_dunders=DEFAULT_CREATIONAL_DUNDERS)
Return the built-in groups, in output order.
The matchers are deliberately mutually exclusive so membership is independent of
the groups' order (only the output order changes when a user reorders them):
creational (exact names) -> dunder (other __x__) -> public (no leading
underscore) -> protected (single leading underscore) -> private (leading __,
non-magic).
classify ¶
classify(member, groups)
Place member into the first group that accepts it (first-match-wins).
_dunder_matcher ¶
_dunder_matcher(creational_dunders)
Match any magic method (__x__) that is not one of the creational names.