Type Checking
Protean ships a mypy plugin that tells static type checkers what the framework changes at runtime. The plugin resolves field factories to native Python types and adds the base classes that decorators inject. Without the plugin, mypy reports false errors on perfectly correct Protean code.
The plugin handles two patterns:
- Field factories:
String(),Integer(), and peers returnFieldSpecat the class level but resolve tostr,int, etc. at runtime. The plugin maps each factory to its Python type. - Decorator-based registration:
@domain.aggregate,@domain.entity, and the other element decorators make the class a subclass of its Protean base at runtime. The plugin adds the same base during type analysis, so mypy sees methods likeraise_()andto_dict(), the injectedid, and a keyword__init__. mypy also accepts the decorated class wherever a signature names its base class.
For the background on why this is needed, see Field System Internals.
Enabling the plugin
Add the plugin to [tool.mypy] in pyproject.toml (or an equivalent
mypy.ini / setup.cfg):
[tool.mypy]
plugins = ["protean.ext.mypy_plugin"]
Field type resolution and decorator base class injection are both enabled by this single entry. There are no further flags.
Debug mode: set PROTEAN_MYPY_DEBUG=1 to print plugin diagnostic
traces to stderr while mypy runs.
Field Type Resolution
Field factories resolve to their Python types:
| Field | Resolved Type |
|---|---|
String() |
str | None |
String(required=True) |
str |
String(default="hello") |
str |
Integer() |
int | None |
Float() |
float | None |
Boolean() |
bool | None |
Date() |
datetime.date | None |
DateTime() |
datetime.datetime | None |
List() |
list |
Dict() |
dict |
Identifier(identifier=True) |
str |
Auto(identifier=True) |
str |
ValueObject(Money) |
Money | None |
ValueObject(Money, required=True) |
Money |
Reference(Order) |
Order | None |
Reference(Order, required=True) |
Order |
HasOne(Shipment) |
Shipment | None |
HasMany(LineItem) |
list[LineItem] |
Fields are Optional (union with None) unless they are required=True,
have a default other than None, or are identifier=True. Container fields (List, Dict)
always have an implicit default and are never Optional. A default does not
make ValueObject or Reference non-optional: a value object or reference
left out at construction reads as None. HasOne is always
Optional, because an association cannot be required at runtime. A target
named by string, such as Reference("Order"), resolves to Any, so the
annotation on the field states the type.
Declaring fields for mypy --strict
The typed-annotation form gives every field its real type. mypy checks the annotation against the field call and reports a mismatch as an error:
from protean import Domain
from protean.fields import Float, HasMany, String, ValueObject
domain = Domain(name="Shop")
@domain.value_object
class Money:
amount: float = Float(required=True)
@domain.entity(part_of="Order")
class LineItem:
sku: str = String(required=True)
@domain.aggregate
class Order:
customer: str = String(required=True, max_length=50)
note: str | None = String()
total: Money = ValueObject(Money, required=True)
items: list[LineItem] = HasMany(LineItem)
Both of these lines are mypy errors:
# fragment
count: int = String(required=True) # error: str is not int
name: str = String() # error: the field is optional
The assignment form (customer = String(required=True)) type-checks the
same way. See
ADR-0038
for why the typed-annotation form is the canonical one.
The annotation-only form (customer: String(required=True)) also passes
mypy --strict, in a decorated element and in a class that inherits a
Protean base. mypy rejects a call in an annotation and keeps no type for it,
so the plugin replaces the annotation with Any and accepts the field as an
optional keyword argument. mypy reports no error when you construct the element
by keyword or use the field, but it does not check the field's type. When a
function declared to return str returns the field, mypy reports
no-any-return. Move a field to the typed-annotation form to get its
real type.
mypy's note for a call with keyword arguments does not name the function, so
the plugin accepts any such call in an annotation as a field. A call without
keyword arguments counts only when it names a Protean field factory:
label: str() still reports valid-type.
Decorator Base Class Injection
A class decorated with @domain.aggregate (or another element decorator)
is a subclass of the matching base class for mypy, as it is at runtime. For
aggregates, this exposes:
.id: Auto-injectedstr.raise_(event).to_dict().state_and the other properties- All other
BaseAggregatemethods and attributes
As a real subtype, the class is accepted wherever the framework asks for the
base: domain.cache_for(Leaderboard), cache.add(Leaderboard(...)),
x: BaseValueObject = Money(...), and isinstance(aggregate, Auditable) all
type-check. In a decorated domain service, super().__init__(...) reaches
BaseDomainService.__init__. Aggregates, entities, value objects, commands,
events, projections and queries also get a keyword __init__ built from their
Protean field declarations and their plain annotations. A plain annotation such
as note: str or note: str = "" is a pydantic field at runtime, and it is
accepted as a keyword typed Any. A class that defines its own __init__
keeps it, because that __init__ is the one that runs. A decorated class that
extends such a class keeps that __init__ for the same reason.
Supported decorators: aggregate, entity, value_object, command,
event, domain_service, command_handler, event_handler,
application_service, subscriber, projection, projector, query,
query_handler, process_manager, repository, database_model, email,
upcaster.
Both @domain.aggregate (bare) and @domain.aggregate() (called) forms
are detected. The decorator rebuilds the class on its Protean base alone, so
a mixin listed as a base is dropped at runtime, and the plugin drops it too.
A class that extends a decorated element, such as an aggregate extending a
decorated abstract aggregate, keeps its parent, and its __init__ takes the
parent's fields. See
ADR-0047
for how the base is added.
Typed framework entry points
domain.view_for(Projection)returnsReadView[Projection], so.get()and.find_by()return the projection class. Without the plugin, a decorated projection is not aBaseProjectionsubclass to a checker, andview_forreturnsReadView[Any]..queryreturns aReadOnlyQuerySet, and its results are not typed by projection.QuerySetchain methods (filter,exclude,order_by,limit,offset,only) return the queryset's own type, so aReadOnlyQuerySetstays read-only through a chain.cls._create_new()andcls.from_events(...)on an event-sourced aggregate return that aggregate's class.@handletakes an event or command class, an event type string such as"Customers.CustomerFact.v1", or the"$any"wildcard. The decorated method keeps its message and result types. The framework calls a handler positionally with the message, so passing the message by keyword is acall-argerror. A redelivery that consume-side idempotency skips returnsNone.@domain.upcaster(event_type=..., from_version=..., to_version=...)keeps the decorated class's type.
VS Code
When you run mypy through the VS Code Mypy Type Checker extension
(ms-python.mypy-type-checker), turn off Pylance's strict checker, because it
cannot read the plugin. The settings combination:
{
"python.analysis.typeCheckingMode": "off",
"mypy-type-checker.args": ["--config-file=pyproject.toml"],
"mypy-type-checker.importStrategy": "fromEnvironment"
}
Protean's own repository ships this configuration in its
.vscode/settings.json for reference.
Known Limitations
-
auto_add_id_field=False: The plugin does not inspect decorator arguments. If you passauto_add_id_field=Falseto@domain.aggregate, the plugin still injectsid. Use# type: ignore[attr-defined]if needed. -
Autocomplete for injected methods: The base class is added for mypy only. IDE engines that do not run the plugin, such as Pylance, may not show the base's methods in suggestions.
-
Explicit inheritance: If you already inherit from a base class (e.g.
class Order(BaseAggregate):), the plugin detects this and skips injection to avoid duplicates.