Lesson 26 of 26
Reading real projects: uv, ruff and pydantic-core
Time to read code nobody wrote for a course. The examples below are taken unchanged from three Rust projects that Python developers use every day:
- uv, the package manager (MIT or Apache-2.0 license),
- ruff, the linter (MIT license),
- pydantic-core, the engine inside pydantic (MIT license, © Samuel Colvin).
Each excerpt is pinned to one commit, and View on GitHub opens exactly those lines, so you can scroll around and see the rest of the file. Next to it is the Python you would write for the same job. You won't know every name, and that's normal in real code: read the shape, use the hover notes, and guess the rest from the names.
ruff: is this a good module name?
ruff checks that module names follow PEP 8: lowercase letters, digits and underscores, not starting with a
digit, and not a keyword. It is all iterators and closures from lesson 8, plus Option from lesson 11.
import keyword
def is_module_name(name: str) -> bool:
# Is the first character a letter or underscore?
if not name or not ("a" <= name[0] <= "z" or name[0] == "_"):
return False
# Are the rest of the characters letters, digits, or underscores?
if not all("a" <= c <= "z" or "0" <= c <= "9" or c == "_" for c in name[1:]):
return False
# Is the identifier a keyword?
if keyword.iskeyword(name):
return False
return True
for name in ["utils", "my_module2", "MyModule", "2fast", "class"]:
print(name, is_module_name(name))/// Returns `true` if a string is a PEP 8-compliant module name (i.e., consists of lowercase
/// letters, numbers, underscores, and is not a keyword).
pub fn is_module_name(name: &str) -> bool {
// Is the first character a letter or underscore?
let mut chars = name.chars();
if !chars
.next()
.is_some_and(|c| c.is_ascii_lowercase() || c == '_')
{
return false;
}
// Are the rest of the characters letters, digits, or underscores?
if !chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') {
return false;
}
// Is the identifier a keyword?
if is_keyword(name) {
return false;
}
true
}utils True
my_module2 True
MyModule False
2fast False
class Falsepydantic-core: is "yes" a boolean?
In lax mode, pydantic accepts strings like "yes" and "off" for a bool
field. This is the function that decides. The body is simple; the signature is the part to practise reading.
def str_as_bool(value: str) -> bool:
if value.lower() in ("0", "f", "n", "no", "off", "false"):
return False
elif value.lower() in ("1", "t", "y", "on", "yes", "true"):
return True
else:
raise ValueError("Input should be a valid boolean, unable to interpret input")
for value in ["yes", "Off", "TRUE", "0", "maybe"]:
try:
print(value, "->", str_as_bool(value))
except ValueError as e:
print(value, "->", e)pub fn str_as_bool<'py>(input: &(impl Input<'py> + ?Sized), str: &str) -> ValResult<bool> {
if str == "0"
|| str.eq_ignore_ascii_case("f")
|| str.eq_ignore_ascii_case("n")
|| str.eq_ignore_ascii_case("no")
|| str.eq_ignore_ascii_case("off")
|| str.eq_ignore_ascii_case("false")
{
Ok(false)
} else if str == "1"
|| str.eq_ignore_ascii_case("t")
|| str.eq_ignore_ascii_case("y")
|| str.eq_ignore_ascii_case("on")
|| str.eq_ignore_ascii_case("yes")
|| str.eq_ignore_ascii_case("true")
{
Ok(true)
} else {
Err(ValError::new(ErrorTypeDefaults::BoolParsing, input))
}
}yes -> True
Off -> False
TRUE -> True
0 -> False
maybe -> Input should be a valid boolean, unable to interpret inputuv: normalizing a package name
PEP 503 says Zope.Interface, zope_interface and zope-interface are the
same package. In Python the rule is usually one line, re.sub(r"[-_.]+", "-", name).lower(). uv does the
same work in one pass over the bytes and checks the name is valid at the same time. The Python below follows uv's
steps, so the two can be read side by side.
def normalize(name: str) -> str:
# An empty string is not a valid package, extra, or group name.
if not name:
raise ValueError(name)
normalized = []
last = None
for char in name:
if "A" <= char <= "Z":
normalized.append(char.lower())
elif "a" <= char <= "z" or "0" <= char <= "9":
normalized.append(char)
elif char in "-_.":
# Names can't start with punctuation.
if last is None:
raise ValueError(name)
if last not in "-_.":
normalized.append("-")
else:
raise ValueError(name)
last = char
# Names can't end with punctuation.
if last in "-_.":
raise ValueError(name)
return "".join(normalized)
for name in ["Django", "zope.interface", "My__Pkg", "-bad"]:
try:
print(normalize(name))
except ValueError:
print(f"invalid: {name}")/// Normalize an unowned package or extra name.
fn normalize(name: &str) -> Result<String, InvalidNameError> {
// An empty string is not a valid package, extra, or group name.
if name.is_empty() {
return Err(InvalidNameError(name.to_string()));
}
let mut normalized = String::with_capacity(name.len());
let mut last = None;
for char in name.bytes() {
match char {
b'A'..=b'Z' => {
normalized.push(char.to_ascii_lowercase() as char);
}
b'a'..=b'z' | b'0'..=b'9' => {
normalized.push(char as char);
}
b'-' | b'_' | b'.' => {
match last {
// Names can't start with punctuation.
None => return Err(InvalidNameError(name.to_string())),
Some(b'-' | b'_' | b'.') => {}
Some(_) => normalized.push('-'),
}
}
_ => return Err(InvalidNameError(name.to_string())),
}
last = Some(char);
}
// Names can't end with punctuation.
if matches!(last, Some(b'-' | b'_' | b'.')) {
return Err(InvalidNameError(name.to_string()));
}
Ok(normalized)
}django
zope-interface
my-pkg
invalid: -baduv: parsing "pypy" into an enum
uv knows four Python implementations, as an enum: CPython, PyPy, GraalPy
and Pyodide. Earlier in the same file, long_name() returns names like
"cpython", and short_name() returns tags like Some("cp"), or None for
Pyodide. This excerpt is the parser: it lets you write "pypy".parse::<ImplementationName>().
from enum import Enum
class ImplementationName(Enum):
CPYTHON = ("cpython", "cp")
PYPY = ("pypy", "pp")
GRAALPY = ("graalpy", "gp")
PYODIDE = ("pyodide", None)
@classmethod
def from_str(cls, s: str) -> "ImplementationName":
found = next(
(
impl
for impl in cls
if s.lower() == impl.value[0]
or (impl.value[1] is not None and s.lower() == impl.value[1])
),
None,
)
if found is None:
raise ValueError(f"Unknown Python implementation `{s}`")
return found
for text in ["CPython", "pp", "jython"]:
try:
print(ImplementationName.from_str(text).name)
except ValueError as e:
print(e)impl FromStr for ImplementationName {
type Err = Error;
/// Parse a Python implementation name from a string.
///
/// Supports the full name and the platform compatibility tag style name.
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::iter_all()
.find(|implementation| {
s.eq_ignore_ascii_case(implementation.long_name())
|| implementation
.short_name()
.is_some_and(|name| s.eq_ignore_ascii_case(name))
})
.ok_or_else(|| Error::UnknownImplementation(s.to_string()))
}
}CPYTHON
PYPY
Unknown Python implementation `jython`pydantic-core: a guard that cleans up in Drop
A model can contain itself, like a dict that holds itself. Validating it naively would loop forever, so
pydantic-core remembers which (object, validator) pairs it is in the middle of. The guard adds the pair when it is
created and removes it in drop (lesson 20). Python would use a context manager. The Python version here
is simplified: it skips the depth limit.
class RecursionState:
def __init__(self):
self.ids = set()
self.depth = 0
class RecursionGuard:
def __init__(self, state, obj_id, node_id):
if (obj_id, node_id) in state.ids:
raise RecursionError("cyclic")
state.ids.add((obj_id, node_id))
state.depth += 1
self.state, self.obj_id, self.node_id = state, obj_id, node_id
def __enter__(self):
return self
def __exit__(self, *exc):
self.state.depth -= 1
self.state.ids.discard((self.obj_id, self.node_id))
state = RecursionState()
with RecursionGuard(state, 1, 7):
print("inside:", state.depth, len(state.ids))
print("after:", state.depth, len(state.ids))impl<S: ContainsRecursionState> RecursionGuard<'_, S> {
/// Creates a recursion guard for the given object and node id.
///
/// When dropped, this will release the recursion for the given object and node id.
pub fn new(state: &'_ mut S, obj_id: usize, node_id: usize) -> Result<RecursionGuard<'_, S>, RecursionError> {
state.access_recursion_state(|state| {
if !state.insert(obj_id, node_id) {
return Err(RecursionError::Cyclic);
}
if state.incr_depth() {
return Err(RecursionError::Depth);
}
Ok(())
})?;
Ok(RecursionGuard { state, obj_id, node_id })
}
/// Retrieves the underlying state for further use.
pub fn state(&mut self) -> &mut S {
self.state
}
}
impl<S: ContainsRecursionState> Drop for RecursionGuard<'_, S> {
fn drop(&mut self) {
self.state.access_recursion_state(|state| {
state.decr_depth();
state.remove(self.obj_id, self.node_id);
});
}
}inside: 1 1
after: 0 0Where the Python analogy breaks
- Real code checks bytes when it can. uv and ruff walk
name.bytes()or matchb'A'..=b'Z', because ASCII checks are much faster than Unicode ones. Python code rarely thinks about this, because astris always characters. - Errors are returned, not raised. Even uv's "this name is invalid" path is a
return Err(...). The return type tells you that a function can fail, and with which error type. In Python you have to read the body or the docs to know what it raises. - Cleanup lives in
Drop, not inwith. pydantic-core's guard undoes its work indrop, which runs on every way out. There is nowithstatement for a caller to forget. - Real code avoids making copies.
eq_ignore_ascii_casecompares without building a lowercase string, andString::with_capacityreserves memory once. In Python,s.lower() == "yes"makes a new string, and nobody minds. - Small types get their own names.
ValResult<bool>is pydantic-core's alias forResult<bool, ValError>, andInvalidNameError(String)wraps a plain string in its own type. Python code would usually usestrand raiseValueError.
Cheat card
| Python | Rust |
|---|---|
all(p(c) for c in s) | s.chars().all(|c| p(c)) |
next((x for x in xs if p(x)), None) | xs.iter().find(|x| p(x)) |
x is not None and p(x) | x.is_some_and(|v| p(v)) |
if x is None:
raise MyError() | x.ok_or_else(|| MyError::new())? |
s.lower() == "yes" | s.eq_ignore_ascii_case("yes") |
"A" <= c <= "Z" | matches!(c, b'A'..=b'Z') |
def __exit__(self, *exc): ... | impl Drop for Guard { fn drop(&mut self) { ... } } |
@classmethod
def from_str(cls, s): ... | impl FromStr for X { fn from_str(s: &str) -> Result<Self, Self::Err> { ... } } |
"".join(parts) | let mut s = String::with_capacity(n); s.push(c); |