python
37 lines · 8 steps
Building a typed descriptor in Python
A data descriptor that enforces types and runs custom validators on every attribute assignment.
Explained by
highlit
1class Typed:
2 """A data descriptor that enforces a type and optional validation."""
3
4 def __init__(self, expected_type, validator=None):
5 self.expected_type = expected_type
6 self.validator = validator
7
8 def __set_name__(self, owner, name):
9 self.name = name
10 self.private_name = f"_{name}"
11
12 def __get__(self, instance, owner=None):
13 if instance is None:
14 return self
15 return getattr(instance, self.private_name)
16
17 def __set__(self, instance, value):
18 if not isinstance(value, self.expected_type):
19 raise TypeError(
20 f"{self.name!r} must be {self.expected_type.__name__}, "
21 f"got {type(value).__name__}"
22 )
23 if self.validator is not None and not self.validator(value):
24 raise ValueError(f"{value!r} failed validation for {self.name!r}")
25 setattr(instance, self.private_name, value)
26
27 def __delete__(self, instance):
28 raise AttributeError(f"{self.name!r} cannot be deleted")
29
30
31class Account:
32 owner = Typed(str, validator=lambda s: len(s) > 0)
33 balance = Typed((int, float), validator=lambda n: n >= 0)
34
35 def __init__(self, owner, balance):
36 self.owner = owner
37 self.balance = balance
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Data descriptors intercept attribute access at the class level, letting you centralize validation logic instead of scattering checks across setters.
- 2__set_name__ hands each descriptor its own attribute name automatically, so one descriptor class can be reused for many fields.
- 3Storing values under a per-name private attribute keeps the descriptor's bookkeeping separate from the public attribute it guards.
Related explainers
python
import argparse import sys from pathlib import Path
Building a subcommand CLI with argparse
cli
argparse
subcommands
Intermediate
6 steps
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
python
from collections.abc import Mapping from typing import Any, Iterator
Flattening nested config into dotted keys
recursion
generators
tree-traversal
Intermediate
7 steps
php
<?php namespace App\Support;
Merging query params onto a URL in PHP
url-parsing
query-strings
immutability
Intermediate
8 steps
python
import csv import io from datetime import datetime
Streaming a CSV export in Flask
streaming
generators
csv
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-typed-descriptor-in-python-explained-python-61cb/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.