Skip to content
Sarj Code Standards

no-nominal-rebrand

Preserve NewType identity across constructor calls.

Why

NewType returns its argument unchanged at runtime. Converting directly between nominal types with a shared carrier can pass type checking and cross a domain boundary without validation.

Fix

Keep the declared nominal type, or validate and translate the value at the domain boundary.

Examples

Before — flagged Keep organization and user keys distinct
app/identities.py
from typing import NewType
UserId = NewType("UserId", str)
OrgId = NewType("OrgId", str)
def user_key(organization: OrgId) -> UserId:
return UserId(organization)
After — preferred Introduce a user key from its raw carrier
app/identities.py
from typing import NewType
UserId = NewType("UserId", str)
OrgId = NewType("OrgId", str)
def user_key(organization: str) -> UserId:
return UserId(organization)