This package is maintained by dud.cl, an independent digital and AI transformation studio for enterprises and corporations.
dud-cl-rut
Validate and format Chilean RUT values in Python.
The Python API matches the TypeScript behavior and uses snake_case names.
Motivation
The last releases of the legacy JavaScript libraries rut.js and rutjs were published in 2021 and 2013, respectively, and they still have open issues. This package brings strict validation and structured issues to Python while matching the TypeScript API. See the agent reference for complete contracts and recipes.
Install
pip install dud-cl-rut
Usage
from dud_cl import rut
value = rut.parse("21.272.789-K")
# "21272789K"
rut.format(value) # "21.272.789-K"
rut.format(value, dots=False) # "21272789-K"
rut.format(value, uppercase=False) # "21.272.789-k"
rut.safe_parse("21.272.789-0", "en")
# SafeParseFailure(
# success=False,
# issue=VerifierIssue(
# kind="verifier",
# message='RUT verifier does not match. Replace "0" with "K".',
# input="21.272.789-0",
# expected="K",
# received="0",
# ),
# )
rut.is_rut("21272789k") # True (the TypeScript name is `is`)
rut.clean("0021.272.789-k") # "21272789K"
rut.get_verifier("21.272.789") # "K"
rut.compare("21.272.789-K", "21272789K") # True
With Pydantic
parse raises RutError, a ValueError subclass. Use it directly with AfterValidator:
from typing import Annotated
from pydantic import AfterValidator, BaseModel
from dud_cl.rut import parse
class User(BaseModel):
national_id: Annotated[str, AfterValidator(parse)]
user = User(national_id="21.272.789-K")
user.national_id # "21272789K"
API
| API | Purpose |
|---|---|
parse(input, language="es") |
Validate input. Return the canonical RUT or raise RutError. |
safe_parse(input, language="es") |
Validate input without raising. Return a structured result. |
is_rut(input) |
Return whether the input is valid. |
format(rut, *, dots=True, uppercase=True) |
Format a validated RUT. |
clean(input) |
Normalize input without validating it. |
compare(left, right) |
Compare two valid RUT values. |
get_verifier(body) |
Calculate the verifier for a valid body. Return None for an invalid body. |
Errors use Spanish (es) by default. Pass en as the second argument to parse or safe_parse for English messages.
The canonical string contains 8 or 9 characters: a 7- or 8-digit body and its verifier. Pass a value returned by parse to format.
Development
uv sync --extra dev
uv run pytest
uv run mypy
License
MIT
Release files for dud-cl-rut 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dud_cl_rut-0.2.0.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dud_cl_rut-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / dud_cl_rut-0.2.0.tar.gz
| Download URL | dud_cl_rut-0.2.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3695561a4d2463f6580390a195a3b8804c413f43c3138d272d944c43300e407b
|
|
BLAKE2b-256 checksum How to use checksums |
3c7dd4de329eb4f078a0cc2bde5972d360db55ae2cc6f09ca5407cb656cff00f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / dud_cl_rut-0.2.0-py3-none-any.whl
| Download URL | dud_cl_rut-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d27fd7cba50bc685ac0157c42f69541564123f6b3babd065bf3ee5112ef23d3e
|
|
BLAKE2b-256 checksum How to use checksums |
281ceaceebc585a08f6fdf674db50f66d4b3bd7ed38b6a8266a16cc6122a7fe9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|