No project description provided
Project description
AstroPydantic
An (unofficial) package providing pydantic typing support for astropy unit types. Can be used to de(serialize) astropy quantities and units.
We provide four types:
AstroPydanticUnit: an overlay forastropy.units.UnitBase(all unit types).AstroPydanticQuantity: an overlay forastropy.units.Quantity, including array types.AstroPydanticTime: an overlay forastropy.time.Time.AstroPydanticICRS: an overlay forastropy.coordinates.ICRSandastropy.coordinates.SkyCoord.
Installation
The package can be installed from pypi:
pip install astropydantic
Example usage
Units
from astropydantic import AstroPydanticUnit
from pydantic import BaseModel
class MyModel(BaseModel):
units: AstroPydanticUnit
my_model = MyModel(units="km")
print(my_model)
>>> units=Unit("km")
my_model.units.to("m")
>>> 1000.0
By default, strings are formatted according to the vounit specification. You can
change this to any supported configuration from astropy using the module-level
constant UNIT_STRING_FORMAT:
import astropydantic
from astropydantic import AstroPydanticUnit
from pydantic import BaseModel
class MyModel(BaseModel):
units: AstroPydanticUnit
my_model = MyModel(units="km / s")
print(my_model.model_dump_json())
>>> {"units":"km.s**-1"}
astropydantic.UNIT_STRING_FORMAT = "fits"
print(my_model.model_dump_json())
>>> {"units":"km s-1"}
Quantities
Regular quantities are supported, and are converted from either strings or
dictionaries specified as {"value": x, "unit": y}:
from astropydantic import AstroPydanticQuantity
from pydantic import BaseModel
class MyModel(BaseModel):
a: AstroPydanticQuantity
b: AstroPydanticQuantity
my_model = MyModel(a="0.1 km", b={"value": [20.0, 30.0], "unit": "A"})
print(my_model)
>>> a=<Quantity 0.1 km> b=<Quantity [20., 30.] A>
You can enforce the type of the quantity by using the indexing syntax:
from astropydantic import AstroPydanticQuantity
from pydantic import BaseModel
from astropy import units
class MyModel(BaseModel):
a: AstroPydanticQuantity[units.m]
my_model = MyModel(a="0.1 km")
print(my_model)
>>> a=<Quantity 0.1 km>
my_model = MyModel(a="10.0 g")
>>> Traceback (most recent call last):
>>> File "<stdin>", line 1, in <module>
>>> my_model = MyModel(a="10.0 g")
>>> File "/Users/borrow-adm/Documents/Projects/astropydantic/.venv/lib/python3.13/site-packages/pydantic/main.py", line 253, in __init__
>>> validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
>>> pydantic_core._pydantic_core.ValidationError: 1 validation error for MyModel
>>> a
>>> Value error, 'g' (mass) and 'm' (length) are not convertible [type=value_error, input_value=<Quantity 10. g>, input_type=Quantity]
>>> For further information visit https://errors.pydantic.dev/2.11/v/value_error
Times
Times are handled similarly, with the output format (either isot_X where X is the precision,
or datetime for the native python datetime) determined by
astropydantic.TIME_OUTPUT_FORMAT:
import astropydantic
import datetime
from pydantic import BaseModel
from astropydantic import AstroPydanticTime
class MyModel(BaseModel):
a: AstroPydanticTime
model = MyModel(a=datetime.datetime.now())
print(model)
>>> a=<Time object: scale='utc' format='datetime' value=2025-08-26 12:24:00.143664>
>>> print(model.model_dump())
{'a': '2025-08-26T12:24:00.143664000'}
print(model.model_dump_json())
>>> {"a":"2025-08-26T12:24:00.143664000"}
astropydantic.TIME_OUTPUT_FORMAT = "datetime"
print(model.model_dump())
>>> {'a': datetime.datetime(2025, 8, 26, 12, 24, 0, 143664)}
print(model.model_dump_json())
>>> {"a":"2025-08-26T12:24:00.143664"}
The string format defaults to isot_9.
Coordinates
Added in 0.0.3, Coordinates are handled through the general ICRS or
SkyCoord interface. So far, only the icrs frame is supported. You can
instantiate an AstroPydanticICRS either directly with an ICRS object, or a
SkyCoord(frame="icrs") object. However, after serialization, all values are
represented internally as ICRS objects. The individual ra and dec
components of ICRS are internally treated as AstroPydanticQuantity
objects, with matching serialization rules.
from astropy.coordinates import SkyCoord, ICRS
from astropy import units as u
from pydantic import BaseModel
from astropydantic import AstroPydanticICRS
class MyModel(BaseModel):
a: AstroPydanticICRS
sky_coord = SkyCoord(ra=20.0, dec=10.0, unit="deg")
icrs = ICRS(ra=5.0 * u.deg, dec=-5.0 * u.deg)
model = MyModel(a=sky_coord)
print(model)
>>> a=<ICRS Coordinate: (ra, dec) in deg
(20., 10.)>
model = MyModel(a=icrs)
print(model)
>>> a=<ICRS Coordinate: (ra, dec) in deg
(5., -5.)>
print(model.model_dump())
>>> {'a': <ICRS Coordinate: (ra, dec) in deg
(5., -5.)>}
print(model.model_dump_json())
>>> {"a":{"ra":{"value":5.0,"unit":"deg"},"dec":{"value":-5.0,"unit":"deg"}}}
An important note here is that if you supply points in a frame that
is transformable to ICRS as a SkyCoord, we internally convert them
to the ICRS frame. For example, following on from the above:
fk4_coord = SkyCoord(ra=20.0, dec=10.0, unit="deg", frame="fk4")
model = MyModel(a=fk4_coord)
print(model)
>>> a=<ICRS Coordinate: (ra, dec) in deg
(20.65792843, 10.26105602)
If a coordinate frame is not convertible, we raise a TypeError.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file astropydantic-0.0.5.tar.gz.
File metadata
- Download URL: astropydantic-0.0.5.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0376f9d6c93b31d3fe7498ad6e92971fd4fbca5c66cdb2913954eec198f6fa6
|
|
| MD5 |
799c6d7f8887463b5e1523d9330bb6e1
|
|
| BLAKE2b-256 |
14629deebe98af11f697e29684b9b7eddf984a5eac04ce9971eca43f72ed4c05
|
Provenance
The following attestation bundles were made for astropydantic-0.0.5.tar.gz:
Publisher:
pypi.yml on JBorrow/astropydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
astropydantic-0.0.5.tar.gz -
Subject digest:
e0376f9d6c93b31d3fe7498ad6e92971fd4fbca5c66cdb2913954eec198f6fa6 - Sigstore transparency entry: 699978534
- Sigstore integration time:
-
Permalink:
JBorrow/astropydantic@0878a8642ec7f41ef899cc08d1d6b3f8895c7733 -
Branch / Tag:
refs/tags/0.0.5 - Owner: https://github.com/JBorrow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@0878a8642ec7f41ef899cc08d1d6b3f8895c7733 -
Trigger Event:
push
-
Statement type:
File details
Details for the file astropydantic-0.0.5-py3-none-any.whl.
File metadata
- Download URL: astropydantic-0.0.5-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2bcb2d4ee96fa0032332737625480099c9ece8754465c2ed9f9db8f11e3da38c
|
|
| MD5 |
bfa376365404e7f960e6c19f459ae4d6
|
|
| BLAKE2b-256 |
6c11d54594b2be5ea68a1c8c67567278f15a190bcbde743eef041d49dd39c542
|
Provenance
The following attestation bundles were made for astropydantic-0.0.5-py3-none-any.whl:
Publisher:
pypi.yml on JBorrow/astropydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
astropydantic-0.0.5-py3-none-any.whl -
Subject digest:
2bcb2d4ee96fa0032332737625480099c9ece8754465c2ed9f9db8f11e3da38c - Sigstore transparency entry: 699978536
- Sigstore integration time:
-
Permalink:
JBorrow/astropydantic@0878a8642ec7f41ef899cc08d1d6b3f8895c7733 -
Branch / Tag:
refs/tags/0.0.5 - Owner: https://github.com/JBorrow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@0878a8642ec7f41ef899cc08d1d6b3f8895c7733 -
Trigger Event:
push
-
Statement type: