File size: 41,446 Bytes
9c6594c |
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 |
"""The networks module contains types for common network-related fields."""
from __future__ import annotations as _annotations
import dataclasses as _dataclasses
import re
from dataclasses import fields
from functools import lru_cache
from importlib.metadata import version
from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
from typing import TYPE_CHECKING, Annotated, Any, ClassVar
from pydantic_core import (
MultiHostHost,
PydanticCustomError,
PydanticSerializationUnexpectedValue,
SchemaSerializer,
core_schema,
)
from pydantic_core import MultiHostUrl as _CoreMultiHostUrl
from pydantic_core import Url as _CoreUrl
from typing_extensions import Self, TypeAlias
from pydantic.errors import PydanticUserError
from ._internal import _repr, _schema_generation_shared
from ._migration import getattr_migration
from .annotated_handlers import GetCoreSchemaHandler
from .json_schema import JsonSchemaValue
from .type_adapter import TypeAdapter
if TYPE_CHECKING:
import email_validator
NetworkType: TypeAlias = 'str | bytes | int | tuple[str | bytes | int, str | int]'
else:
email_validator = None
__all__ = [
'AnyUrl',
'AnyHttpUrl',
'FileUrl',
'FtpUrl',
'HttpUrl',
'WebsocketUrl',
'AnyWebsocketUrl',
'UrlConstraints',
'EmailStr',
'NameEmail',
'IPvAnyAddress',
'IPvAnyInterface',
'IPvAnyNetwork',
'PostgresDsn',
'CockroachDsn',
'AmqpDsn',
'RedisDsn',
'MongoDsn',
'KafkaDsn',
'NatsDsn',
'validate_email',
'MySQLDsn',
'MariaDBDsn',
'ClickHouseDsn',
'SnowflakeDsn',
]
@_dataclasses.dataclass
class UrlConstraints:
"""Url constraints.
Attributes:
max_length: The maximum length of the url. Defaults to `None`.
allowed_schemes: The allowed schemes. Defaults to `None`.
host_required: Whether the host is required. Defaults to `None`.
default_host: The default host. Defaults to `None`.
default_port: The default port. Defaults to `None`.
default_path: The default path. Defaults to `None`.
"""
max_length: int | None = None
allowed_schemes: list[str] | None = None
host_required: bool | None = None
default_host: str | None = None
default_port: int | None = None
default_path: str | None = None
def __hash__(self) -> int:
return hash(
(
self.max_length,
tuple(self.allowed_schemes) if self.allowed_schemes is not None else None,
self.host_required,
self.default_host,
self.default_port,
self.default_path,
)
)
@property
def defined_constraints(self) -> dict[str, Any]:
"""Fetch a key / value mapping of constraints to values that are not None. Used for core schema updates."""
return {field.name: value for field in fields(self) if (value := getattr(self, field.name)) is not None}
def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
schema = handler(source)
# for function-wrap schemas, url constraints is applied to the inner schema
# because when we generate schemas for urls, we wrap a core_schema.url_schema() with a function-wrap schema
# that helps with validation on initialization, see _BaseUrl and _BaseMultiHostUrl below.
schema_to_mutate = schema['schema'] if schema['type'] == 'function-wrap' else schema
if annotated_type := schema_to_mutate['type'] not in ('url', 'multi-host-url'):
raise PydanticUserError(
f"'UrlConstraints' cannot annotate '{annotated_type}'.", code='invalid-annotated-type'
)
for constraint_key, constraint_value in self.defined_constraints.items():
schema_to_mutate[constraint_key] = constraint_value
return schema
class _BaseUrl:
_constraints: ClassVar[UrlConstraints] = UrlConstraints()
_url: _CoreUrl
def __init__(self, url: str | _CoreUrl | _BaseUrl) -> None:
self._url = _build_type_adapter(self.__class__).validate_python(url)._url
@property
def scheme(self) -> str:
"""The scheme part of the URL.
e.g. `https` in `https://user:pass@host:port/path?query#fragment`
"""
return self._url.scheme
@property
def username(self) -> str | None:
"""The username part of the URL, or `None`.
e.g. `user` in `https://user:pass@host:port/path?query#fragment`
"""
return self._url.username
@property
def password(self) -> str | None:
"""The password part of the URL, or `None`.
e.g. `pass` in `https://user:pass@host:port/path?query#fragment`
"""
return self._url.password
@property
def host(self) -> str | None:
"""The host part of the URL, or `None`.
If the URL must be punycode encoded, this is the encoded host, e.g if the input URL is `https://ยฃยฃยฃ.com`,
`host` will be `xn--9aaa.com`
"""
return self._url.host
def unicode_host(self) -> str | None:
"""The host part of the URL as a unicode string, or `None`.
e.g. `host` in `https://user:pass@host:port/path?query#fragment`
If the URL must be punycode encoded, this is the decoded host, e.g if the input URL is `https://ยฃยฃยฃ.com`,
`unicode_host()` will be `ยฃยฃยฃ.com`
"""
return self._url.unicode_host()
@property
def port(self) -> int | None:
"""The port part of the URL, or `None`.
e.g. `port` in `https://user:pass@host:port/path?query#fragment`
"""
return self._url.port
@property
def path(self) -> str | None:
"""The path part of the URL, or `None`.
e.g. `/path` in `https://user:pass@host:port/path?query#fragment`
"""
return self._url.path
@property
def query(self) -> str | None:
"""The query part of the URL, or `None`.
e.g. `query` in `https://user:pass@host:port/path?query#fragment`
"""
return self._url.query
def query_params(self) -> list[tuple[str, str]]:
"""The query part of the URL as a list of key-value pairs.
e.g. `[('foo', 'bar')]` in `https://user:pass@host:port/path?foo=bar#fragment`
"""
return self._url.query_params()
@property
def fragment(self) -> str | None:
"""The fragment part of the URL, or `None`.
e.g. `fragment` in `https://user:pass@host:port/path?query#fragment`
"""
return self._url.fragment
def unicode_string(self) -> str:
"""The URL as a unicode string, unlike `__str__()` this will not punycode encode the host.
If the URL must be punycode encoded, this is the decoded string, e.g if the input URL is `https://ยฃยฃยฃ.com`,
`unicode_string()` will be `https://ยฃยฃยฃ.com`
"""
return self._url.unicode_string()
def encoded_string(self) -> str:
"""The URL's encoded string representation via __str__().
This returns the punycode-encoded host version of the URL as a string.
"""
return str(self)
def __str__(self) -> str:
"""The URL as a string, this will punycode encode the host if required."""
return str(self._url)
def __repr__(self) -> str:
return f'{self.__class__.__name__}({str(self._url)!r})'
def __deepcopy__(self, memo: dict) -> Self:
return self.__class__(self._url)
def __eq__(self, other: Any) -> bool:
return self.__class__ is other.__class__ and self._url == other._url
def __lt__(self, other: Any) -> bool:
return self.__class__ is other.__class__ and self._url < other._url
def __gt__(self, other: Any) -> bool:
return self.__class__ is other.__class__ and self._url > other._url
def __le__(self, other: Any) -> bool:
return self.__class__ is other.__class__ and self._url <= other._url
def __ge__(self, other: Any) -> bool:
return self.__class__ is other.__class__ and self._url >= other._url
def __hash__(self) -> int:
return hash(self._url)
def __len__(self) -> int:
return len(str(self._url))
@classmethod
def build(
cls,
*,
scheme: str,
username: str | None = None,
password: str | None = None,
host: str,
port: int | None = None,
path: str | None = None,
query: str | None = None,
fragment: str | None = None,
) -> Self:
"""Build a new `Url` instance from its component parts.
Args:
scheme: The scheme part of the URL.
username: The username part of the URL, or omit for no username.
password: The password part of the URL, or omit for no password.
host: The host part of the URL.
port: The port part of the URL, or omit for no port.
path: The path part of the URL, or omit for no path.
query: The query part of the URL, or omit for no query.
fragment: The fragment part of the URL, or omit for no fragment.
Returns:
An instance of URL
"""
return cls(
_CoreUrl.build(
scheme=scheme,
username=username,
password=password,
host=host,
port=port,
path=path,
query=query,
fragment=fragment,
)
)
@classmethod
def serialize_url(cls, url: Any, info: core_schema.SerializationInfo) -> str | Self:
if not isinstance(url, cls):
raise PydanticSerializationUnexpectedValue(
f"Expected `{cls}` but got `{type(url)}` with value `'{url}'` - serialized value may not be as expected."
)
if info.mode == 'json':
return str(url)
return url
@classmethod
def __get_pydantic_core_schema__(
cls, source: type[_BaseUrl], handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
def wrap_val(v, h):
if isinstance(v, source):
return v
if isinstance(v, _BaseUrl):
v = str(v)
core_url = h(v)
instance = source.__new__(source)
instance._url = core_url
return instance
return core_schema.no_info_wrap_validator_function(
wrap_val,
schema=core_schema.url_schema(**cls._constraints.defined_constraints),
serialization=core_schema.plain_serializer_function_ser_schema(
cls.serialize_url, info_arg=True, when_used='always'
),
)
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
) -> JsonSchemaValue:
# we use the url schema for json schema generation, but we might have to extract it from
# the function-wrap schema we use as a tool for validation on initialization
inner_schema = core_schema['schema'] if core_schema['type'] == 'function-wrap' else core_schema
return handler(inner_schema)
__pydantic_serializer__ = SchemaSerializer(core_schema.any_schema(serialization=core_schema.to_string_ser_schema()))
class _BaseMultiHostUrl:
_constraints: ClassVar[UrlConstraints] = UrlConstraints()
_url: _CoreMultiHostUrl
def __init__(self, url: str | _CoreMultiHostUrl | _BaseMultiHostUrl) -> None:
self._url = _build_type_adapter(self.__class__).validate_python(url)._url
@property
def scheme(self) -> str:
"""The scheme part of the URL.
e.g. `https` in `https://foo.com,bar.com/path?query#fragment`
"""
return self._url.scheme
@property
def path(self) -> str | None:
"""The path part of the URL, or `None`.
e.g. `/path` in `https://foo.com,bar.com/path?query#fragment`
"""
return self._url.path
@property
def query(self) -> str | None:
"""The query part of the URL, or `None`.
e.g. `query` in `https://foo.com,bar.com/path?query#fragment`
"""
return self._url.query
def query_params(self) -> list[tuple[str, str]]:
"""The query part of the URL as a list of key-value pairs.
e.g. `[('foo', 'bar')]` in `https://foo.com,bar.com/path?foo=bar#fragment`
"""
return self._url.query_params()
@property
def fragment(self) -> str | None:
"""The fragment part of the URL, or `None`.
e.g. `fragment` in `https://foo.com,bar.com/path?query#fragment`
"""
return self._url.fragment
def hosts(self) -> list[MultiHostHost]:
'''The hosts of the `MultiHostUrl` as [`MultiHostHost`][pydantic_core.MultiHostHost] typed dicts.
```python
from pydantic_core import MultiHostUrl
mhu = MultiHostUrl('https://foo.com:123,foo:bar@bar.com/path')
print(mhu.hosts())
"""
[
{'username': None, 'password': None, 'host': 'foo.com', 'port': 123},
{'username': 'foo', 'password': 'bar', 'host': 'bar.com', 'port': 443}
]
```
Returns:
A list of dicts, each representing a host.
'''
return self._url.hosts()
def encoded_string(self) -> str:
"""The URL's encoded string representation via __str__().
This returns the punycode-encoded host version of the URL as a string.
"""
return str(self)
def unicode_string(self) -> str:
"""The URL as a unicode string, unlike `__str__()` this will not punycode encode the hosts."""
return self._url.unicode_string()
def __str__(self) -> str:
"""The URL as a string, this will punycode encode the host if required."""
return str(self._url)
def __repr__(self) -> str:
return f'{self.__class__.__name__}({str(self._url)!r})'
def __deepcopy__(self, memo: dict) -> Self:
return self.__class__(self._url)
def __eq__(self, other: Any) -> bool:
return self.__class__ is other.__class__ and self._url == other._url
def __hash__(self) -> int:
return hash(self._url)
def __len__(self) -> int:
return len(str(self._url))
@classmethod
def build(
cls,
*,
scheme: str,
hosts: list[MultiHostHost] | None = None,
username: str | None = None,
password: str | None = None,
host: str | None = None,
port: int | None = None,
path: str | None = None,
query: str | None = None,
fragment: str | None = None,
) -> Self:
"""Build a new `MultiHostUrl` instance from its component parts.
This method takes either `hosts` - a list of `MultiHostHost` typed dicts, or the individual components
`username`, `password`, `host` and `port`.
Args:
scheme: The scheme part of the URL.
hosts: Multiple hosts to build the URL from.
username: The username part of the URL.
password: The password part of the URL.
host: The host part of the URL.
port: The port part of the URL.
path: The path part of the URL.
query: The query part of the URL, or omit for no query.
fragment: The fragment part of the URL, or omit for no fragment.
Returns:
An instance of `MultiHostUrl`
"""
return cls(
_CoreMultiHostUrl.build(
scheme=scheme,
hosts=hosts,
username=username,
password=password,
host=host,
port=port,
path=path,
query=query,
fragment=fragment,
)
)
@classmethod
def serialize_url(cls, url: Any, info: core_schema.SerializationInfo) -> str | Self:
if not isinstance(url, cls):
raise PydanticSerializationUnexpectedValue(
f"Expected `{cls}` but got `{type(url)}` with value `'{url}'` - serialized value may not be as expected."
)
if info.mode == 'json':
return str(url)
return url
@classmethod
def __get_pydantic_core_schema__(
cls, source: type[_BaseMultiHostUrl], handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
def wrap_val(v, h):
if isinstance(v, source):
return v
if isinstance(v, _BaseMultiHostUrl):
v = str(v)
core_url = h(v)
instance = source.__new__(source)
instance._url = core_url
return instance
return core_schema.no_info_wrap_validator_function(
wrap_val,
schema=core_schema.multi_host_url_schema(**cls._constraints.defined_constraints),
serialization=core_schema.plain_serializer_function_ser_schema(
cls.serialize_url, info_arg=True, when_used='always'
),
)
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
) -> JsonSchemaValue:
# we use the url schema for json schema generation, but we might have to extract it from
# the function-wrap schema we use as a tool for validation on initialization
inner_schema = core_schema['schema'] if core_schema['type'] == 'function-wrap' else core_schema
return handler(inner_schema)
__pydantic_serializer__ = SchemaSerializer(core_schema.any_schema(serialization=core_schema.to_string_ser_schema()))
@lru_cache
def _build_type_adapter(cls: type[_BaseUrl | _BaseMultiHostUrl]) -> TypeAdapter:
return TypeAdapter(cls)
class AnyUrl(_BaseUrl):
"""Base type for all URLs.
* Any scheme allowed
* Top-level domain (TLD) not required
* Host not required
Assuming an input URL of `http://samuel:pass@example.com:8000/the/path/?query=here#fragment=is;this=bit`,
the types export the following properties:
- `scheme`: the URL scheme (`http`), always set.
- `host`: the URL host (`example.com`).
- `username`: optional username if included (`samuel`).
- `password`: optional password if included (`pass`).
- `port`: optional port (`8000`).
- `path`: optional path (`/the/path/`).
- `query`: optional URL query (for example, `GET` arguments or "search string", such as `query=here`).
- `fragment`: optional fragment (`fragment=is;this=bit`).
"""
# Note: all single host urls inherit from `AnyUrl` to preserve compatibility with pre-v2.10 code
# Where urls were annotated variants of `AnyUrl`, which was an alias to `pydantic_core.Url`
class AnyHttpUrl(AnyUrl):
"""A type that will accept any http or https URL.
* TLD not required
* Host not required
"""
_constraints = UrlConstraints(allowed_schemes=['http', 'https'])
class HttpUrl(AnyUrl):
"""A type that will accept any http or https URL.
* TLD not required
* Host not required
* Max length 2083
```python
from pydantic import BaseModel, HttpUrl, ValidationError
class MyModel(BaseModel):
url: HttpUrl
m = MyModel(url='http://www.example.com') # (1)!
print(m.url)
#> http://www.example.com/
try:
MyModel(url='ftp://invalid.url')
except ValidationError as e:
print(e)
'''
1 validation error for MyModel
url
URL scheme should be 'http' or 'https' [type=url_scheme, input_value='ftp://invalid.url', input_type=str]
'''
try:
MyModel(url='not a url')
except ValidationError as e:
print(e)
'''
1 validation error for MyModel
url
Input should be a valid URL, relative URL without a base [type=url_parsing, input_value='not a url', input_type=str]
'''
```
1. Note: mypy would prefer `m = MyModel(url=HttpUrl('http://www.example.com'))`, but Pydantic will convert the string to an HttpUrl instance anyway.
"International domains" (e.g. a URL where the host or TLD includes non-ascii characters) will be encoded via
[punycode](https://en.wikipedia.org/wiki/Punycode) (see
[this article](https://www.xudongz.com/blog/2017/idn-phishing/) for a good description of why this is important):
```python
from pydantic import BaseModel, HttpUrl
class MyModel(BaseModel):
url: HttpUrl
m1 = MyModel(url='http://punyยฃcode.com')
print(m1.url)
#> http://xn--punycode-eja.com/
m2 = MyModel(url='https://www.ะฐััำะต.com/')
print(m2.url)
#> https://www.xn--80ak6aa92e.com/
m3 = MyModel(url='https://www.example.็ ๅฎ/')
print(m3.url)
#> https://www.example.xn--pbt977c/
```
!!! warning "Underscores in Hostnames"
In Pydantic, underscores are allowed in all parts of a domain except the TLD.
Technically this might be wrong - in theory the hostname cannot have underscores, but subdomains can.
To explain this; consider the following two cases:
- `exam_ple.co.uk`: the hostname is `exam_ple`, which should not be allowed since it contains an underscore.
- `foo_bar.example.com` the hostname is `example`, which should be allowed since the underscore is in the subdomain.
Without having an exhaustive list of TLDs, it would be impossible to differentiate between these two. Therefore
underscores are allowed, but you can always do further validation in a validator if desired.
Also, Chrome, Firefox, and Safari all currently accept `http://exam_ple.com` as a URL, so we're in good
(or at least big) company.
"""
_constraints = UrlConstraints(max_length=2083, allowed_schemes=['http', 'https'])
class AnyWebsocketUrl(AnyUrl):
"""A type that will accept any ws or wss URL.
* TLD not required
* Host not required
"""
_constraints = UrlConstraints(allowed_schemes=['ws', 'wss'])
class WebsocketUrl(AnyUrl):
"""A type that will accept any ws or wss URL.
* TLD not required
* Host not required
* Max length 2083
"""
_constraints = UrlConstraints(max_length=2083, allowed_schemes=['ws', 'wss'])
class FileUrl(AnyUrl):
"""A type that will accept any file URL.
* Host not required
"""
_constraints = UrlConstraints(allowed_schemes=['file'])
class FtpUrl(AnyUrl):
"""A type that will accept ftp URL.
* TLD not required
* Host not required
"""
_constraints = UrlConstraints(allowed_schemes=['ftp'])
class PostgresDsn(_BaseMultiHostUrl):
"""A type that will accept any Postgres DSN.
* User info required
* TLD not required
* Host required
* Supports multiple hosts
If further validation is required, these properties can be used by validators to enforce specific behaviour:
```python
from pydantic import (
BaseModel,
HttpUrl,
PostgresDsn,
ValidationError,
field_validator,
)
class MyModel(BaseModel):
url: HttpUrl
m = MyModel(url='http://www.example.com')
# the repr() method for a url will display all properties of the url
print(repr(m.url))
#> HttpUrl('http://www.example.com/')
print(m.url.scheme)
#> http
print(m.url.host)
#> www.example.com
print(m.url.port)
#> 80
class MyDatabaseModel(BaseModel):
db: PostgresDsn
@field_validator('db')
def check_db_name(cls, v):
assert v.path and len(v.path) > 1, 'database must be provided'
return v
m = MyDatabaseModel(db='postgres://user:pass@localhost:5432/foobar')
print(m.db)
#> postgres://user:pass@localhost:5432/foobar
try:
MyDatabaseModel(db='postgres://user:pass@localhost:5432')
except ValidationError as e:
print(e)
'''
1 validation error for MyDatabaseModel
db
Assertion failed, database must be provided
assert (None)
+ where None = PostgresDsn('postgres://user:pass@localhost:5432').path [type=assertion_error, input_value='postgres://user:pass@localhost:5432', input_type=str]
'''
```
"""
_constraints = UrlConstraints(
host_required=True,
allowed_schemes=[
'postgres',
'postgresql',
'postgresql+asyncpg',
'postgresql+pg8000',
'postgresql+psycopg',
'postgresql+psycopg2',
'postgresql+psycopg2cffi',
'postgresql+py-postgresql',
'postgresql+pygresql',
],
)
@property
def host(self) -> str:
"""The required URL host."""
return self._url.host # pyright: ignore[reportAttributeAccessIssue]
class CockroachDsn(AnyUrl):
"""A type that will accept any Cockroach DSN.
* User info required
* TLD not required
* Host required
"""
_constraints = UrlConstraints(
host_required=True,
allowed_schemes=[
'cockroachdb',
'cockroachdb+psycopg2',
'cockroachdb+asyncpg',
],
)
@property
def host(self) -> str:
"""The required URL host."""
return self._url.host # pyright: ignore[reportReturnType]
class AmqpDsn(AnyUrl):
"""A type that will accept any AMQP DSN.
* User info required
* TLD not required
* Host not required
"""
_constraints = UrlConstraints(allowed_schemes=['amqp', 'amqps'])
class RedisDsn(AnyUrl):
"""A type that will accept any Redis DSN.
* User info required
* TLD not required
* Host required (e.g., `rediss://:pass@localhost`)
"""
_constraints = UrlConstraints(
allowed_schemes=['redis', 'rediss'],
default_host='localhost',
default_port=6379,
default_path='/0',
host_required=True,
)
@property
def host(self) -> str:
"""The required URL host."""
return self._url.host # pyright: ignore[reportReturnType]
class MongoDsn(_BaseMultiHostUrl):
"""A type that will accept any MongoDB DSN.
* User info not required
* Database name not required
* Port not required
* User info may be passed without user part (e.g., `mongodb://mongodb0.example.com:27017`).
"""
_constraints = UrlConstraints(allowed_schemes=['mongodb', 'mongodb+srv'], default_port=27017)
class KafkaDsn(AnyUrl):
"""A type that will accept any Kafka DSN.
* User info required
* TLD not required
* Host not required
"""
_constraints = UrlConstraints(allowed_schemes=['kafka'], default_host='localhost', default_port=9092)
class NatsDsn(_BaseMultiHostUrl):
"""A type that will accept any NATS DSN.
NATS is a connective technology built for the ever increasingly hyper-connected world.
It is a single technology that enables applications to securely communicate across
any combination of cloud vendors, on-premise, edge, web and mobile, and devices.
More: https://nats.io
"""
_constraints = UrlConstraints(
allowed_schemes=['nats', 'tls', 'ws', 'wss'], default_host='localhost', default_port=4222
)
class MySQLDsn(AnyUrl):
"""A type that will accept any MySQL DSN.
* User info required
* TLD not required
* Host not required
"""
_constraints = UrlConstraints(
allowed_schemes=[
'mysql',
'mysql+mysqlconnector',
'mysql+aiomysql',
'mysql+asyncmy',
'mysql+mysqldb',
'mysql+pymysql',
'mysql+cymysql',
'mysql+pyodbc',
],
default_port=3306,
host_required=True,
)
class MariaDBDsn(AnyUrl):
"""A type that will accept any MariaDB DSN.
* User info required
* TLD not required
* Host not required
"""
_constraints = UrlConstraints(
allowed_schemes=['mariadb', 'mariadb+mariadbconnector', 'mariadb+pymysql'],
default_port=3306,
)
class ClickHouseDsn(AnyUrl):
"""A type that will accept any ClickHouse DSN.
* User info required
* TLD not required
* Host not required
"""
_constraints = UrlConstraints(
allowed_schemes=[
'clickhouse+native',
'clickhouse+asynch',
'clickhouse+http',
'clickhouse',
'clickhouses',
'clickhousedb',
],
default_host='localhost',
default_port=9000,
)
class SnowflakeDsn(AnyUrl):
"""A type that will accept any Snowflake DSN.
* User info required
* TLD not required
* Host required
"""
_constraints = UrlConstraints(
allowed_schemes=['snowflake'],
host_required=True,
)
@property
def host(self) -> str:
"""The required URL host."""
return self._url.host # pyright: ignore[reportReturnType]
def import_email_validator() -> None:
global email_validator
try:
import email_validator
except ImportError as e:
raise ImportError('email-validator is not installed, run `pip install pydantic[email]`') from e
if not version('email-validator').partition('.')[0] == '2':
raise ImportError('email-validator version >= 2.0 required, run pip install -U email-validator')
if TYPE_CHECKING:
EmailStr = Annotated[str, ...]
else:
class EmailStr:
"""
Info:
To use this type, you need to install the optional
[`email-validator`](https://github.com/JoshData/python-email-validator) package:
```bash
pip install email-validator
```
Validate email addresses.
```python
from pydantic import BaseModel, EmailStr
class Model(BaseModel):
email: EmailStr
print(Model(email='contact@mail.com'))
#> email='contact@mail.com'
```
""" # noqa: D212
@classmethod
def __get_pydantic_core_schema__(
cls,
_source: type[Any],
_handler: GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
import_email_validator()
return core_schema.no_info_after_validator_function(cls._validate, core_schema.str_schema())
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
) -> JsonSchemaValue:
field_schema = handler(core_schema)
field_schema.update(type='string', format='email')
return field_schema
@classmethod
def _validate(cls, input_value: str, /) -> str:
return validate_email(input_value)[1]
class NameEmail(_repr.Representation):
"""
Info:
To use this type, you need to install the optional
[`email-validator`](https://github.com/JoshData/python-email-validator) package:
```bash
pip install email-validator
```
Validate a name and email address combination, as specified by
[RFC 5322](https://datatracker.ietf.org/doc/html/rfc5322#section-3.4).
The `NameEmail` has two properties: `name` and `email`.
In case the `name` is not provided, it's inferred from the email address.
```python
from pydantic import BaseModel, NameEmail
class User(BaseModel):
email: NameEmail
user = User(email='Fred Bloggs <fred.bloggs@example.com>')
print(user.email)
#> Fred Bloggs <fred.bloggs@example.com>
print(user.email.name)
#> Fred Bloggs
user = User(email='fred.bloggs@example.com')
print(user.email)
#> fred.bloggs <fred.bloggs@example.com>
print(user.email.name)
#> fred.bloggs
```
""" # noqa: D212
__slots__ = 'name', 'email'
def __init__(self, name: str, email: str):
self.name = name
self.email = email
def __eq__(self, other: Any) -> bool:
return isinstance(other, NameEmail) and (self.name, self.email) == (other.name, other.email)
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
) -> JsonSchemaValue:
field_schema = handler(core_schema)
field_schema.update(type='string', format='name-email')
return field_schema
@classmethod
def __get_pydantic_core_schema__(
cls,
_source: type[Any],
_handler: GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
import_email_validator()
return core_schema.no_info_after_validator_function(
cls._validate,
core_schema.json_or_python_schema(
json_schema=core_schema.str_schema(),
python_schema=core_schema.union_schema(
[core_schema.is_instance_schema(cls), core_schema.str_schema()],
custom_error_type='name_email_type',
custom_error_message='Input is not a valid NameEmail',
),
serialization=core_schema.to_string_ser_schema(),
),
)
@classmethod
def _validate(cls, input_value: Self | str, /) -> Self:
if isinstance(input_value, str):
name, email = validate_email(input_value)
return cls(name, email)
else:
return input_value
def __str__(self) -> str:
if '@' in self.name:
return f'"{self.name}" <{self.email}>'
return f'{self.name} <{self.email}>'
IPvAnyAddressType: TypeAlias = 'IPv4Address | IPv6Address'
IPvAnyInterfaceType: TypeAlias = 'IPv4Interface | IPv6Interface'
IPvAnyNetworkType: TypeAlias = 'IPv4Network | IPv6Network'
if TYPE_CHECKING:
IPvAnyAddress = IPvAnyAddressType
IPvAnyInterface = IPvAnyInterfaceType
IPvAnyNetwork = IPvAnyNetworkType
else:
class IPvAnyAddress:
"""Validate an IPv4 or IPv6 address.
```python
from pydantic import BaseModel
from pydantic.networks import IPvAnyAddress
class IpModel(BaseModel):
ip: IPvAnyAddress
print(IpModel(ip='127.0.0.1'))
#> ip=IPv4Address('127.0.0.1')
try:
IpModel(ip='http://www.example.com')
except ValueError as e:
print(e.errors())
'''
[
{
'type': 'ip_any_address',
'loc': ('ip',),
'msg': 'value is not a valid IPv4 or IPv6 address',
'input': 'http://www.example.com',
}
]
'''
```
"""
__slots__ = ()
def __new__(cls, value: Any) -> IPvAnyAddressType:
"""Validate an IPv4 or IPv6 address."""
try:
return IPv4Address(value)
except ValueError:
pass
try:
return IPv6Address(value)
except ValueError:
raise PydanticCustomError('ip_any_address', 'value is not a valid IPv4 or IPv6 address')
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
) -> JsonSchemaValue:
field_schema = {}
field_schema.update(type='string', format='ipvanyaddress')
return field_schema
@classmethod
def __get_pydantic_core_schema__(
cls,
_source: type[Any],
_handler: GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
return core_schema.no_info_plain_validator_function(
cls._validate, serialization=core_schema.to_string_ser_schema()
)
@classmethod
def _validate(cls, input_value: Any, /) -> IPvAnyAddressType:
return cls(input_value) # type: ignore[return-value]
class IPvAnyInterface:
"""Validate an IPv4 or IPv6 interface."""
__slots__ = ()
def __new__(cls, value: NetworkType) -> IPvAnyInterfaceType:
"""Validate an IPv4 or IPv6 interface."""
try:
return IPv4Interface(value)
except ValueError:
pass
try:
return IPv6Interface(value)
except ValueError:
raise PydanticCustomError('ip_any_interface', 'value is not a valid IPv4 or IPv6 interface')
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
) -> JsonSchemaValue:
field_schema = {}
field_schema.update(type='string', format='ipvanyinterface')
return field_schema
@classmethod
def __get_pydantic_core_schema__(
cls,
_source: type[Any],
_handler: GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
return core_schema.no_info_plain_validator_function(
cls._validate, serialization=core_schema.to_string_ser_schema()
)
@classmethod
def _validate(cls, input_value: NetworkType, /) -> IPvAnyInterfaceType:
return cls(input_value) # type: ignore[return-value]
class IPvAnyNetwork:
"""Validate an IPv4 or IPv6 network."""
__slots__ = ()
def __new__(cls, value: NetworkType) -> IPvAnyNetworkType:
"""Validate an IPv4 or IPv6 network."""
# Assume IP Network is defined with a default value for `strict` argument.
# Define your own class if you want to specify network address check strictness.
try:
return IPv4Network(value)
except ValueError:
pass
try:
return IPv6Network(value)
except ValueError:
raise PydanticCustomError('ip_any_network', 'value is not a valid IPv4 or IPv6 network')
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
) -> JsonSchemaValue:
field_schema = {}
field_schema.update(type='string', format='ipvanynetwork')
return field_schema
@classmethod
def __get_pydantic_core_schema__(
cls,
_source: type[Any],
_handler: GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
return core_schema.no_info_plain_validator_function(
cls._validate, serialization=core_schema.to_string_ser_schema()
)
@classmethod
def _validate(cls, input_value: NetworkType, /) -> IPvAnyNetworkType:
return cls(input_value) # type: ignore[return-value]
def _build_pretty_email_regex() -> re.Pattern[str]:
name_chars = r'[\w!#$%&\'*+\-/=?^_`{|}~]'
unquoted_name_group = rf'((?:{name_chars}+\s+)*{name_chars}+)'
quoted_name_group = r'"((?:[^"]|\")+)"'
email_group = r'<(.+)>'
return re.compile(rf'\s*(?:{unquoted_name_group}|{quoted_name_group})?\s*{email_group}\s*')
pretty_email_regex = _build_pretty_email_regex()
MAX_EMAIL_LENGTH = 2048
"""Maximum length for an email.
A somewhat arbitrary but very generous number compared to what is allowed by most implementations.
"""
def validate_email(value: str) -> tuple[str, str]:
"""Email address validation using [email-validator](https://pypi.org/project/email-validator/).
Returns:
A tuple containing the local part of the email (or the name for "pretty" email addresses)
and the normalized email.
Raises:
PydanticCustomError: If the email is invalid.
Note:
Note that:
* Raw IP address (literal) domain parts are not allowed.
* `"John Doe <local_part@domain.com>"` style "pretty" email addresses are processed.
* Spaces are striped from the beginning and end of addresses, but no error is raised.
"""
if email_validator is None:
import_email_validator()
if len(value) > MAX_EMAIL_LENGTH:
raise PydanticCustomError(
'value_error',
'value is not a valid email address: {reason}',
{'reason': f'Length must not exceed {MAX_EMAIL_LENGTH} characters'},
)
m = pretty_email_regex.fullmatch(value)
name: str | None = None
if m:
unquoted_name, quoted_name, value = m.groups()
name = unquoted_name or quoted_name
email = value.strip()
try:
parts = email_validator.validate_email(email, check_deliverability=False)
except email_validator.EmailNotValidError as e:
raise PydanticCustomError(
'value_error', 'value is not a valid email address: {reason}', {'reason': str(e.args[0])}
) from e
email = parts.normalized
assert email is not None
name = name or parts.local_part
return name, email
__getattr__ = getattr_migration(__name__)
|