Constructors#
Three constructor-only decorators — @NoArgsConstructor,
@AllArgsConstructor, and @RequiredArgsConstructor — that generate an
__init__ and nothing else. They mirror Lombok’s constructor annotations,
and are the right choice when you want a constructor but not the rest of
what @Data provides (no repr, no eq/hash, no accessors).
The problem they solve#
Different classes want different constructor shapes: a form with no arguments (everything defaulted), a form taking every field, or a form taking only the fields that must be supplied. Writing each by hand is mechanical and easy to get out of order. These three decorators generate exactly the shape you name, deriving parameters from the class’s fields.
@AllArgsConstructor#
An __init__ taking every field — required fields first, defaulted
fields after (standard Python ordering).
from inito import AllArgsConstructor
@AllArgsConstructor
class Point:
x: int
y: int
label: str = "origin"
p = Point(1, 2) # label defaults to "origin"
q = Point(1, 2, "corner")
# __init__ signature: (self, x, y, label='origin')
@RequiredArgsConstructor#
An __init__ taking only the fields without a default. Defaulted fields
are set to their default and are not constructor parameters — so the
constructor asks only for what it genuinely needs.
from inito import RequiredArgsConstructor
@RequiredArgsConstructor
class Connection:
host: str # required -> a parameter
port: int # required -> a parameter
timeout: float = 30.0 # has a default -> NOT a parameter
c = Connection("localhost", 5432)
print(c.timeout) # 30.0
# __init__ signature: (self, host, port) -- timeout is excluded
This is the closest match to Lombok’s @RequiredArgsConstructor, and pairs
naturally with dependency injection: a field
annotation is all @Service needs to autowire it.
@NoArgsConstructor#
An __init__ taking no arguments; every field is set to its default.
from inito import NoArgsConstructor
@NoArgsConstructor
class Settings:
host: str = "localhost"
port: int = 8080
s = Settings() # (self) -- no parameters
print(s.host, s.port) # localhost 8080
Every field must have a default — otherwise there would be nothing to
assign a required field. A field without a default raises
InvalidFieldDefinitionError at decoration time (fail fast, at import),
not later at construction.
At a glance#
Decorator |
|
Defaulted fields |
|---|---|---|
|
every field |
become optional parameters |
|
required fields only |
set to their default, not parameters |
|
none |
set to their default (all fields must have one) |
Notes & gotchas#
These generate only
__init__. Add @ToString, @EqualsAndHashCode, or the accessors alongside if you want them — or just use @Data, which is@AllArgsConstructorplus all of those.Typing:
@AllArgsConstructorships adataclass_transformstub, so both mypy and pyright infer its__init__signature.@NoArgsConstructor/@RequiredArgsConstructorare deliberately not marked, becausedataclass_transformcan’t express “zero args” or “defaulted fields excluded” without misleading the checker — enable the mypy plugin for those two.
See also#
@Data — bundles
@AllArgsConstructorwith repr/eq/hash/accessors.@Builder — a fluent alternative to a many-argument constructor.
Dependency injection —
@Serviceon top of@RequiredArgsConstructor.