Skip to main content

Immutable object modifications with the Result pattern - inspired by C# records

Project description

validate-with-resolute

Immutable object modifications with the Result pattern, inspired by C# records.

Wraps dataclass and Pydantic model construction/modification in resolute so validation errors are returned, never raised.

Installation

pip install validate-with-resolute

Requires Python 3.13+.

Usage

Record base class (recommended)

Inherit from Record to get .from_() and .with_() with full type checking and IDE autocomplete.

from dataclasses import dataclass
from validate_with_resolute import Record

@dataclass
class User(Record):
    name: str
    age: int

# Safe construction
result = User.from_(name="Bob", age=25)
if result.is_success:
    user = result.value

# Safe modification — original unchanged
result = user.with_(name="Alice", age=30)
if result.is_success:
    print(result.value.name)  # "Alice"
    print(user.name)          # "Bob"

Works with Pydantic:

from pydantic import BaseModel, field_validator
from validate_with_resolute import Record

class User(BaseModel, Record):
    name: str
    age: int

    @field_validator('age')
    @classmethod
    def validate_age(cls, v: int) -> int:
        if v < 0:
            raise ValueError('age must be positive')
        return v

result = User.from_(name="Bob", age=-5)
if result.has_errors:
    print(result.errors)  # validation errors captured, not raised

user = User(name="Bob", age=25)
result = user.with_(age=-5)  # same — errors captured

modify() standalone function

No inheritance required:

from dataclasses import dataclass
from validate_with_resolute import modify

@dataclass
class User:
    name: str
    age: int

user = User(name="Bob", age=25)
result = modify(user, name="Alice", age=30)
if result.is_success:
    print(result.value.name)  # "Alice"

@with_modify decorator

Adds .with_() to an existing class without inheritance. Type checkers require # type: ignore[attr-defined] on call sites.

from validate_with_resolute import with_modify

@with_modify
@dataclass
class User:
    name: str
    age: int

result = user.with_(name="Alice")  # type: ignore[attr-defined]

Prefer Record or modify() instead.

Result API

All functions return Resolute[T] from the resolute package:

result.is_success   # True on success
result.has_errors   # True on failure
result.value        # Modified object (success only)
result.errors       # List of captured errors (failure only)

Supported types

Type Strategy
Pydantic v2 model model_validate
Dataclass dataclasses.replace
Other (Python 3.13+) copy.replace()
Unsupported TypeError in result.errors

Type checking

The package ships a py.typed marker and full generic annotations. result.value is typed as T.

Approach mypy Autocomplete
Record base class full full
modify() function full full
@with_modify decorator needs ignore none

Nested objects

new_address = modify(address, city="LA").value
result = modify(person, address=new_address)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

validate_with_resolute-0.9.2.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

validate_with_resolute-0.9.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file validate_with_resolute-0.9.2.tar.gz.

File metadata

  • Download URL: validate_with_resolute-0.9.2.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for validate_with_resolute-0.9.2.tar.gz
Algorithm Hash digest
SHA256 02836e26c0a691c173a72c8c6914b25dec33b1a95a9a21bc135586ce5a528f39
MD5 56f9e346d298d9b97336224e74535191
BLAKE2b-256 69f52066e105c59516b7b12c0a54efbfba0601e24b87ba960c8df91300d9d2d4

See more details on using hashes here.

File details

Details for the file validate_with_resolute-0.9.2-py3-none-any.whl.

File metadata

File hashes

Hashes for validate_with_resolute-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cb5c3136de1aaf682967684c200ffb9da5faabb86528ca50a14d031066345559
MD5 718c9988f110b68f6753ecbed2e8ddc0
BLAKE2b-256 7dd62875476cebbb4b5ed8b45ba2ade11d756cbc4e59e22d782a2f36d00e113a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page