Skip to main content

Official HMRC exchange rates (monthly, spot, yearly average, weekly) with bundled history and exact GBP conversion

Project description

hmrc-rates

Official HMRC exchange rates for Python, backed by the hmrc-rates Rust crate. The full published history ships inside the wheel, so Rates() is free and needs no network, parsing or I/O. All money values are decimal.Decimal and conversions are exact — the library never rounds.

Install

python3 -m pip install hmrc-rates

Python ≥ 3.10. Wheels ship for Linux (glibc and musl, x86_64 and aarch64), macOS and Windows. The sdist builds anywhere with a Rust toolchain.

Quick start

import datetime
from decimal import Decimal

from hmrc_rates import YearMonth, Rates, YearEnd

rates = Rates()

# Monthly customs/VAT rate; months can be YearMonth, datetime.date, or "YYYY-MM"
rate = rates.monthly_rate("USD", YearMonth(2025, 8))
gbp = rate.to_gbp(Decimal("2500"))  # exact — you choose the rounding
print(f"$2500 in Aug 2025 = £{gbp.quantize(Decimal('0.01'))}")

# Self Assessment style: the yearly average to 31 March 2025
eur = rates.average(YearEnd.march(2025)).rate("EUR")
print(f"EUR average, year to 31 Mar 2025: {eur.units_per_gbp}")

# Whole tables iterate as (Currency, Rate) pairs
table = rates.monthly(datetime.date(2025, 8, 15))
print(f"{len(table)} currencies in {table.period}")

"GBP" always resolves to the identity rate, and code lookups are case-insensitive.

Data coverage

One Rates value holds all four series HMRC has published:

Series Coverage Lookup
Monthly customs/VAT 2014-02 — present, no gaps monthly_rate(code, month)
Spot Dec 2010 — present, years ending 31 Mar / 31 Dec spot(year_end)
Yearly average Dec 2010 — present, years ending 31 Mar / 31 Dec average(year_end)
Weekly amendments 2014-01 — 2016-04, complete (discontinued by HMRC) weekly(date)

Currency codes are as published by HMRC, which is not always ISO 4217. E.g. Ecuador appears as ECS.

Strict lookups and exceptions

A period HMRC never published is an exception, never a silently substituted older rate. Every exception derives from HmrcRatesError (not the builtin LookupError):

  • UnknownCurrencyError — the code never appears in that series
  • PeriodNotAvailableError — no table for that period (the message includes the available range)
  • NotInPeriodError — the period exists but the currency is absent from it
  • FetchError — fetching fresh rates failed

Fallback is opt-in and visible:

rate = rates.monthly_rate_or_earlier("USD", month, 2)
rate.period  # reveals which month was actually used

Exactness

Amounts must be decimal.Decimal or int: floats raise TypeError because they are inexact. Results come back as decimal.Decimal, unrounded. Apply whatever rounding your tax context requires.

Fresh rates

Updater fetches whatever HMRC has published since this release and caches the files verbatim in the system cache directory. refreshed() performs blocking HTTP with the GIL released. cached() never touches the network.

from hmrc_rates import FetchError, Updater

updater = Updater()
try:
    rates = updater.refreshed()
except FetchError as e:
    print(f"warning: possibly stale rates: {e}")
    rates = updater.cached()  # offline fallback is an explicit choice

New HMRC publications also trigger a new patch release of this package automatically. So pinning the latest version is an alternative to runtime fetching.

Development

cd python
uv venv && uv pip install maturin pytest
VIRTUAL_ENV=$PWD/.venv .venv/bin/maturin develop
.venv/bin/pytest

The package is a PyO3 binding built with Maturin. The Rust sources live in the repository root.

Licence

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

hmrc_rates-0.3.0.tar.gz (682.6 kB view details)

Uploaded Source

Built Distributions

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

hmrc_rates-0.3.0-cp310-abi3-win_amd64.whl (1.4 MB view details)

Uploaded CPython 3.10+Windows x86-64

hmrc_rates-0.3.0-cp310-abi3-musllinux_1_2_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

hmrc_rates-0.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

hmrc_rates-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

hmrc_rates-0.3.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (2.8 MB view details)

Uploaded CPython 3.10+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file hmrc_rates-0.3.0.tar.gz.

File metadata

  • Download URL: hmrc_rates-0.3.0.tar.gz
  • Upload date:
  • Size: 682.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hmrc_rates-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5c640d92fde92125b257ded0ee418a4b42c694e8f79bcddb3a4ab90521479ef5
MD5 9a25f6cf2ab5dc6784827269f6ce0801
BLAKE2b-256 98e6fd795bd1fd068271447cb6b7547392c3f0261328cb9ee57769d36da8c09c

See more details on using hashes here.

Provenance

The following attestation bundles were made for hmrc_rates-0.3.0.tar.gz:

Publisher: python.yaml on velikodniy/hmrc-rates

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hmrc_rates-0.3.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: hmrc_rates-0.3.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hmrc_rates-0.3.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b673ff15f525eb6476572a2fabd25f7c5e65dca72773127e50ef09345efe9d65
MD5 36d564388bc65602317e2b67f0855263
BLAKE2b-256 d4f770963d9338bdc4cc4f1c16a28c057dcbc7a3b70339836ff2aafabcf98cdc

See more details on using hashes here.

Provenance

The following attestation bundles were made for hmrc_rates-0.3.0-cp310-abi3-win_amd64.whl:

Publisher: python.yaml on velikodniy/hmrc-rates

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hmrc_rates-0.3.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for hmrc_rates-0.3.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b68157627126e78539699dabee780c1f093a913dafa60fb29cd2a5133542fa28
MD5 c8b47768969fb19137483f10d7029d41
BLAKE2b-256 51d743e1b79919c3c3305efb826f97faedc4fda9c138c2b4c87790ad596a9a20

See more details on using hashes here.

Provenance

The following attestation bundles were made for hmrc_rates-0.3.0-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: python.yaml on velikodniy/hmrc-rates

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hmrc_rates-0.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for hmrc_rates-0.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c93df26a8924609a23b6037ee9a938a514373392a38ecc1fb2c55edc38168ac5
MD5 a7a95b876f8b9aced74bb795060cf0c8
BLAKE2b-256 02eded2c5f44e7b0c57d7d069f65644f93b30ac39aed2acb9ce78074fcdf8d2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for hmrc_rates-0.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python.yaml on velikodniy/hmrc-rates

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hmrc_rates-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for hmrc_rates-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d19773563e97350810a8c23132742c101285ed6d95293b018adc3dac54e4c027
MD5 7ea078a66ec72892b7ff4b4734a0d03c
BLAKE2b-256 cdbbe1ce3be4f4b99efe1a59738bc08827278c22e08ff26084c9ee75eee40992

See more details on using hashes here.

Provenance

The following attestation bundles were made for hmrc_rates-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: python.yaml on velikodniy/hmrc-rates

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hmrc_rates-0.3.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for hmrc_rates-0.3.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 6a540265c967811f3251a675e5292f5025d582d798887a2d0ac838b8d4dd8746
MD5 f40faf4ee2e5ad3cee7b25be55921134
BLAKE2b-256 8cf66360fc65ff51e0ed6378c2bd37a00a4297e654ef4e2e7b3f5a54810eb435

See more details on using hashes here.

Provenance

The following attestation bundles were made for hmrc_rates-0.3.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: python.yaml on velikodniy/hmrc-rates

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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