Spaces:
Sleeping
Sleeping
File size: 74,756 Bytes
334dfdb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 |
# ruff: noqa: F841, SLF001
from __future__ import annotations
import collections
import copy
import datetime as dt
import decimal
import ipaddress
import math
import numbers
import typing
import uuid
import warnings
from collections.abc import Mapping as _Mapping
from marshmallow import class_registry, types, utils, validate
from marshmallow.base import FieldABC
from marshmallow.exceptions import (
FieldInstanceResolutionError,
StringNotCollectionError,
ValidationError,
)
from marshmallow.utils import (
is_aware,
is_collection,
resolve_field_instance,
)
from marshmallow.utils import (
missing as missing_,
)
from marshmallow.validate import And, Length
from marshmallow.warnings import (
ChangedInMarshmallow4Warning,
RemovedInMarshmallow4Warning,
)
if typing.TYPE_CHECKING:
from enum import Enum as EnumType
from marshmallow.schema import Schema, SchemaMeta
__all__ = [
"IP",
"URL",
"UUID",
"AwareDateTime",
"Bool",
"Boolean",
"Constant",
"Date",
"DateTime",
"Decimal",
"Dict",
"Email",
"Enum",
"Field",
"Float",
"Function",
"IPInterface",
"IPv4",
"IPv4Interface",
"IPv6",
"IPv6Interface",
"Int",
"Integer",
"List",
"Mapping",
"Method",
"NaiveDateTime",
"Nested",
"Number",
"Pluck",
"Raw",
"Str",
"String",
"Time",
"TimeDelta",
"Tuple",
"Url",
]
class Field(FieldABC):
"""Base field from which other fields inherit.
:param dump_default: If set, this value will be used during serialization if the
input value is missing. If not set, the field will be excluded from the
serialized output if the input value is missing. May be a value or a callable.
:param load_default: Default deserialization value for the field if the field is not
found in the input data. May be a value or a callable.
:param data_key: The name of the dict key in the external representation, i.e.
the input of `load` and the output of `dump`.
If `None`, the key will match the name of the field.
:param attribute: The name of the key/attribute in the internal representation, i.e.
the output of `load` and the input of `dump`.
If `None`, the key/attribute will match the name of the field.
Note: This should only be used for very specific use cases such as
outputting multiple fields for a single attribute, or using keys/attributes
that are invalid variable names, unsuitable for field names. In most cases,
you should use ``data_key`` instead.
:param validate: Validator or collection of validators that are called
during deserialization. Validator takes a field's input value as
its only parameter and returns a boolean.
If it returns `False`, an :exc:`ValidationError` is raised.
:param required: Raise a :exc:`ValidationError` if the field value
is not supplied during deserialization.
:param allow_none: Set this to `True` if `None` should be considered a valid value during
validation/deserialization. If set to `False` (the default), `None` is considered invalid input.
If ``load_default`` is explicitly set to `None` and ``allow_none`` is unset,
`allow_none` is implicitly set to ``True``.
:param load_only: If `True` skip this field during serialization, otherwise
its value will be present in the serialized data.
:param dump_only: If `True` skip this field during deserialization, otherwise
its value will be present in the deserialized object. In the context of an
HTTP API, this effectively marks the field as "read-only".
:param error_messages: Overrides for `Field.default_error_messages`.
:param metadata: Extra information to be stored as field metadata.
.. versionchanged:: 3.0.0b8
Add ``data_key`` parameter for the specifying the key in the input and
output data. This parameter replaced both ``load_from`` and ``dump_to``.
.. versionchanged:: 3.13.0
Replace ``missing`` and ``default`` parameters with ``load_default`` and ``dump_default``.
.. versionchanged:: 3.24.0
`Field <marshmallow.fields.Field>` should no longer be used as a field within a `Schema <marshmallow.Schema>`.
Use `Raw <marshmallow.fields.Raw>` or another `Field <marshmallow.fields.Field>` subclass instead.
"""
# Some fields, such as Method fields and Function fields, are not expected
# to exist as attributes on the objects to serialize. Set this to False
# for those fields
_CHECK_ATTRIBUTE = True
#: Default error messages for various kinds of errors. The keys in this dictionary
#: are passed to `Field.make_error`. The values are error messages passed to
#: :exc:`marshmallow.exceptions.ValidationError`.
default_error_messages: dict[str, str] = {
"required": "Missing data for required field.",
"null": "Field may not be null.",
"validator_failed": "Invalid value.",
}
def __init__(
self,
*,
load_default: typing.Any = missing_,
missing: typing.Any = missing_,
dump_default: typing.Any = missing_,
default: typing.Any = missing_,
data_key: str | None = None,
attribute: str | None = None,
validate: types.Validator | typing.Iterable[types.Validator] | None = None,
required: bool = False,
allow_none: bool | None = None,
load_only: bool = False,
dump_only: bool = False,
error_messages: dict[str, str] | None = None,
metadata: typing.Mapping[str, typing.Any] | None = None,
**additional_metadata,
) -> None:
if self.__class__ is Field:
warnings.warn(
"`Field` should not be instantiated. Use `fields.Raw` or "
"another field subclass instead.",
ChangedInMarshmallow4Warning,
stacklevel=2,
)
# handle deprecated `default` and `missing` parameters
if default is not missing_:
warnings.warn(
"The 'default' argument to fields is deprecated. "
"Use 'dump_default' instead.",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
if dump_default is missing_:
dump_default = default
if missing is not missing_:
warnings.warn(
"The 'missing' argument to fields is deprecated. "
"Use 'load_default' instead.",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
if load_default is missing_:
load_default = missing
self.dump_default = dump_default
self.load_default = load_default
self.attribute = attribute
self.data_key = data_key
self.validate = validate
if validate is None:
self.validators = []
elif callable(validate):
self.validators = [validate]
elif utils.is_iterable_but_not_string(validate):
self.validators = list(validate)
else:
raise ValueError(
"The 'validate' parameter must be a callable "
"or a collection of callables."
)
# If allow_none is None and load_default is None
# None should be considered valid by default
self.allow_none = load_default is None if allow_none is None else allow_none
self.load_only = load_only
self.dump_only = dump_only
if required is True and load_default is not missing_:
raise ValueError("'load_default' must not be set for required fields.")
self.required = required
metadata = metadata or {}
self.metadata = {**metadata, **additional_metadata}
if additional_metadata:
warnings.warn(
"Passing field metadata as keyword arguments is deprecated. Use the "
"explicit `metadata=...` argument instead. "
f"Additional metadata: {additional_metadata}",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
# Collect default error message from self and parent classes
messages: dict[str, str] = {}
for cls in reversed(self.__class__.__mro__):
messages.update(getattr(cls, "default_error_messages", {}))
messages.update(error_messages or {})
self.error_messages = messages
self.parent: Field | Schema | None = None
self.name: str | None = None
self.root: Schema | None = None
def __repr__(self) -> str:
return (
f"<fields.{self.__class__.__name__}(dump_default={self.dump_default!r}, "
f"attribute={self.attribute!r}, "
f"validate={self.validate}, required={self.required}, "
f"load_only={self.load_only}, dump_only={self.dump_only}, "
f"load_default={self.load_default}, allow_none={self.allow_none}, "
f"error_messages={self.error_messages})>"
)
def __deepcopy__(self, memo):
return copy.copy(self)
def get_value(
self,
obj: typing.Any,
attr: str,
accessor: (
typing.Callable[[typing.Any, str, typing.Any], typing.Any] | None
) = None,
default: typing.Any = missing_,
):
"""Return the value for a given key from an object.
:param obj: The object to get the value from.
:param attr: The attribute/key in `obj` to get the value from.
:param accessor: A callable used to retrieve the value of `attr` from
the object `obj`. Defaults to `marshmallow.utils.get_value`.
"""
accessor_func = accessor or utils.get_value
check_key = attr if self.attribute is None else self.attribute
return accessor_func(obj, check_key, default)
def _validate(self, value: typing.Any):
"""Perform validation on ``value``. Raise a :exc:`ValidationError` if validation
does not succeed.
"""
self._validate_all(value)
@property
def _validate_all(self) -> typing.Callable[[typing.Any], None]:
return And(*self.validators, error=self.error_messages["validator_failed"])
def make_error(self, key: str, **kwargs) -> ValidationError:
"""Helper method to make a `ValidationError` with an error message
from ``self.error_messages``.
"""
try:
msg = self.error_messages[key]
except KeyError as error:
class_name = self.__class__.__name__
message = (
f"ValidationError raised by `{class_name}`, but error key `{key}` does "
"not exist in the `error_messages` dictionary."
)
raise AssertionError(message) from error
if isinstance(msg, (str, bytes)):
msg = msg.format(**kwargs)
return ValidationError(msg)
def fail(self, key: str, **kwargs):
"""Helper method that raises a `ValidationError` with an error message
from ``self.error_messages``.
.. deprecated:: 3.0.0
Use `make_error <marshmallow.fields.Field.make_error>` instead.
"""
warnings.warn(
f'`Field.fail` is deprecated. Use `raise self.make_error("{key}", ...)` instead.',
RemovedInMarshmallow4Warning,
stacklevel=2,
)
raise self.make_error(key=key, **kwargs)
def _validate_missing(self, value: typing.Any) -> None:
"""Validate missing values. Raise a :exc:`ValidationError` if
`value` should be considered missing.
"""
if value is missing_ and self.required:
raise self.make_error("required")
if value is None and not self.allow_none:
raise self.make_error("null")
def serialize(
self,
attr: str,
obj: typing.Any,
accessor: (
typing.Callable[[typing.Any, str, typing.Any], typing.Any] | None
) = None,
**kwargs,
):
"""Pulls the value for the given key from the object, applies the
field's formatting and returns the result.
:param attr: The attribute/key to get from the object.
:param obj: The object to access the attribute/key from.
:param accessor: Function used to access values from ``obj``.
:param kwargs: Field-specific keyword arguments.
"""
if self._CHECK_ATTRIBUTE:
value = self.get_value(obj, attr, accessor=accessor)
if value is missing_:
default = self.dump_default
value = default() if callable(default) else default
if value is missing_:
return value
else:
value = None
return self._serialize(value, attr, obj, **kwargs)
def deserialize(
self,
value: typing.Any,
attr: str | None = None,
data: typing.Mapping[str, typing.Any] | None = None,
**kwargs,
):
"""Deserialize ``value``.
:param value: The value to deserialize.
:param attr: The attribute/key in `data` to deserialize.
:param data: The raw input data passed to `Schema.load <marshmallow.Schema.load>`.
:param kwargs: Field-specific keyword arguments.
:raise ValidationError: If an invalid value is passed or if a required value
is missing.
"""
# Validate required fields, deserialize, then validate
# deserialized value
self._validate_missing(value)
if value is missing_:
_miss = self.load_default
return _miss() if callable(_miss) else _miss
if self.allow_none and value is None:
return None
output = self._deserialize(value, attr, data, **kwargs)
self._validate(output)
return output
# Methods for concrete classes to override.
def _bind_to_schema(self, field_name: str, schema: Schema | Field) -> None:
"""Update field with values from its parent schema. Called by
`Schema._bind_field <marshmallow.Schema._bind_field>`.
:param field_name: Field name set in schema.
:param schema: Parent object.
"""
self.parent = self.parent or schema
self.name = self.name or field_name
self.root = self.root or (
self.parent.root if isinstance(self.parent, FieldABC) else self.parent
)
def _serialize(
self, value: typing.Any, attr: str | None, obj: typing.Any, **kwargs
) -> typing.Any:
"""Serializes ``value`` to a basic Python datatype. Noop by default.
Concrete :class:`Field` classes should implement this method.
Example: ::
class TitleCase(Field):
def _serialize(self, value, attr, obj, **kwargs):
if not value:
return ""
return str(value).title()
:param value: The value to be serialized.
:param attr: The attribute or key on the object to be serialized.
:param obj: The object the value was pulled from.
:param kwargs: Field-specific keyword arguments.
:return: The serialized value
"""
return value
def _deserialize(
self,
value: typing.Any,
attr: str | None,
data: typing.Mapping[str, typing.Any] | None,
**kwargs,
) -> typing.Any:
"""Deserialize value. Concrete :class:`Field` classes should implement this method.
:param value: The value to be deserialized.
:param attr: The attribute/key in `data` to be deserialized.
:param data: The raw input data passed to the `Schema.load <marshmallow.Schema.load>`.
:param kwargs: Field-specific keyword arguments.
:raise ValidationError: In case of formatting or validation failure.
:return: The deserialized value.
.. versionchanged:: 3.0.0
Added ``**kwargs`` to signature.
"""
return value
# Properties
@property
def context(self) -> dict | None:
"""The context dictionary for the parent `Schema <marshmallow.Schema>`."""
if self.parent:
return self.parent.context
return None
# the default and missing properties are provided for compatibility and
# emit warnings when they are accessed and set
@property
def default(self):
warnings.warn(
"The 'default' attribute of fields is deprecated. "
"Use 'dump_default' instead.",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
return self.dump_default
@default.setter
def default(self, value):
warnings.warn(
"The 'default' attribute of fields is deprecated. "
"Use 'dump_default' instead.",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
self.dump_default = value
@property
def missing(self):
warnings.warn(
"The 'missing' attribute of fields is deprecated. "
"Use 'load_default' instead.",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
return self.load_default
@missing.setter
def missing(self, value):
warnings.warn(
"The 'missing' attribute of fields is deprecated. "
"Use 'load_default' instead.",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
self.load_default = value
class Raw(Field):
"""Field that applies no formatting."""
class Nested(Field):
"""Allows you to nest a :class:`Schema <marshmallow.Schema>`
inside a field.
Examples: ::
class ChildSchema(Schema):
id = fields.Str()
name = fields.Str()
# Use lambda functions when you need two-way nesting or self-nesting
parent = fields.Nested(lambda: ParentSchema(only=("id",)), dump_only=True)
siblings = fields.List(
fields.Nested(lambda: ChildSchema(only=("id", "name")))
)
class ParentSchema(Schema):
id = fields.Str()
children = fields.List(
fields.Nested(ChildSchema(only=("id", "parent", "siblings")))
)
spouse = fields.Nested(lambda: ParentSchema(only=("id",)))
When passing a `Schema <marshmallow.Schema>` instance as the first argument,
the instance's ``exclude``, ``only``, and ``many`` attributes will be respected.
Therefore, when passing the ``exclude``, ``only``, or ``many`` arguments to `fields.Nested`,
you should pass a `Schema <marshmallow.Schema>` class (not an instance) as the first argument.
::
# Yes
author = fields.Nested(UserSchema, only=("id", "name"))
# No
author = fields.Nested(UserSchema(), only=("id", "name"))
:param nested: `Schema <marshmallow.Schema>` instance, class, class name (string), dictionary, or callable that
returns a `Schema <marshmallow.Schema>` or dictionary.
Dictionaries are converted with `Schema.from_dict <marshmallow.Schema.from_dict>`.
:param exclude: A list or tuple of fields to exclude.
:param only: A list or tuple of fields to marshal. If `None`, all fields are marshalled.
This parameter takes precedence over ``exclude``.
:param many: Whether the field is a collection of objects.
:param unknown: Whether to exclude, include, or raise an error for unknown
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
:param kwargs: The same keyword arguments that :class:`Field` receives.
"""
#: Default error messages.
default_error_messages = {"type": "Invalid type."}
def __init__(
self,
nested: (
Schema
| SchemaMeta
| str
| dict[str, Field]
| typing.Callable[[], Schema | SchemaMeta | dict[str, Field]]
),
*,
dump_default: typing.Any = missing_,
default: typing.Any = missing_,
only: types.StrSequenceOrSet | None = None,
exclude: types.StrSequenceOrSet = (),
many: bool = False,
unknown: str | None = None,
**kwargs,
):
# Raise error if only or exclude is passed as string, not list of strings
if only is not None and not is_collection(only):
raise StringNotCollectionError('"only" should be a collection of strings.')
if not is_collection(exclude):
raise StringNotCollectionError(
'"exclude" should be a collection of strings.'
)
if nested == "self":
warnings.warn(
"Passing 'self' to `Nested` is deprecated. "
"Use `Nested(lambda: MySchema(...))` instead.",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
self.nested = nested
self.only = only
self.exclude = exclude
self.many = many
self.unknown = unknown
self._schema: Schema | None = None # Cached Schema instance
super().__init__(default=default, dump_default=dump_default, **kwargs)
@property
def schema(self) -> Schema:
"""The nested `Schema <marshmallow.Schema>` object.
.. versionchanged:: 1.0.0
Renamed from ``serializer`` to ``schema``.
"""
if not self._schema:
# Inherit context from parent.
context = getattr(self.parent, "context", {})
if callable(self.nested) and not isinstance(self.nested, type):
nested = self.nested()
else:
nested = typing.cast("Schema", self.nested)
# defer the import of `marshmallow.schema` to avoid circular imports
from marshmallow.schema import Schema
if isinstance(nested, dict):
nested = Schema.from_dict(nested)
if isinstance(nested, Schema):
self._schema = copy.copy(nested)
self._schema.context.update(context)
# Respect only and exclude passed from parent and re-initialize fields
set_class = typing.cast(type[set], self._schema.set_class)
if self.only is not None:
if self._schema.only is not None:
original = self._schema.only
else: # only=None -> all fields
original = self._schema.fields.keys()
self._schema.only = set_class(self.only) & set_class(original)
if self.exclude:
original = self._schema.exclude
self._schema.exclude = set_class(self.exclude) | set_class(original)
self._schema._init_fields()
else:
if isinstance(nested, type) and issubclass(nested, Schema):
schema_class: type[Schema] = nested
elif not isinstance(nested, (str, bytes)):
raise ValueError(
"`Nested` fields must be passed a "
f"`Schema`, not {nested.__class__}."
)
elif nested == "self":
schema_class = typing.cast(Schema, self.root).__class__
else:
schema_class = class_registry.get_class(nested, all=False)
self._schema = schema_class(
many=self.many,
only=self.only,
exclude=self.exclude,
context=context,
load_only=self._nested_normalized_option("load_only"),
dump_only=self._nested_normalized_option("dump_only"),
)
return self._schema
def _nested_normalized_option(self, option_name: str) -> list[str]:
nested_field = f"{self.name}."
return [
field.split(nested_field, 1)[1]
for field in getattr(self.root, option_name, set())
if field.startswith(nested_field)
]
def _serialize(self, nested_obj, attr, obj, **kwargs):
# Load up the schema first. This allows a RegistryError to be raised
# if an invalid schema name was passed
schema = self.schema
if nested_obj is None:
return None
many = schema.many or self.many
return schema.dump(nested_obj, many=many)
def _test_collection(self, value: typing.Any) -> None:
many = self.schema.many or self.many
if many and not utils.is_collection(value):
raise self.make_error("type", input=value, type=value.__class__.__name__)
def _load(
self, value: typing.Any, partial: bool | types.StrSequenceOrSet | None = None
):
try:
valid_data = self.schema.load(value, unknown=self.unknown, partial=partial)
except ValidationError as error:
raise ValidationError(
error.messages, valid_data=error.valid_data
) from error
return valid_data
def _deserialize(
self,
value: typing.Any,
attr: str | None,
data: typing.Mapping[str, typing.Any] | None,
partial: bool | types.StrSequenceOrSet | None = None,
**kwargs,
) -> typing.Any:
"""Same as :meth:`Field._deserialize` with additional ``partial`` argument.
:param partial: For nested schemas, the ``partial``
parameter passed to `marshmallow.Schema.load`.
.. versionchanged:: 3.0.0
Add ``partial`` parameter.
"""
self._test_collection(value)
return self._load(value, partial=partial)
class Pluck(Nested):
"""Allows you to replace nested data with one of the data's fields.
Example: ::
from marshmallow import Schema, fields
class ArtistSchema(Schema):
id = fields.Int()
name = fields.Str()
class AlbumSchema(Schema):
artist = fields.Pluck(ArtistSchema, "id")
in_data = {"artist": 42}
loaded = AlbumSchema().load(in_data) # => {'artist': {'id': 42}}
dumped = AlbumSchema().dump(loaded) # => {'artist': 42}
:param nested: The Schema class or class name (string)
to nest, or ``"self"`` to nest the `Schema <marshmallow.Schema>` within itself.
:param field_name: The key to pluck a value from.
:param kwargs: The same keyword arguments that :class:`Nested` receives.
"""
def __init__(
self,
nested: Schema | SchemaMeta | str | typing.Callable[[], Schema],
field_name: str,
*,
many: bool = False,
unknown: str | None = None,
**kwargs,
):
super().__init__(
nested, only=(field_name,), many=many, unknown=unknown, **kwargs
)
self.field_name = field_name
@property
def _field_data_key(self) -> str:
only_field = self.schema.fields[self.field_name]
return only_field.data_key or self.field_name
def _serialize(self, nested_obj, attr, obj, **kwargs):
ret = super()._serialize(nested_obj, attr, obj, **kwargs)
if ret is None:
return None
if self.many:
return utils.pluck(ret, key=self._field_data_key)
return ret[self._field_data_key]
def _deserialize(self, value, attr, data, partial=None, **kwargs):
self._test_collection(value)
if self.many:
value = [{self._field_data_key: v} for v in value]
else:
value = {self._field_data_key: value}
return self._load(value, partial=partial)
class List(Field):
"""A list field, composed with another `Field` class or
instance.
Example: ::
numbers = fields.List(fields.Float())
:param cls_or_instance: A field class or instance.
:param kwargs: The same keyword arguments that :class:`Field` receives.
.. versionchanged:: 3.0.0rc9
Does not serialize scalar values to single-item lists.
"""
#: Default error messages.
default_error_messages = {"invalid": "Not a valid list."}
def __init__(self, cls_or_instance: Field | type[Field], **kwargs):
super().__init__(**kwargs)
try:
self.inner = resolve_field_instance(cls_or_instance)
except FieldInstanceResolutionError as error:
raise ValueError(
"The list elements must be a subclass or instance of "
"marshmallow.base.FieldABC."
) from error
if isinstance(self.inner, Nested):
self.only = self.inner.only
self.exclude = self.inner.exclude
def _bind_to_schema(self, field_name: str, schema: Schema | Field) -> None:
super()._bind_to_schema(field_name, schema)
self.inner = copy.deepcopy(self.inner)
self.inner._bind_to_schema(field_name, self)
if isinstance(self.inner, Nested):
self.inner.only = self.only
self.inner.exclude = self.exclude
def _serialize(self, value, attr, obj, **kwargs) -> list[typing.Any] | None:
if value is None:
return None
return [self.inner._serialize(each, attr, obj, **kwargs) for each in value]
def _deserialize(self, value, attr, data, **kwargs) -> list[typing.Any]:
if not utils.is_collection(value):
raise self.make_error("invalid")
result = []
errors = {}
for idx, each in enumerate(value):
try:
result.append(self.inner.deserialize(each, **kwargs))
except ValidationError as error:
if error.valid_data is not None:
result.append(error.valid_data)
errors.update({idx: error.messages})
if errors:
raise ValidationError(errors, valid_data=result)
return result
class Tuple(Field):
"""A tuple field, composed of a fixed number of other `Field` classes or
instances
Example: ::
row = Tuple((fields.String(), fields.Integer(), fields.Float()))
.. note::
Because of the structured nature of `collections.namedtuple` and
`typing.NamedTuple`, using a Schema within a Nested field for them is
more appropriate than using a `Tuple` field.
:param tuple_fields: An iterable of field classes or
instances.
:param kwargs: The same keyword arguments that :class:`Field` receives.
.. versionadded:: 3.0.0rc4
"""
#: Default error messages.
default_error_messages = {"invalid": "Not a valid tuple."}
def __init__(
self,
tuple_fields: typing.Iterable[Field] | typing.Iterable[type[Field]],
**kwargs,
):
super().__init__(**kwargs)
if not utils.is_collection(tuple_fields):
raise ValueError(
"tuple_fields must be an iterable of Field classes or instances."
)
try:
self.tuple_fields = [
resolve_field_instance(cls_or_instance)
for cls_or_instance in tuple_fields
]
except FieldInstanceResolutionError as error:
raise ValueError(
'Elements of "tuple_fields" must be subclasses or '
"instances of marshmallow.base.FieldABC."
) from error
self.validate_length = Length(equal=len(self.tuple_fields))
def _bind_to_schema(self, field_name: str, schema: Schema | Field) -> None:
super()._bind_to_schema(field_name, schema)
new_tuple_fields = []
for field in self.tuple_fields:
new_field = copy.deepcopy(field)
new_field._bind_to_schema(field_name, self)
new_tuple_fields.append(new_field)
self.tuple_fields = new_tuple_fields
def _serialize(self, value, attr, obj, **kwargs) -> tuple | None:
if value is None:
return None
return tuple(
field._serialize(each, attr, obj, **kwargs)
for field, each in zip(self.tuple_fields, value)
)
def _deserialize(self, value, attr, data, **kwargs) -> tuple:
if not utils.is_collection(value):
raise self.make_error("invalid")
self.validate_length(value)
result = []
errors = {}
for idx, (field, each) in enumerate(zip(self.tuple_fields, value)):
try:
result.append(field.deserialize(each, **kwargs))
except ValidationError as error:
if error.valid_data is not None:
result.append(error.valid_data)
errors.update({idx: error.messages})
if errors:
raise ValidationError(errors, valid_data=result)
return tuple(result)
class String(Field):
"""A string field.
:param kwargs: The same keyword arguments that :class:`Field` receives.
"""
#: Default error messages.
default_error_messages = {
"invalid": "Not a valid string.",
"invalid_utf8": "Not a valid utf-8 string.",
}
def _serialize(self, value, attr, obj, **kwargs) -> str | None:
if value is None:
return None
return utils.ensure_text_type(value)
def _deserialize(self, value, attr, data, **kwargs) -> typing.Any:
if not isinstance(value, (str, bytes)):
raise self.make_error("invalid")
try:
return utils.ensure_text_type(value)
except UnicodeDecodeError as error:
raise self.make_error("invalid_utf8") from error
class UUID(String):
"""A UUID field."""
#: Default error messages.
default_error_messages = {"invalid_uuid": "Not a valid UUID."}
def _validated(self, value) -> uuid.UUID | None:
"""Format the value or raise a :exc:`ValidationError` if an error occurs."""
if value is None:
return None
if isinstance(value, uuid.UUID):
return value
try:
if isinstance(value, bytes) and len(value) == 16:
return uuid.UUID(bytes=value)
return uuid.UUID(value)
except (ValueError, AttributeError, TypeError) as error:
raise self.make_error("invalid_uuid") from error
def _deserialize(self, value, attr, data, **kwargs) -> uuid.UUID | None:
return self._validated(value)
_NumType = typing.TypeVar("_NumType")
class Number(Field, typing.Generic[_NumType]):
"""Base class for number fields.
:param as_string: If `True`, format the serialized value as a string.
:param kwargs: The same keyword arguments that :class:`Field` receives.
.. versionchanged:: 3.24.0
`Number <marshmallow.fields.Number>` should no longer be used as a field within a `Schema <marshmallow.Schema>`.
Use `Integer <marshmallow.fields.Integer>`, `Float <marshmallow.fields.Float>`, or `Decimal <marshmallow.fields.Decimal>` instead.
"""
num_type: type = float
#: Default error messages.
default_error_messages = {
"invalid": "Not a valid number.",
"too_large": "Number too large.",
}
def __init__(self, *, as_string: bool = False, **kwargs):
if self.__class__ is Number:
warnings.warn(
"`Number` field should not be instantiated. Use `Integer`, `Float`, or `Decimal` instead.",
ChangedInMarshmallow4Warning,
stacklevel=2,
)
self.as_string = as_string
super().__init__(**kwargs)
def _format_num(self, value) -> _NumType:
"""Return the number value for value, given this field's `num_type`."""
return self.num_type(value)
def _validated(self, value: typing.Any) -> _NumType:
"""Format the value or raise a :exc:`ValidationError` if an error occurs."""
# (value is True or value is False) is ~5x faster than isinstance(value, bool)
if value is True or value is False:
raise self.make_error("invalid", input=value)
try:
return self._format_num(value)
except (TypeError, ValueError) as error:
raise self.make_error("invalid", input=value) from error
except OverflowError as error:
raise self.make_error("too_large", input=value) from error
def _to_string(self, value: _NumType) -> str:
return str(value)
def _serialize(self, value, attr, obj, **kwargs) -> str | _NumType | None:
"""Return a string if `self.as_string=True`, otherwise return this field's `num_type`."""
if value is None:
return None
ret: _NumType = self._format_num(value)
return self._to_string(ret) if self.as_string else ret
def _deserialize(self, value, attr, data, **kwargs) -> _NumType | None:
return self._validated(value)
class Integer(Number[int]):
"""An integer field.
:param strict: If `True`, only integer types are valid.
Otherwise, any value castable to `int` is valid.
:param kwargs: The same keyword arguments that :class:`Number` receives.
"""
num_type = int
#: Default error messages.
default_error_messages = {"invalid": "Not a valid integer."}
def __init__(self, *, strict: bool = False, **kwargs):
self.strict = strict
super().__init__(**kwargs)
# override Number
def _validated(self, value: typing.Any) -> int:
if self.strict and not isinstance(value, numbers.Integral):
raise self.make_error("invalid", input=value)
return super()._validated(value)
class Float(Number[float]):
"""A double as an IEEE-754 double precision string.
:param allow_nan: If `True`, `NaN`, `Infinity` and `-Infinity` are allowed,
even though they are illegal according to the JSON specification.
:param as_string: If `True`, format the value as a string.
:param kwargs: The same keyword arguments that :class:`Number` receives.
"""
num_type = float
#: Default error messages.
default_error_messages = {
"special": "Special numeric values (nan or infinity) are not permitted."
}
def __init__(self, *, allow_nan: bool = False, as_string: bool = False, **kwargs):
self.allow_nan = allow_nan
super().__init__(as_string=as_string, **kwargs)
def _validated(self, value: typing.Any) -> float:
num = super()._validated(value)
if self.allow_nan is False:
if math.isnan(num) or num == float("inf") or num == float("-inf"):
raise self.make_error("special")
return num
class Decimal(Number[decimal.Decimal]):
"""A field that (de)serializes to the Python ``decimal.Decimal`` type.
It's safe to use when dealing with money values, percentages, ratios
or other numbers where precision is critical.
.. warning::
This field serializes to a `decimal.Decimal` object by default. If you need
to render your data as JSON, keep in mind that the `json` module from the
standard library does not encode `decimal.Decimal`. Therefore, you must use
a JSON library that can handle decimals, such as `simplejson`, or serialize
to a string by passing ``as_string=True``.
.. warning::
If a JSON `float` value is passed to this field for deserialization it will
first be cast to its corresponding `string` value before being deserialized
to a `decimal.Decimal` object. The default `__str__` implementation of the
built-in Python `float` type may apply a destructive transformation upon
its input data and therefore cannot be relied upon to preserve precision.
To avoid this, you can instead pass a JSON `string` to be deserialized
directly.
:param places: How many decimal places to quantize the value. If `None`, does
not quantize the value.
:param rounding: How to round the value during quantize, for example
`decimal.ROUND_UP`. If `None`, uses the rounding value from
the current thread's context.
:param allow_nan: If `True`, `NaN`, `Infinity` and `-Infinity` are allowed,
even though they are illegal according to the JSON specification.
:param as_string: If `True`, serialize to a string instead of a Python
`decimal.Decimal` type.
:param kwargs: The same keyword arguments that :class:`Number` receives.
.. versionadded:: 1.2.0
"""
num_type = decimal.Decimal
#: Default error messages.
default_error_messages = {
"special": "Special numeric values (nan or infinity) are not permitted."
}
def __init__(
self,
places: int | None = None,
rounding: str | None = None,
*,
allow_nan: bool = False,
as_string: bool = False,
**kwargs,
):
self.places = (
decimal.Decimal((0, (1,), -places)) if places is not None else None
)
self.rounding = rounding
self.allow_nan = allow_nan
super().__init__(as_string=as_string, **kwargs)
# override Number
def _format_num(self, value):
num = decimal.Decimal(str(value))
if self.allow_nan:
if num.is_nan():
return decimal.Decimal("NaN") # avoid sNaN, -sNaN and -NaN
if self.places is not None and num.is_finite():
num = num.quantize(self.places, rounding=self.rounding)
return num
# override Number
def _validated(self, value: typing.Any) -> decimal.Decimal:
try:
num = super()._validated(value)
except decimal.InvalidOperation as error:
raise self.make_error("invalid") from error
if not self.allow_nan and (num.is_nan() or num.is_infinite()):
raise self.make_error("special")
return num
# override Number
def _to_string(self, value: decimal.Decimal) -> str:
return format(value, "f")
class Boolean(Field):
"""A boolean field.
:param truthy: Values that will (de)serialize to `True`. If an empty
set, any non-falsy value will deserialize to `True`. If `None`,
`marshmallow.fields.Boolean.truthy` will be used.
:param falsy: Values that will (de)serialize to `False`. If `None`,
`marshmallow.fields.Boolean.falsy` will be used.
:param kwargs: The same keyword arguments that :class:`Field` receives.
"""
#: Default truthy values.
truthy = {
"t",
"T",
"true",
"True",
"TRUE",
"on",
"On",
"ON",
"y",
"Y",
"yes",
"Yes",
"YES",
"1",
1,
# Equal to 1
# True,
}
#: Default falsy values.
falsy = {
"f",
"F",
"false",
"False",
"FALSE",
"off",
"Off",
"OFF",
"n",
"N",
"no",
"No",
"NO",
"0",
0,
# Equal to 0
# 0.0,
# False,
}
#: Default error messages.
default_error_messages = {"invalid": "Not a valid boolean."}
def __init__(
self,
*,
truthy: typing.Iterable | None = None,
falsy: typing.Iterable | None = None,
**kwargs,
):
super().__init__(**kwargs)
if truthy is not None:
self.truthy = set(truthy)
if falsy is not None:
self.falsy = set(falsy)
def _serialize(
self, value: typing.Any, attr: str | None, obj: typing.Any, **kwargs
):
if value is None:
return None
try:
if value in self.truthy:
return True
if value in self.falsy:
return False
except TypeError:
pass
return bool(value)
def _deserialize(self, value, attr, data, **kwargs):
if not self.truthy:
return bool(value)
try:
if value in self.truthy:
return True
if value in self.falsy:
return False
except TypeError as error:
raise self.make_error("invalid", input=value) from error
raise self.make_error("invalid", input=value)
class DateTime(Field):
"""A formatted datetime string.
Example: ``'2014-12-22T03:12:58.019077+00:00'``
:param format: Either ``"rfc"`` (for RFC822), ``"iso"`` (for ISO8601),
``"timestamp"``, ``"timestamp_ms"`` (for a POSIX timestamp) or a date format string.
If `None`, defaults to "iso".
:param kwargs: The same keyword arguments that :class:`Field` receives.
.. versionchanged:: 3.0.0rc9
Does not modify timezone information on (de)serialization.
.. versionchanged:: 3.19
Add timestamp as a format.
"""
SERIALIZATION_FUNCS: dict[str, typing.Callable[[typing.Any], str | float]] = {
"iso": utils.isoformat,
"iso8601": utils.isoformat,
"rfc": utils.rfcformat,
"rfc822": utils.rfcformat,
"timestamp": utils.timestamp,
"timestamp_ms": utils.timestamp_ms,
}
DESERIALIZATION_FUNCS: dict[str, typing.Callable[[str], typing.Any]] = {
"iso": utils.from_iso_datetime,
"iso8601": utils.from_iso_datetime,
"rfc": utils.from_rfc,
"rfc822": utils.from_rfc,
"timestamp": utils.from_timestamp,
"timestamp_ms": utils.from_timestamp_ms,
}
DEFAULT_FORMAT = "iso"
OBJ_TYPE = "datetime"
SCHEMA_OPTS_VAR_NAME = "datetimeformat"
#: Default error messages.
default_error_messages = {
"invalid": "Not a valid {obj_type}.",
"invalid_awareness": "Not a valid {awareness} {obj_type}.",
"format": '"{input}" cannot be formatted as a {obj_type}.',
}
def __init__(self, format: str | None = None, **kwargs) -> None: # noqa: A002
super().__init__(**kwargs)
# Allow this to be None. It may be set later in the ``_serialize``
# or ``_deserialize`` methods. This allows a Schema to dynamically set the
# format, e.g. from a Meta option
self.format = format
def _bind_to_schema(self, field_name, schema):
super()._bind_to_schema(field_name, schema)
self.format = (
self.format
or getattr(self.root.opts, self.SCHEMA_OPTS_VAR_NAME)
or self.DEFAULT_FORMAT
)
def _serialize(self, value, attr, obj, **kwargs) -> str | float | None:
if value is None:
return None
data_format = self.format or self.DEFAULT_FORMAT
format_func = self.SERIALIZATION_FUNCS.get(data_format)
if format_func:
return format_func(value)
return value.strftime(data_format)
def _deserialize(self, value, attr, data, **kwargs) -> dt.datetime:
data_format = self.format or self.DEFAULT_FORMAT
func = self.DESERIALIZATION_FUNCS.get(data_format)
try:
if func:
return func(value)
return self._make_object_from_format(value, data_format)
except (TypeError, AttributeError, ValueError) as error:
raise self.make_error(
"invalid", input=value, obj_type=self.OBJ_TYPE
) from error
@staticmethod
def _make_object_from_format(value, data_format) -> dt.datetime:
return dt.datetime.strptime(value, data_format)
class NaiveDateTime(DateTime):
"""A formatted naive datetime string.
:param format: See :class:`DateTime`.
:param timezone: Used on deserialization. If `None`,
aware datetimes are rejected. If not `None`, aware datetimes are
converted to this timezone before their timezone information is
removed.
:param kwargs: The same keyword arguments that :class:`Field` receives.
.. versionadded:: 3.0.0rc9
"""
AWARENESS = "naive"
def __init__(
self,
format: str | None = None, # noqa: A002
*,
timezone: dt.timezone | None = None,
**kwargs,
) -> None:
super().__init__(format=format, **kwargs)
self.timezone = timezone
def _deserialize(self, value, attr, data, **kwargs) -> dt.datetime:
ret = super()._deserialize(value, attr, data, **kwargs)
if is_aware(ret):
if self.timezone is None:
raise self.make_error(
"invalid_awareness",
awareness=self.AWARENESS,
obj_type=self.OBJ_TYPE,
)
ret = ret.astimezone(self.timezone).replace(tzinfo=None)
return ret
class AwareDateTime(DateTime):
"""A formatted aware datetime string.
:param format: See :class:`DateTime`.
:param default_timezone: Used on deserialization. If `None`, naive
datetimes are rejected. If not `None`, naive datetimes are set this
timezone.
:param kwargs: The same keyword arguments that :class:`Field` receives.
.. versionadded:: 3.0.0rc9
"""
AWARENESS = "aware"
def __init__(
self,
format: str | None = None, # noqa: A002
*,
default_timezone: dt.tzinfo | None = None,
**kwargs,
) -> None:
super().__init__(format=format, **kwargs)
self.default_timezone = default_timezone
def _deserialize(self, value, attr, data, **kwargs) -> dt.datetime:
ret = super()._deserialize(value, attr, data, **kwargs)
if not is_aware(ret):
if self.default_timezone is None:
raise self.make_error(
"invalid_awareness",
awareness=self.AWARENESS,
obj_type=self.OBJ_TYPE,
)
ret = ret.replace(tzinfo=self.default_timezone)
return ret
class Time(DateTime):
"""A formatted time string.
Example: ``'03:12:58.019077'``
:param format: Either ``"iso"`` (for ISO8601) or a date format string.
If `None`, defaults to "iso".
:param kwargs: The same keyword arguments that :class:`Field` receives.
"""
SERIALIZATION_FUNCS = {"iso": utils.to_iso_time, "iso8601": utils.to_iso_time}
DESERIALIZATION_FUNCS = {"iso": utils.from_iso_time, "iso8601": utils.from_iso_time}
DEFAULT_FORMAT = "iso"
OBJ_TYPE = "time"
SCHEMA_OPTS_VAR_NAME = "timeformat"
@staticmethod
def _make_object_from_format(value, data_format):
return dt.datetime.strptime(value, data_format).time()
class Date(DateTime):
"""ISO8601-formatted date string.
:param format: Either ``"iso"`` (for ISO8601) or a date format string.
If `None`, defaults to "iso".
:param kwargs: The same keyword arguments that :class:`Field` receives.
"""
#: Default error messages.
default_error_messages = {
"invalid": "Not a valid date.",
"format": '"{input}" cannot be formatted as a date.',
}
SERIALIZATION_FUNCS = {"iso": utils.to_iso_date, "iso8601": utils.to_iso_date}
DESERIALIZATION_FUNCS = {"iso": utils.from_iso_date, "iso8601": utils.from_iso_date}
DEFAULT_FORMAT = "iso"
OBJ_TYPE = "date"
SCHEMA_OPTS_VAR_NAME = "dateformat"
@staticmethod
def _make_object_from_format(value, data_format):
return dt.datetime.strptime(value, data_format).date()
class TimeDelta(Field):
"""A field that (de)serializes a :class:`datetime.timedelta` object to an
integer or float and vice versa. The integer or float can represent the
number of days, seconds or microseconds.
:param precision: Influences how the integer or float is interpreted during
(de)serialization. Must be 'days', 'seconds', 'microseconds',
'milliseconds', 'minutes', 'hours' or 'weeks'.
:param serialization_type: Whether to (de)serialize to a `int` or `float`.
:param kwargs: The same keyword arguments that :class:`Field` receives.
Integer Caveats
---------------
Any fractional parts (which depends on the precision used) will be truncated
when serializing using `int`.
Float Caveats
-------------
Use of `float` when (de)serializing may result in data precision loss due
to the way machines handle floating point values.
Regardless of the precision chosen, the fractional part when using `float`
will always be truncated to microseconds.
For example, `1.12345` interpreted as microseconds will result in `timedelta(microseconds=1)`.
.. versionchanged:: 3.17.0
Allow (de)serialization to `float` through use of a new `serialization_type` parameter.
`int` is the default to retain previous behaviour.
"""
DAYS = "days"
SECONDS = "seconds"
MICROSECONDS = "microseconds"
MILLISECONDS = "milliseconds"
MINUTES = "minutes"
HOURS = "hours"
WEEKS = "weeks"
#: Default error messages.
default_error_messages = {
"invalid": "Not a valid period of time.",
"format": "{input!r} cannot be formatted as a timedelta.",
}
def __init__(
self,
precision: str = SECONDS,
serialization_type: type[int | float] = int,
**kwargs,
):
precision = precision.lower()
units = (
self.DAYS,
self.SECONDS,
self.MICROSECONDS,
self.MILLISECONDS,
self.MINUTES,
self.HOURS,
self.WEEKS,
)
if precision not in units:
msg = 'The precision must be {} or "{}".'.format(
", ".join([f'"{each}"' for each in units[:-1]]), units[-1]
)
raise ValueError(msg)
if serialization_type not in (int, float):
raise ValueError("The serialization type must be one of int or float")
self.precision = precision
self.serialization_type = serialization_type
super().__init__(**kwargs)
def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return None
base_unit = dt.timedelta(**{self.precision: 1})
if self.serialization_type is int:
delta = utils.timedelta_to_microseconds(value)
unit = utils.timedelta_to_microseconds(base_unit)
return delta // unit
assert self.serialization_type is float # noqa: S101
return value.total_seconds() / base_unit.total_seconds()
def _deserialize(self, value, attr, data, **kwargs):
try:
value = self.serialization_type(value)
except (TypeError, ValueError) as error:
raise self.make_error("invalid") from error
kwargs = {self.precision: value}
try:
return dt.timedelta(**kwargs)
except OverflowError as error:
raise self.make_error("invalid") from error
class Mapping(Field):
"""An abstract class for objects with key-value pairs. This class should not be used within schemas.
:param keys: A field class or instance for dict keys.
:param values: A field class or instance for dict values.
:param kwargs: The same keyword arguments that :class:`Field` receives.
.. note::
When the structure of nested data is not known, you may omit the
`keys` and `values` arguments to prevent content validation.
.. versionadded:: 3.0.0rc4
.. versionchanged:: 3.24.0
`Mapping <marshmallow.fields.Mapping>` should no longer be used as a field within a `Schema <marshmallow.Schema>`.
Use `Dict <marshmallow.fields.Dict>` instead.
"""
mapping_type = dict
#: Default error messages.
default_error_messages = {"invalid": "Not a valid mapping type."}
def __init__(
self,
keys: Field | type[Field] | None = None,
values: Field | type[Field] | None = None,
**kwargs,
):
if self.__class__ is Mapping:
warnings.warn(
"`Mapping` field should not be instantiated. Use `Dict` instead.",
ChangedInMarshmallow4Warning,
stacklevel=2,
)
super().__init__(**kwargs)
if keys is None:
self.key_field = None
else:
try:
self.key_field = resolve_field_instance(keys)
except FieldInstanceResolutionError as error:
raise ValueError(
'"keys" must be a subclass or instance of '
"marshmallow.base.FieldABC."
) from error
if values is None:
self.value_field = None
else:
try:
self.value_field = resolve_field_instance(values)
except FieldInstanceResolutionError as error:
raise ValueError(
'"values" must be a subclass or instance of '
"marshmallow.base.FieldABC."
) from error
if isinstance(self.value_field, Nested):
self.only = self.value_field.only
self.exclude = self.value_field.exclude
def _bind_to_schema(self, field_name, schema):
super()._bind_to_schema(field_name, schema)
if self.value_field:
self.value_field = copy.deepcopy(self.value_field)
self.value_field._bind_to_schema(field_name, self)
if isinstance(self.value_field, Nested):
self.value_field.only = self.only
self.value_field.exclude = self.exclude
if self.key_field:
self.key_field = copy.deepcopy(self.key_field)
self.key_field._bind_to_schema(field_name, self)
def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return None
if not self.value_field and not self.key_field:
return self.mapping_type(value)
# Serialize keys
if self.key_field is None:
keys = {k: k for k in value}
else:
keys = {
k: self.key_field._serialize(k, None, None, **kwargs) for k in value
}
# Serialize values
result = self.mapping_type()
if self.value_field is None:
for k, v in value.items():
if k in keys:
result[keys[k]] = v
else:
for k, v in value.items():
result[keys[k]] = self.value_field._serialize(v, None, None, **kwargs)
return result
def _deserialize(self, value, attr, data, **kwargs):
if not isinstance(value, _Mapping):
raise self.make_error("invalid")
if not self.value_field and not self.key_field:
return self.mapping_type(value)
errors = collections.defaultdict(dict)
# Deserialize keys
if self.key_field is None:
keys = {k: k for k in value}
else:
keys = {}
for key in value:
try:
keys[key] = self.key_field.deserialize(key, **kwargs)
except ValidationError as error:
errors[key]["key"] = error.messages
# Deserialize values
result = self.mapping_type()
if self.value_field is None:
for k, v in value.items():
if k in keys:
result[keys[k]] = v
else:
for key, val in value.items():
try:
deser_val = self.value_field.deserialize(val, **kwargs)
except ValidationError as error:
errors[key]["value"] = error.messages
if error.valid_data is not None and key in keys:
result[keys[key]] = error.valid_data
else:
if key in keys:
result[keys[key]] = deser_val
if errors:
raise ValidationError(errors, valid_data=result)
return result
class Dict(Mapping):
"""A dict field. Supports dicts and dict-like objects. Extends
Mapping with dict as the mapping_type.
Example: ::
numbers = fields.Dict(keys=fields.Str(), values=fields.Float())
:param kwargs: The same keyword arguments that :class:`Mapping` receives.
.. versionadded:: 2.1.0
"""
mapping_type = dict
class Url(String):
"""An URL field.
:param default: Default value for the field if the attribute is not set.
:param relative: Whether to allow relative URLs.
:param absolute: Whether to allow absolute URLs.
:param require_tld: Whether to reject non-FQDN hostnames.
:param schemes: Valid schemes. By default, ``http``, ``https``,
``ftp``, and ``ftps`` are allowed.
:param kwargs: The same keyword arguments that :class:`String` receives.
"""
#: Default error messages.
default_error_messages = {"invalid": "Not a valid URL."}
def __init__(
self,
*,
relative: bool = False,
absolute: bool = True,
schemes: types.StrSequenceOrSet | None = None,
require_tld: bool = True,
**kwargs,
):
super().__init__(**kwargs)
self.relative = relative
self.absolute = absolute
self.require_tld = require_tld
# Insert validation into self.validators so that multiple errors can be stored.
validator = validate.URL(
relative=self.relative,
absolute=self.absolute,
schemes=schemes,
require_tld=self.require_tld,
error=self.error_messages["invalid"],
)
self.validators.insert(0, validator)
class Email(String):
"""An email field.
:param args: The same positional arguments that :class:`String` receives.
:param kwargs: The same keyword arguments that :class:`String` receives.
"""
#: Default error messages.
default_error_messages = {"invalid": "Not a valid email address."}
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
# Insert validation into self.validators so that multiple errors can be stored.
validator = validate.Email(error=self.error_messages["invalid"])
self.validators.insert(0, validator)
class IP(Field):
"""A IP address field.
:param exploded: If `True`, serialize ipv6 address in long form, ie. with groups
consisting entirely of zeros included.
.. versionadded:: 3.8.0
"""
default_error_messages = {"invalid_ip": "Not a valid IP address."}
DESERIALIZATION_CLASS: type | None = None
def __init__(self, *args, exploded=False, **kwargs):
super().__init__(*args, **kwargs)
self.exploded = exploded
def _serialize(self, value, attr, obj, **kwargs) -> str | None:
if value is None:
return None
if self.exploded:
return value.exploded
return value.compressed
def _deserialize(
self, value, attr, data, **kwargs
) -> ipaddress.IPv4Address | ipaddress.IPv6Address | None:
if value is None:
return None
try:
return (self.DESERIALIZATION_CLASS or ipaddress.ip_address)(
utils.ensure_text_type(value)
)
except (ValueError, TypeError) as error:
raise self.make_error("invalid_ip") from error
class IPv4(IP):
"""A IPv4 address field.
.. versionadded:: 3.8.0
"""
default_error_messages = {"invalid_ip": "Not a valid IPv4 address."}
DESERIALIZATION_CLASS = ipaddress.IPv4Address
class IPv6(IP):
"""A IPv6 address field.
.. versionadded:: 3.8.0
"""
default_error_messages = {"invalid_ip": "Not a valid IPv6 address."}
DESERIALIZATION_CLASS = ipaddress.IPv6Address
class IPInterface(Field):
"""A IPInterface field.
IP interface is the non-strict form of the IPNetwork type where arbitrary host
addresses are always accepted.
IPAddress and mask e.g. '192.168.0.2/24' or '192.168.0.2/255.255.255.0'
see https://python.readthedocs.io/en/latest/library/ipaddress.html#interface-objects
:param exploded: If `True`, serialize ipv6 interface in long form, ie. with groups
consisting entirely of zeros included.
"""
default_error_messages = {"invalid_ip_interface": "Not a valid IP interface."}
DESERIALIZATION_CLASS: type | None = None
def __init__(self, *args, exploded: bool = False, **kwargs):
super().__init__(*args, **kwargs)
self.exploded = exploded
def _serialize(self, value, attr, obj, **kwargs) -> str | None:
if value is None:
return None
if self.exploded:
return value.exploded
return value.compressed
def _deserialize(self, value, attr, data, **kwargs) -> None | (
ipaddress.IPv4Interface | ipaddress.IPv6Interface
):
if value is None:
return None
try:
return (self.DESERIALIZATION_CLASS or ipaddress.ip_interface)(
utils.ensure_text_type(value)
)
except (ValueError, TypeError) as error:
raise self.make_error("invalid_ip_interface") from error
class IPv4Interface(IPInterface):
"""A IPv4 Network Interface field."""
default_error_messages = {"invalid_ip_interface": "Not a valid IPv4 interface."}
DESERIALIZATION_CLASS = ipaddress.IPv4Interface
class IPv6Interface(IPInterface):
"""A IPv6 Network Interface field."""
default_error_messages = {"invalid_ip_interface": "Not a valid IPv6 interface."}
DESERIALIZATION_CLASS = ipaddress.IPv6Interface
class Enum(Field):
"""An Enum field (de)serializing enum members by symbol (name) or by value.
:param enum: Enum class
:param by_value: Whether to (de)serialize by value or by name,
or Field class or instance to use to (de)serialize by value. Defaults to False.
If `by_value` is `False` (default), enum members are (de)serialized by symbol (name).
If it is `True`, they are (de)serialized by value using :class:`Raw`.
If it is a field instance or class, they are (de)serialized by value using this field.
.. versionadded:: 3.18.0
"""
default_error_messages = {
"unknown": "Must be one of: {choices}.",
}
def __init__(
self,
enum: type[EnumType],
*,
by_value: bool | Field | type[Field] = False,
**kwargs,
):
super().__init__(**kwargs)
self.enum = enum
self.by_value = by_value
# Serialization by name
if by_value is False:
self.field: Field = String()
self.choices_text = ", ".join(
str(self.field._serialize(m, None, None)) for m in enum.__members__
)
# Serialization by value
else:
if by_value is True:
self.field = Raw()
else:
try:
self.field = resolve_field_instance(by_value)
except FieldInstanceResolutionError as error:
raise ValueError(
'"by_value" must be either a bool or a subclass or instance of '
"marshmallow.base.FieldABC."
) from error
self.choices_text = ", ".join(
str(self.field._serialize(m.value, None, None)) for m in enum
)
def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return None
if self.by_value:
val = value.value
else:
val = value.name
return self.field._serialize(val, attr, obj, **kwargs)
def _deserialize(self, value, attr, data, **kwargs):
val = self.field._deserialize(value, attr, data, **kwargs)
if self.by_value:
try:
return self.enum(val)
except ValueError as error:
raise self.make_error("unknown", choices=self.choices_text) from error
try:
return getattr(self.enum, val)
except AttributeError as error:
raise self.make_error("unknown", choices=self.choices_text) from error
class Method(Field):
"""A field that takes the value returned by a `Schema <marshmallow.Schema>` method.
:param serialize: The name of the Schema method from which
to retrieve the value. The method must take an argument ``obj``
(in addition to self) that is the object to be serialized.
:param deserialize: Optional name of the Schema method for deserializing
a value The method must take a single argument ``value``, which is the
value to deserialize.
.. versionchanged:: 2.3.0
Deprecated ``method_name`` parameter in favor of ``serialize`` and allow
``serialize`` to not be passed at all.
.. versionchanged:: 3.0.0
Removed ``method_name`` parameter.
"""
_CHECK_ATTRIBUTE = False
def __init__(
self,
serialize: str | None = None,
deserialize: str | None = None,
**kwargs,
):
# Set dump_only and load_only based on arguments
kwargs["dump_only"] = bool(serialize) and not bool(deserialize)
kwargs["load_only"] = bool(deserialize) and not bool(serialize)
super().__init__(**kwargs)
self.serialize_method_name = serialize
self.deserialize_method_name = deserialize
self._serialize_method = None
self._deserialize_method = None
def _bind_to_schema(self, field_name, schema):
if self.serialize_method_name:
self._serialize_method = utils.callable_or_raise(
getattr(schema, self.serialize_method_name)
)
if self.deserialize_method_name:
self._deserialize_method = utils.callable_or_raise(
getattr(schema, self.deserialize_method_name)
)
super()._bind_to_schema(field_name, schema)
def _serialize(self, value, attr, obj, **kwargs):
if self._serialize_method is not None:
return self._serialize_method(obj)
return missing_
def _deserialize(self, value, attr, data, **kwargs):
if self._deserialize_method is not None:
return self._deserialize_method(value)
return value
class Function(Field):
"""A field that takes the value returned by a function.
:param serialize: A callable from which to retrieve the value.
The function must take a single argument ``obj`` which is the object
to be serialized. It can also optionally take a ``context`` argument,
which is a dictionary of context variables passed to the serializer.
If no callable is provided then the ```load_only``` flag will be set
to True.
:param deserialize: A callable from which to retrieve the value.
The function must take a single argument ``value`` which is the value
to be deserialized. It can also optionally take a ``context`` argument,
which is a dictionary of context variables passed to the deserializer.
If no callable is provided then ```value``` will be passed through
unchanged.
.. versionchanged:: 2.3.0
Deprecated ``func`` parameter in favor of ``serialize``.
.. versionchanged:: 3.0.0a1
Removed ``func`` parameter.
"""
_CHECK_ATTRIBUTE = False
def __init__(
self,
serialize: (
typing.Callable[[typing.Any], typing.Any]
| typing.Callable[[typing.Any, dict], typing.Any]
| None
) = None,
deserialize: (
typing.Callable[[typing.Any], typing.Any]
| typing.Callable[[typing.Any, dict], typing.Any]
| None
) = None,
**kwargs,
):
# Set dump_only and load_only based on arguments
kwargs["dump_only"] = bool(serialize) and not bool(deserialize)
kwargs["load_only"] = bool(deserialize) and not bool(serialize)
super().__init__(**kwargs)
self.serialize_func = serialize and utils.callable_or_raise(serialize)
self.deserialize_func = deserialize and utils.callable_or_raise(deserialize)
def _serialize(self, value, attr, obj, **kwargs):
return self._call_or_raise(self.serialize_func, obj, attr)
def _deserialize(self, value, attr, data, **kwargs):
if self.deserialize_func:
return self._call_or_raise(self.deserialize_func, value, attr)
return value
def _call_or_raise(self, func, value, attr):
if len(utils.get_func_args(func)) > 1:
if self.parent.context is None:
msg = f"No context available for Function field {attr!r}"
raise ValidationError(msg)
return func(value, self.parent.context)
return func(value)
class Constant(Field):
"""A field that (de)serializes to a preset constant. If you only want the
constant added for serialization or deserialization, you should use
``dump_only=True`` or ``load_only=True`` respectively.
:param constant: The constant to return for the field attribute.
"""
_CHECK_ATTRIBUTE = False
def __init__(self, constant: typing.Any, **kwargs):
super().__init__(**kwargs)
self.constant = constant
self.load_default = constant
self.dump_default = constant
def _serialize(self, value, *args, **kwargs):
return self.constant
def _deserialize(self, value, *args, **kwargs):
return self.constant
class Inferred(Field):
"""A field that infers how to serialize, based on the value type.
.. warning::
This class is treated as private API.
Users should not need to use this class directly.
"""
def __init__(self):
super().__init__()
# We memoize the fields to avoid creating and binding new fields
# every time on serialization.
self._field_cache = {}
def _serialize(self, value, attr, obj, **kwargs):
field_cls = self.root.TYPE_MAPPING.get(type(value))
if field_cls is None:
field = super()
else:
field = self._field_cache.get(field_cls)
if field is None:
field = field_cls()
field._bind_to_schema(self.name, self.parent)
self._field_cache[field_cls] = field
return field._serialize(value, attr, obj, **kwargs)
# Aliases
URL = Url
Str = String
Bool = Boolean
Int = Integer
|