Functional Weave
Code in Python

manufacturing.tolerance-check

Check a measurement against a nominal size and its upper and lower deviations, in whole micrometres.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 14 tests, run in TypeScript, Python and Rust.

What it does

Checks one measurement against a nominal size and its tolerance, the way a go/no-go gauge or an inspection sheet does:

lowerLimit = nominal + lowerDeviation upperLimit = nominal + upperDeviation passed = lowerLimit <= measured <= upperLimit

For example

  • check_tolerance(25,030, 25,000, 50, -50) → passed true, status within, deviation 30, lower limit 24,950, upper limit 25,050, out by 0 symmetric ±0.05 mm at 25 mm: 25.030 passes
  • check_tolerance(25,050, 25,000, 50, -50) → passed true, status within, deviation 50, lower limit 24,950, upper limit 25,050, out by 0 exactly on the upper limit passes: limits are inclusive
  • check_tolerance(25,051, 25,000, 50, -50) → passed false, status above, deviation 51, lower limit 24,950, upper limit 25,050, out by 1 one micrometre over the upper limit is a reject

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def check_tolerance(measured: int, nominal: int, upper_deviation: int, lower_deviation: int) -> ToleranceCheck
measuredintthe measured size in micrometres (1 mm = 1000)
nominalintthe nominal (basic) size in micrometres
upper_deviationintupper limit minus nominal: +50 for ±0.05 mm, -7 for a g6 shaft at 25 mm
lower_deviationintlower limit minus nominal: -50 for ±0.05 mm, -20 for a g6 shaft at 25 mm
returnsToleranceCheck

The types it declares, generated into your project

@dataclass(frozen=True)
class ToleranceCheck:
    """The verdict, where the limits are, and how far out a reject is."""

    #: true when lowerLimit <= measured <= upperLimit; the limits are inside
    passed: bool
    status: ToleranceStatus
    #: measured minus nominal, signed
    deviation: int
    lower_limit: int
    upper_limit: int
    #: distance past the limit that was broken; 0 when passed
    out_by: int

ToleranceStatus = Literal["within", "above", "below"]

Your code names it in one line, in the file that uses it

from fune.manufacturing.tolerance_check import check_tolerance  # manufacturing.tolerance-check@^1
impl/python.py · 44 lines · open · raw
from .manufacturing_tolerance_check_types import ToleranceCheck

LIMIT = 10**12


def _micrometres(name: str, value: int) -> int:
    if isinstance(value, bool) or not isinstance(value, int) or value > LIMIT or value < -LIMIT:
        raise ValueError("%s must be a whole number of micrometres within ±10^12, received %r" % (name, value))
    return value


def check_tolerance(measured: int, nominal: int, upper_deviation: int, lower_deviation: int) -> ToleranceCheck:
    """Check a measurement against nominal plus its upper and lower deviations.

    The deviations are signed and independent, so a tolerance band that lies
    wholly above or below nominal (an H7 hole, a g6 shaft) is checked
    correctly; a symmetric band is just ``+t, -t``.
    """
    _micrometres("measured", measured)
    _micrometres("nominal", nominal)
    _micrometres("upperDeviation", upper_deviation)
    _micrometres("lowerDeviation", lower_deviation)
    if lower_deviation > upper_deviation:
        raise ValueError(
            "lowerDeviation must not be greater than upperDeviation, received %d and %d"
            % (lower_deviation, upper_deviation)
        )
    lower_limit = nominal + lower_deviation
    upper_limit = nominal + upper_deviation
    deviation = measured - nominal
    if measured > upper_limit:
        return ToleranceCheck(
            passed=False, status="above", deviation=deviation,
            lower_limit=lower_limit, upper_limit=upper_limit, out_by=measured - upper_limit,
        )
    if measured < lower_limit:
        return ToleranceCheck(
            passed=False, status="below", deviation=deviation,
            lower_limit=lower_limit, upper_limit=upper_limit, out_by=lower_limit - measured,
        )
    return ToleranceCheck(
        passed=True, status="within", deviation=deviation,
        lower_limit=lower_limit, upper_limit=upper_limit, out_by=0,
    )

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add manufacturing.tolerance-check
Download for Python manufacturing.tolerance-check-1.0.0-python.fune · 9,961 bytes sha256 80fa5c75c71a6b1fc06474c31fc2f3a05fb9fbc145fd46985907d6bc24581df4

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./manufacturing.tolerance-check-1.0.0-python.fune, or fetch it from a terminal with fune pull manufacturing.tolerance-check@1.0.0:python.

The whole function, every language, is one file too: manufacturing.tolerance-check-1.0.0.fune, 14,651 bytes, sha256 02a3faf1c5fd1fc72dfdfa4fe659af7e86632dfe9b9993836204e86a78d84a43. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before manufacturing.tolerance-check

after — your function gets the result and the arguments, and returns the final result.

# fune: after manufacturing.tolerance-check

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show manufacturing.tolerance-check --steps.

# fune: step manufacturing.tolerance-check after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
symmetric ±0.05 mm at 25 mm: 25.030 passes 25,030, 25,000, 50, -50 → passed true, status within, deviation 30, lower limit 24,950, upper limit 25,050, out by 0
exactly on the upper limit passes: limits are inclusive 25,050, 25,000, 50, -50 → passed true, status within, deviation 50, lower limit 24,950, upper limit 25,050, out by 0
one micrometre over the upper limit is a reject 25,051, 25,000, 50, -50 → passed false, status above, deviation 51, lower limit 24,950, upper limit 25,050, out by 1
exactly on the lower limit passes 24,950, 25,000, 50, -50 → passed true, status within, deviation -50, lower limit 24,950, upper limit 25,050, out by 0
under the lower limit is a reject by the distance below it 24,930, 25,000, 50, -50 → passed false, status below, deviation -70, lower limit 24,950, upper limit 25,050, out by 20
a g6 shaft at 25 mm measured at exactly nominal is too big: both limits are below nominal 25,000, 25,000, -7, -20 → passed false, status above, deviation 0, lower limit 24,980, upper limit 24,993, out by 7
a g6 shaft at 24.990 mm is inside 24.980 to 24.993 24,990, 25,000, -7, -20 → passed true, status within, deviation -10, lower limit 24,980, upper limit 24,993, out by 0
an H7 hole at 25 mm: nominal itself is the lower limit and passes 25,000, 25,000, 21, 0 → passed true, status within, deviation 0, lower limit 25,000, upper limit 25,021, out by 0
an H7 hole one micrometre under nominal is a reject 24,999, 25,000, 21, 0 → passed false, status below, deviation -1, lower limit 25,000, upper limit 25,021, out by 1
a zero tolerance accepts only the exact size 10,000, 10,000, 0, 0 → passed true, status within, deviation 0, lower limit 10,000, upper limit 10,000, out by 0
Show the other 4 tests
CaseArgumentsExpected
a negative coordinate from a datum, -12.000 mm +0.1/-0.1, measured -12.150 -12,150, -12,000, 100, -100 → passed false, status below, deviation -150, lower limit -12,100, upper limit -11,900, out by 50
a lower deviation above the upper one is an error 25,000, 25,000, -20, -7 → error: lowerDeviation must not be greater than upperDeviation
a fractional micrometre is an error 25,000.5, 25,000, 50, -50 → error: measured must be a whole number of micrometres
a size beyond 10^12 micrometres is an error 25,000, 1,000,000,000,001, 50, -50 → error: nominal must be a whole number of micrometres within ±10^12

More from the author

**Deviations, not a ± number.** Tolerances are given as the upper and lower deviation from nominal, as ISO 286 writes them (`es`/`ei` for shafts, `ES`/`EI` for holes), so both kinds of limit are one call:

- symmetric, 25 mm ± 0.05: `checkTolerance(m, 25000, 50, -50)` - asymmetric, a 25 mm H7 hole (+0.021 / 0): `checkTolerance(m, 25000, 21, 0)` - one-sided, a 25 mm g6 shaft (-0.007 / -0.020): `checkTolerance(m, 25000, -7, -20)`

The last is the case a "within ±tolerance of nominal" check gets wrong: a g6 shaft measured at exactly 25.000 mm is **too big**, 7 µm above its upper limit, because both limits sit below nominal.

**Whole micrometres.** Every size is an integer number of micrometres (µm), so limits and deviations are exact and all three languages agree; a reading of 25.0125 mm is 25013 µm after the gauge's own rounding. Every value must lie within ±10^12 µm (1,000 km), which keeps every sum exact.

**Limits are inclusive.** A part measured exactly on a limit passes. `outBy` is how far past the broken limit a reject is (always positive for a reject, 0 for a pass), and `status` says which side: `above`, `below` or `within`.

The lower deviation may not be greater than the upper one. Negative sizes are allowed, for coordinates and offsets measured from a datum.

Source: ISO 286-1:2010, Geometrical product specifications (GPS) - ISO code system for tolerances on linear sizes, clause 3 (deviations and limits). The H7 and g6 values for 25 mm are from its tables (IT6 = 13 µm, IT7 = 21 µm, fundamental deviation g = -7 µm for sizes over 18 up to 30 mm).

Files

PathBytes
README.md1,882
impl/python.py1,894
impl/rust.rs2,815
impl/typescript.ts1,671
vectors.json3,014