networks.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. """The networks module contains types for common network-related fields."""
  2. from __future__ import annotations as _annotations
  3. import dataclasses as _dataclasses
  4. import re
  5. from dataclasses import fields
  6. from functools import lru_cache
  7. from importlib.metadata import version
  8. from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
  9. from typing import TYPE_CHECKING, Annotated, Any, ClassVar
  10. from pydantic_core import (
  11. MultiHostHost,
  12. PydanticCustomError,
  13. PydanticSerializationUnexpectedValue,
  14. SchemaSerializer,
  15. core_schema,
  16. )
  17. from pydantic_core import MultiHostUrl as _CoreMultiHostUrl
  18. from pydantic_core import Url as _CoreUrl
  19. from typing_extensions import Self, TypeAlias
  20. from pydantic.errors import PydanticUserError
  21. from ._internal import _repr, _schema_generation_shared
  22. from ._migration import getattr_migration
  23. from .annotated_handlers import GetCoreSchemaHandler
  24. from .json_schema import JsonSchemaValue
  25. from .type_adapter import TypeAdapter
  26. if TYPE_CHECKING:
  27. import email_validator
  28. NetworkType: TypeAlias = 'str | bytes | int | tuple[str | bytes | int, str | int]'
  29. else:
  30. email_validator = None
  31. __all__ = [
  32. 'AnyUrl',
  33. 'AnyHttpUrl',
  34. 'FileUrl',
  35. 'FtpUrl',
  36. 'HttpUrl',
  37. 'WebsocketUrl',
  38. 'AnyWebsocketUrl',
  39. 'UrlConstraints',
  40. 'EmailStr',
  41. 'NameEmail',
  42. 'IPvAnyAddress',
  43. 'IPvAnyInterface',
  44. 'IPvAnyNetwork',
  45. 'PostgresDsn',
  46. 'CockroachDsn',
  47. 'AmqpDsn',
  48. 'RedisDsn',
  49. 'MongoDsn',
  50. 'KafkaDsn',
  51. 'NatsDsn',
  52. 'validate_email',
  53. 'MySQLDsn',
  54. 'MariaDBDsn',
  55. 'ClickHouseDsn',
  56. 'SnowflakeDsn',
  57. ]
  58. @_dataclasses.dataclass
  59. class UrlConstraints:
  60. """Url constraints.
  61. Attributes:
  62. max_length: The maximum length of the url. Defaults to `None`.
  63. allowed_schemes: The allowed schemes. Defaults to `None`.
  64. host_required: Whether the host is required. Defaults to `None`.
  65. default_host: The default host. Defaults to `None`.
  66. default_port: The default port. Defaults to `None`.
  67. default_path: The default path. Defaults to `None`.
  68. preserve_empty_path: Whether to preserve empty URL paths. Defaults to `None`.
  69. """
  70. max_length: int | None = None
  71. allowed_schemes: list[str] | None = None
  72. host_required: bool | None = None
  73. default_host: str | None = None
  74. default_port: int | None = None
  75. default_path: str | None = None
  76. preserve_empty_path: bool | None = None
  77. def __hash__(self) -> int:
  78. return hash(
  79. (
  80. self.max_length,
  81. tuple(self.allowed_schemes) if self.allowed_schemes is not None else None,
  82. self.host_required,
  83. self.default_host,
  84. self.default_port,
  85. self.default_path,
  86. self.preserve_empty_path,
  87. )
  88. )
  89. @property
  90. def defined_constraints(self) -> dict[str, Any]:
  91. """Fetch a key / value mapping of constraints to values that are not None. Used for core schema updates."""
  92. return {field.name: value for field in fields(self) if (value := getattr(self, field.name)) is not None}
  93. def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  94. schema = handler(source)
  95. # for function-wrap schemas, url constraints is applied to the inner schema
  96. # because when we generate schemas for urls, we wrap a core_schema.url_schema() with a function-wrap schema
  97. # that helps with validation on initialization, see _BaseUrl and _BaseMultiHostUrl below.
  98. schema_to_mutate = schema['schema'] if schema['type'] == 'function-wrap' else schema
  99. if annotated_type := schema_to_mutate['type'] not in ('url', 'multi-host-url'):
  100. raise PydanticUserError(
  101. f"'UrlConstraints' cannot annotate '{annotated_type}'.", code='invalid-annotated-type'
  102. )
  103. for constraint_key, constraint_value in self.defined_constraints.items():
  104. schema_to_mutate[constraint_key] = constraint_value
  105. return schema
  106. class _BaseUrl:
  107. _constraints: ClassVar[UrlConstraints] = UrlConstraints()
  108. _url: _CoreUrl
  109. def __init__(self, url: str | _CoreUrl | _BaseUrl) -> None:
  110. self._url = _build_type_adapter(self.__class__).validate_python(url)._url
  111. @property
  112. def scheme(self) -> str:
  113. """The scheme part of the URL.
  114. e.g. `https` in `https://user:pass@host:port/path?query#fragment`
  115. """
  116. return self._url.scheme
  117. @property
  118. def username(self) -> str | None:
  119. """The username part of the URL, or `None`.
  120. e.g. `user` in `https://user:pass@host:port/path?query#fragment`
  121. """
  122. return self._url.username
  123. @property
  124. def password(self) -> str | None:
  125. """The password part of the URL, or `None`.
  126. e.g. `pass` in `https://user:pass@host:port/path?query#fragment`
  127. """
  128. return self._url.password
  129. @property
  130. def host(self) -> str | None:
  131. """The host part of the URL, or `None`.
  132. If the URL must be punycode encoded, this is the encoded host, e.g if the input URL is `https://£££.com`,
  133. `host` will be `xn--9aaa.com`
  134. """
  135. return self._url.host
  136. def unicode_host(self) -> str | None:
  137. """The host part of the URL as a unicode string, or `None`.
  138. e.g. `host` in `https://user:pass@host:port/path?query#fragment`
  139. If the URL must be punycode encoded, this is the decoded host, e.g if the input URL is `https://£££.com`,
  140. `unicode_host()` will be `£££.com`
  141. """
  142. return self._url.unicode_host()
  143. @property
  144. def port(self) -> int | None:
  145. """The port part of the URL, or `None`.
  146. e.g. `port` in `https://user:pass@host:port/path?query#fragment`
  147. """
  148. return self._url.port
  149. @property
  150. def path(self) -> str | None:
  151. """The path part of the URL, or `None`.
  152. e.g. `/path` in `https://user:pass@host:port/path?query#fragment`
  153. """
  154. return self._url.path
  155. @property
  156. def query(self) -> str | None:
  157. """The query part of the URL, or `None`.
  158. e.g. `query` in `https://user:pass@host:port/path?query#fragment`
  159. """
  160. return self._url.query
  161. def query_params(self) -> list[tuple[str, str]]:
  162. """The query part of the URL as a list of key-value pairs.
  163. e.g. `[('foo', 'bar')]` in `https://user:pass@host:port/path?foo=bar#fragment`
  164. """
  165. return self._url.query_params()
  166. @property
  167. def fragment(self) -> str | None:
  168. """The fragment part of the URL, or `None`.
  169. e.g. `fragment` in `https://user:pass@host:port/path?query#fragment`
  170. """
  171. return self._url.fragment
  172. def unicode_string(self) -> str:
  173. """The URL as a unicode string, unlike `__str__()` this will not punycode encode the host.
  174. If the URL must be punycode encoded, this is the decoded string, e.g if the input URL is `https://£££.com`,
  175. `unicode_string()` will be `https://£££.com`
  176. """
  177. return self._url.unicode_string()
  178. def encoded_string(self) -> str:
  179. """The URL's encoded string representation via __str__().
  180. This returns the punycode-encoded host version of the URL as a string.
  181. """
  182. return str(self)
  183. def __str__(self) -> str:
  184. """The URL as a string, this will punycode encode the host if required."""
  185. return str(self._url)
  186. def __repr__(self) -> str:
  187. return f'{self.__class__.__name__}({str(self._url)!r})'
  188. def __deepcopy__(self, memo: dict) -> Self:
  189. return self.__class__(self._url)
  190. def __eq__(self, other: Any) -> bool:
  191. return self.__class__ is other.__class__ and self._url == other._url
  192. def __lt__(self, other: Any) -> bool:
  193. return self.__class__ is other.__class__ and self._url < other._url
  194. def __gt__(self, other: Any) -> bool:
  195. return self.__class__ is other.__class__ and self._url > other._url
  196. def __le__(self, other: Any) -> bool:
  197. return self.__class__ is other.__class__ and self._url <= other._url
  198. def __ge__(self, other: Any) -> bool:
  199. return self.__class__ is other.__class__ and self._url >= other._url
  200. def __hash__(self) -> int:
  201. return hash(self._url)
  202. def __len__(self) -> int:
  203. return len(str(self._url))
  204. @classmethod
  205. def build(
  206. cls,
  207. *,
  208. scheme: str,
  209. username: str | None = None,
  210. password: str | None = None,
  211. host: str,
  212. port: int | None = None,
  213. path: str | None = None,
  214. query: str | None = None,
  215. fragment: str | None = None,
  216. ) -> Self:
  217. """Build a new `Url` instance from its component parts.
  218. Args:
  219. scheme: The scheme part of the URL.
  220. username: The username part of the URL, or omit for no username.
  221. password: The password part of the URL, or omit for no password.
  222. host: The host part of the URL.
  223. port: The port part of the URL, or omit for no port.
  224. path: The path part of the URL, or omit for no path.
  225. query: The query part of the URL, or omit for no query.
  226. fragment: The fragment part of the URL, or omit for no fragment.
  227. Returns:
  228. An instance of URL
  229. """
  230. return cls(
  231. _CoreUrl.build(
  232. scheme=scheme,
  233. username=username,
  234. password=password,
  235. host=host,
  236. port=port,
  237. path=path,
  238. query=query,
  239. fragment=fragment,
  240. )
  241. )
  242. @classmethod
  243. def serialize_url(cls, url: Any, info: core_schema.SerializationInfo) -> str | Self:
  244. if not isinstance(url, cls):
  245. raise PydanticSerializationUnexpectedValue(
  246. f"Expected `{cls}` but got `{type(url)}` with value `'{url}'` - serialized value may not be as expected."
  247. )
  248. if info.mode == 'json':
  249. return str(url)
  250. return url
  251. @classmethod
  252. def __get_pydantic_core_schema__(
  253. cls, source: type[_BaseUrl], handler: GetCoreSchemaHandler
  254. ) -> core_schema.CoreSchema:
  255. def wrap_val(v, h):
  256. if isinstance(v, source):
  257. return v
  258. if isinstance(v, _BaseUrl):
  259. v = str(v)
  260. core_url = h(v)
  261. instance = source.__new__(source)
  262. instance._url = core_url
  263. return instance
  264. return core_schema.no_info_wrap_validator_function(
  265. wrap_val,
  266. schema=core_schema.url_schema(**cls._constraints.defined_constraints),
  267. serialization=core_schema.plain_serializer_function_ser_schema(
  268. cls.serialize_url, info_arg=True, when_used='always'
  269. ),
  270. )
  271. @classmethod
  272. def __get_pydantic_json_schema__(
  273. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  274. ) -> JsonSchemaValue:
  275. # we use the url schema for json schema generation, but we might have to extract it from
  276. # the function-wrap schema we use as a tool for validation on initialization
  277. inner_schema = core_schema['schema'] if core_schema['type'] == 'function-wrap' else core_schema
  278. return handler(inner_schema)
  279. __pydantic_serializer__ = SchemaSerializer(core_schema.any_schema(serialization=core_schema.to_string_ser_schema()))
  280. class _BaseMultiHostUrl:
  281. _constraints: ClassVar[UrlConstraints] = UrlConstraints()
  282. _url: _CoreMultiHostUrl
  283. def __init__(self, url: str | _CoreMultiHostUrl | _BaseMultiHostUrl) -> None:
  284. self._url = _build_type_adapter(self.__class__).validate_python(url)._url
  285. @property
  286. def scheme(self) -> str:
  287. """The scheme part of the URL.
  288. e.g. `https` in `https://foo.com,bar.com/path?query#fragment`
  289. """
  290. return self._url.scheme
  291. @property
  292. def path(self) -> str | None:
  293. """The path part of the URL, or `None`.
  294. e.g. `/path` in `https://foo.com,bar.com/path?query#fragment`
  295. """
  296. return self._url.path
  297. @property
  298. def query(self) -> str | None:
  299. """The query part of the URL, or `None`.
  300. e.g. `query` in `https://foo.com,bar.com/path?query#fragment`
  301. """
  302. return self._url.query
  303. def query_params(self) -> list[tuple[str, str]]:
  304. """The query part of the URL as a list of key-value pairs.
  305. e.g. `[('foo', 'bar')]` in `https://foo.com,bar.com/path?foo=bar#fragment`
  306. """
  307. return self._url.query_params()
  308. @property
  309. def fragment(self) -> str | None:
  310. """The fragment part of the URL, or `None`.
  311. e.g. `fragment` in `https://foo.com,bar.com/path?query#fragment`
  312. """
  313. return self._url.fragment
  314. def hosts(self) -> list[MultiHostHost]:
  315. '''The hosts of the `MultiHostUrl` as [`MultiHostHost`][pydantic_core.MultiHostHost] typed dicts.
  316. ```python
  317. from pydantic_core import MultiHostUrl
  318. mhu = MultiHostUrl('https://foo.com:123,foo:bar@bar.com/path')
  319. print(mhu.hosts())
  320. """
  321. [
  322. {'username': None, 'password': None, 'host': 'foo.com', 'port': 123},
  323. {'username': 'foo', 'password': 'bar', 'host': 'bar.com', 'port': 443}
  324. ]
  325. ```
  326. Returns:
  327. A list of dicts, each representing a host.
  328. '''
  329. return self._url.hosts()
  330. def encoded_string(self) -> str:
  331. """The URL's encoded string representation via __str__().
  332. This returns the punycode-encoded host version of the URL as a string.
  333. """
  334. return str(self)
  335. def unicode_string(self) -> str:
  336. """The URL as a unicode string, unlike `__str__()` this will not punycode encode the hosts."""
  337. return self._url.unicode_string()
  338. def __str__(self) -> str:
  339. """The URL as a string, this will punycode encode the host if required."""
  340. return str(self._url)
  341. def __repr__(self) -> str:
  342. return f'{self.__class__.__name__}({str(self._url)!r})'
  343. def __deepcopy__(self, memo: dict) -> Self:
  344. return self.__class__(self._url)
  345. def __eq__(self, other: Any) -> bool:
  346. return self.__class__ is other.__class__ and self._url == other._url
  347. def __hash__(self) -> int:
  348. return hash(self._url)
  349. def __len__(self) -> int:
  350. return len(str(self._url))
  351. @classmethod
  352. def build(
  353. cls,
  354. *,
  355. scheme: str,
  356. hosts: list[MultiHostHost] | None = None,
  357. username: str | None = None,
  358. password: str | None = None,
  359. host: str | None = None,
  360. port: int | None = None,
  361. path: str | None = None,
  362. query: str | None = None,
  363. fragment: str | None = None,
  364. ) -> Self:
  365. """Build a new `MultiHostUrl` instance from its component parts.
  366. This method takes either `hosts` - a list of `MultiHostHost` typed dicts, or the individual components
  367. `username`, `password`, `host` and `port`.
  368. Args:
  369. scheme: The scheme part of the URL.
  370. hosts: Multiple hosts to build the URL from.
  371. username: The username part of the URL.
  372. password: The password part of the URL.
  373. host: The host part of the URL.
  374. port: The port part of the URL.
  375. path: The path part of the URL.
  376. query: The query part of the URL, or omit for no query.
  377. fragment: The fragment part of the URL, or omit for no fragment.
  378. Returns:
  379. An instance of `MultiHostUrl`
  380. """
  381. return cls(
  382. _CoreMultiHostUrl.build(
  383. scheme=scheme,
  384. hosts=hosts,
  385. username=username,
  386. password=password,
  387. host=host,
  388. port=port,
  389. path=path,
  390. query=query,
  391. fragment=fragment,
  392. )
  393. )
  394. @classmethod
  395. def serialize_url(cls, url: Any, info: core_schema.SerializationInfo) -> str | Self:
  396. if not isinstance(url, cls):
  397. raise PydanticSerializationUnexpectedValue(
  398. f"Expected `{cls}` but got `{type(url)}` with value `'{url}'` - serialized value may not be as expected."
  399. )
  400. if info.mode == 'json':
  401. return str(url)
  402. return url
  403. @classmethod
  404. def __get_pydantic_core_schema__(
  405. cls, source: type[_BaseMultiHostUrl], handler: GetCoreSchemaHandler
  406. ) -> core_schema.CoreSchema:
  407. def wrap_val(v, h):
  408. if isinstance(v, source):
  409. return v
  410. if isinstance(v, _BaseMultiHostUrl):
  411. v = str(v)
  412. core_url = h(v)
  413. instance = source.__new__(source)
  414. instance._url = core_url
  415. return instance
  416. return core_schema.no_info_wrap_validator_function(
  417. wrap_val,
  418. schema=core_schema.multi_host_url_schema(**cls._constraints.defined_constraints),
  419. serialization=core_schema.plain_serializer_function_ser_schema(
  420. cls.serialize_url, info_arg=True, when_used='always'
  421. ),
  422. )
  423. @classmethod
  424. def __get_pydantic_json_schema__(
  425. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  426. ) -> JsonSchemaValue:
  427. # we use the url schema for json schema generation, but we might have to extract it from
  428. # the function-wrap schema we use as a tool for validation on initialization
  429. inner_schema = core_schema['schema'] if core_schema['type'] == 'function-wrap' else core_schema
  430. return handler(inner_schema)
  431. __pydantic_serializer__ = SchemaSerializer(core_schema.any_schema(serialization=core_schema.to_string_ser_schema()))
  432. @lru_cache
  433. def _build_type_adapter(cls: type[_BaseUrl | _BaseMultiHostUrl]) -> TypeAdapter:
  434. return TypeAdapter(cls)
  435. class AnyUrl(_BaseUrl):
  436. """Base type for all URLs.
  437. * Any scheme allowed
  438. * Top-level domain (TLD) not required
  439. * Host not required
  440. Assuming an input URL of `http://samuel:pass@example.com:8000/the/path/?query=here#fragment=is;this=bit`,
  441. the types export the following properties:
  442. - `scheme`: the URL scheme (`http`), always set.
  443. - `host`: the URL host (`example.com`).
  444. - `username`: optional username if included (`samuel`).
  445. - `password`: optional password if included (`pass`).
  446. - `port`: optional port (`8000`).
  447. - `path`: optional path (`/the/path/`).
  448. - `query`: optional URL query (for example, `GET` arguments or "search string", such as `query=here`).
  449. - `fragment`: optional fragment (`fragment=is;this=bit`).
  450. """
  451. # Note: all single host urls inherit from `AnyUrl` to preserve compatibility with pre-v2.10 code
  452. # Where urls were annotated variants of `AnyUrl`, which was an alias to `pydantic_core.Url`
  453. class AnyHttpUrl(AnyUrl):
  454. """A type that will accept any http or https URL.
  455. * TLD not required
  456. * Host not required
  457. """
  458. _constraints = UrlConstraints(allowed_schemes=['http', 'https'])
  459. class HttpUrl(AnyUrl):
  460. """A type that will accept any http or https URL.
  461. * TLD not required
  462. * Host not required
  463. * Max length 2083
  464. ```python
  465. from pydantic import BaseModel, HttpUrl, ValidationError
  466. class MyModel(BaseModel):
  467. url: HttpUrl
  468. m = MyModel(url='http://www.example.com') # (1)!
  469. print(m.url)
  470. #> http://www.example.com/
  471. try:
  472. MyModel(url='ftp://invalid.url')
  473. except ValidationError as e:
  474. print(e)
  475. '''
  476. 1 validation error for MyModel
  477. url
  478. URL scheme should be 'http' or 'https' [type=url_scheme, input_value='ftp://invalid.url', input_type=str]
  479. '''
  480. try:
  481. MyModel(url='not a url')
  482. except ValidationError as e:
  483. print(e)
  484. '''
  485. 1 validation error for MyModel
  486. url
  487. Input should be a valid URL, relative URL without a base [type=url_parsing, input_value='not a url', input_type=str]
  488. '''
  489. ```
  490. 1. Note: mypy would prefer `m = MyModel(url=HttpUrl('http://www.example.com'))`, but Pydantic will convert the string to an HttpUrl instance anyway.
  491. "International domains" (e.g. a URL where the host or TLD includes non-ascii characters) will be encoded via
  492. [punycode](https://en.wikipedia.org/wiki/Punycode) (see
  493. [this article](https://www.xudongz.com/blog/2017/idn-phishing/) for a good description of why this is important):
  494. ```python
  495. from pydantic import BaseModel, HttpUrl
  496. class MyModel(BaseModel):
  497. url: HttpUrl
  498. m1 = MyModel(url='http://puny£code.com')
  499. print(m1.url)
  500. #> http://xn--punycode-eja.com/
  501. m2 = MyModel(url='https://www.аррӏе.com/')
  502. print(m2.url)
  503. #> https://www.xn--80ak6aa92e.com/
  504. m3 = MyModel(url='https://www.example.珠宝/')
  505. print(m3.url)
  506. #> https://www.example.xn--pbt977c/
  507. ```
  508. !!! warning "Underscores in Hostnames"
  509. In Pydantic, underscores are allowed in all parts of a domain except the TLD.
  510. Technically this might be wrong - in theory the hostname cannot have underscores, but subdomains can.
  511. To explain this; consider the following two cases:
  512. - `exam_ple.co.uk`: the hostname is `exam_ple`, which should not be allowed since it contains an underscore.
  513. - `foo_bar.example.com` the hostname is `example`, which should be allowed since the underscore is in the subdomain.
  514. Without having an exhaustive list of TLDs, it would be impossible to differentiate between these two. Therefore
  515. underscores are allowed, but you can always do further validation in a validator if desired.
  516. Also, Chrome, Firefox, and Safari all currently accept `http://exam_ple.com` as a URL, so we're in good
  517. (or at least big) company.
  518. """
  519. _constraints = UrlConstraints(max_length=2083, allowed_schemes=['http', 'https'])
  520. class AnyWebsocketUrl(AnyUrl):
  521. """A type that will accept any ws or wss URL.
  522. * TLD not required
  523. * Host not required
  524. """
  525. _constraints = UrlConstraints(allowed_schemes=['ws', 'wss'])
  526. class WebsocketUrl(AnyUrl):
  527. """A type that will accept any ws or wss URL.
  528. * TLD not required
  529. * Host not required
  530. * Max length 2083
  531. """
  532. _constraints = UrlConstraints(max_length=2083, allowed_schemes=['ws', 'wss'])
  533. class FileUrl(AnyUrl):
  534. """A type that will accept any file URL.
  535. * Host not required
  536. """
  537. _constraints = UrlConstraints(allowed_schemes=['file'])
  538. class FtpUrl(AnyUrl):
  539. """A type that will accept ftp URL.
  540. * TLD not required
  541. * Host not required
  542. """
  543. _constraints = UrlConstraints(allowed_schemes=['ftp'])
  544. class PostgresDsn(_BaseMultiHostUrl):
  545. """A type that will accept any Postgres DSN.
  546. * User info required
  547. * TLD not required
  548. * Host required
  549. * Supports multiple hosts
  550. If further validation is required, these properties can be used by validators to enforce specific behaviour:
  551. ```python
  552. from pydantic import (
  553. BaseModel,
  554. HttpUrl,
  555. PostgresDsn,
  556. ValidationError,
  557. field_validator,
  558. )
  559. class MyModel(BaseModel):
  560. url: HttpUrl
  561. m = MyModel(url='http://www.example.com')
  562. # the repr() method for a url will display all properties of the url
  563. print(repr(m.url))
  564. #> HttpUrl('http://www.example.com/')
  565. print(m.url.scheme)
  566. #> http
  567. print(m.url.host)
  568. #> www.example.com
  569. print(m.url.port)
  570. #> 80
  571. class MyDatabaseModel(BaseModel):
  572. db: PostgresDsn
  573. @field_validator('db')
  574. def check_db_name(cls, v):
  575. assert v.path and len(v.path) > 1, 'database must be provided'
  576. return v
  577. m = MyDatabaseModel(db='postgres://user:pass@localhost:5432/foobar')
  578. print(m.db)
  579. #> postgres://user:pass@localhost:5432/foobar
  580. try:
  581. MyDatabaseModel(db='postgres://user:pass@localhost:5432')
  582. except ValidationError as e:
  583. print(e)
  584. '''
  585. 1 validation error for MyDatabaseModel
  586. db
  587. Assertion failed, database must be provided
  588. assert (None)
  589. + where None = PostgresDsn('postgres://user:pass@localhost:5432').path [type=assertion_error, input_value='postgres://user:pass@localhost:5432', input_type=str]
  590. '''
  591. ```
  592. """
  593. _constraints = UrlConstraints(
  594. host_required=True,
  595. allowed_schemes=[
  596. 'postgres',
  597. 'postgresql',
  598. 'postgresql+asyncpg',
  599. 'postgresql+pg8000',
  600. 'postgresql+psycopg',
  601. 'postgresql+psycopg2',
  602. 'postgresql+psycopg2cffi',
  603. 'postgresql+py-postgresql',
  604. 'postgresql+pygresql',
  605. ],
  606. )
  607. @property
  608. def host(self) -> str:
  609. """The required URL host."""
  610. return self._url.host # pyright: ignore[reportAttributeAccessIssue]
  611. class CockroachDsn(AnyUrl):
  612. """A type that will accept any Cockroach DSN.
  613. * User info required
  614. * TLD not required
  615. * Host required
  616. """
  617. _constraints = UrlConstraints(
  618. host_required=True,
  619. allowed_schemes=[
  620. 'cockroachdb',
  621. 'cockroachdb+psycopg2',
  622. 'cockroachdb+asyncpg',
  623. ],
  624. )
  625. @property
  626. def host(self) -> str:
  627. """The required URL host."""
  628. return self._url.host # pyright: ignore[reportReturnType]
  629. class AmqpDsn(AnyUrl):
  630. """A type that will accept any AMQP DSN.
  631. * User info required
  632. * TLD not required
  633. * Host not required
  634. """
  635. _constraints = UrlConstraints(allowed_schemes=['amqp', 'amqps'])
  636. class RedisDsn(AnyUrl):
  637. """A type that will accept any Redis DSN.
  638. * User info required
  639. * TLD not required
  640. * Host required (e.g., `rediss://:pass@localhost`)
  641. """
  642. _constraints = UrlConstraints(
  643. allowed_schemes=['redis', 'rediss'],
  644. default_host='localhost',
  645. default_port=6379,
  646. default_path='/0',
  647. host_required=True,
  648. )
  649. @property
  650. def host(self) -> str:
  651. """The required URL host."""
  652. return self._url.host # pyright: ignore[reportReturnType]
  653. class MongoDsn(_BaseMultiHostUrl):
  654. """A type that will accept any MongoDB DSN.
  655. * User info not required
  656. * Database name not required
  657. * Port not required
  658. * User info may be passed without user part (e.g., `mongodb://mongodb0.example.com:27017`).
  659. !!! warning
  660. If a port isn't specified, the default MongoDB port `27017` will be used. If this behavior is
  661. undesirable, you can use the following:
  662. ```python
  663. from typing import Annotated
  664. from pydantic import UrlConstraints
  665. from pydantic_core import MultiHostUrl
  666. MongoDsnNoDefaultPort = Annotated[
  667. MultiHostUrl,
  668. UrlConstraints(allowed_schemes=['mongodb', 'mongodb+srv']),
  669. ]
  670. ```
  671. """
  672. _constraints = UrlConstraints(allowed_schemes=['mongodb', 'mongodb+srv'], default_port=27017)
  673. class KafkaDsn(AnyUrl):
  674. """A type that will accept any Kafka DSN.
  675. * User info required
  676. * TLD not required
  677. * Host not required
  678. """
  679. _constraints = UrlConstraints(allowed_schemes=['kafka'], default_host='localhost', default_port=9092)
  680. class NatsDsn(_BaseMultiHostUrl):
  681. """A type that will accept any NATS DSN.
  682. NATS is a connective technology built for the ever increasingly hyper-connected world.
  683. It is a single technology that enables applications to securely communicate across
  684. any combination of cloud vendors, on-premise, edge, web and mobile, and devices.
  685. More: https://nats.io
  686. """
  687. _constraints = UrlConstraints(
  688. allowed_schemes=['nats', 'tls', 'ws', 'wss'], default_host='localhost', default_port=4222
  689. )
  690. class MySQLDsn(AnyUrl):
  691. """A type that will accept any MySQL DSN.
  692. * User info required
  693. * TLD not required
  694. * Host not required
  695. """
  696. _constraints = UrlConstraints(
  697. allowed_schemes=[
  698. 'mysql',
  699. 'mysql+mysqlconnector',
  700. 'mysql+aiomysql',
  701. 'mysql+asyncmy',
  702. 'mysql+mysqldb',
  703. 'mysql+pymysql',
  704. 'mysql+cymysql',
  705. 'mysql+pyodbc',
  706. ],
  707. default_port=3306,
  708. host_required=True,
  709. )
  710. class MariaDBDsn(AnyUrl):
  711. """A type that will accept any MariaDB DSN.
  712. * User info required
  713. * TLD not required
  714. * Host not required
  715. """
  716. _constraints = UrlConstraints(
  717. allowed_schemes=['mariadb', 'mariadb+mariadbconnector', 'mariadb+pymysql'],
  718. default_port=3306,
  719. )
  720. class ClickHouseDsn(AnyUrl):
  721. """A type that will accept any ClickHouse DSN.
  722. * User info required
  723. * TLD not required
  724. * Host not required
  725. """
  726. _constraints = UrlConstraints(
  727. allowed_schemes=[
  728. 'clickhouse+native',
  729. 'clickhouse+asynch',
  730. 'clickhouse+http',
  731. 'clickhouse',
  732. 'clickhouses',
  733. 'clickhousedb',
  734. ],
  735. default_host='localhost',
  736. default_port=9000,
  737. )
  738. class SnowflakeDsn(AnyUrl):
  739. """A type that will accept any Snowflake DSN.
  740. * User info required
  741. * TLD not required
  742. * Host required
  743. """
  744. _constraints = UrlConstraints(
  745. allowed_schemes=['snowflake'],
  746. host_required=True,
  747. )
  748. @property
  749. def host(self) -> str:
  750. """The required URL host."""
  751. return self._url.host # pyright: ignore[reportReturnType]
  752. def import_email_validator() -> None:
  753. global email_validator
  754. try:
  755. import email_validator
  756. except ImportError as e:
  757. raise ImportError("email-validator is not installed, run `pip install 'pydantic[email]'`") from e
  758. if not version('email-validator').partition('.')[0] == '2':
  759. raise ImportError('email-validator version >= 2.0 required, run pip install -U email-validator')
  760. if TYPE_CHECKING:
  761. EmailStr = Annotated[str, ...]
  762. else:
  763. class EmailStr:
  764. """
  765. Info:
  766. To use this type, you need to install the optional
  767. [`email-validator`](https://github.com/JoshData/python-email-validator) package:
  768. ```bash
  769. pip install email-validator
  770. ```
  771. Validate email addresses.
  772. ```python
  773. from pydantic import BaseModel, EmailStr
  774. class Model(BaseModel):
  775. email: EmailStr
  776. print(Model(email='contact@mail.com'))
  777. #> email='contact@mail.com'
  778. ```
  779. """ # noqa: D212
  780. @classmethod
  781. def __get_pydantic_core_schema__(
  782. cls,
  783. _source: type[Any],
  784. _handler: GetCoreSchemaHandler,
  785. ) -> core_schema.CoreSchema:
  786. import_email_validator()
  787. return core_schema.no_info_after_validator_function(cls._validate, core_schema.str_schema())
  788. @classmethod
  789. def __get_pydantic_json_schema__(
  790. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  791. ) -> JsonSchemaValue:
  792. field_schema = handler(core_schema)
  793. field_schema.update(type='string', format='email')
  794. return field_schema
  795. @classmethod
  796. def _validate(cls, input_value: str, /) -> str:
  797. return validate_email(input_value)[1]
  798. class NameEmail(_repr.Representation):
  799. """
  800. Info:
  801. To use this type, you need to install the optional
  802. [`email-validator`](https://github.com/JoshData/python-email-validator) package:
  803. ```bash
  804. pip install email-validator
  805. ```
  806. Validate a name and email address combination, as specified by
  807. [RFC 5322](https://datatracker.ietf.org/doc/html/rfc5322#section-3.4).
  808. The `NameEmail` has two properties: `name` and `email`.
  809. In case the `name` is not provided, it's inferred from the email address.
  810. ```python
  811. from pydantic import BaseModel, NameEmail
  812. class User(BaseModel):
  813. email: NameEmail
  814. user = User(email='Fred Bloggs <fred.bloggs@example.com>')
  815. print(user.email)
  816. #> Fred Bloggs <fred.bloggs@example.com>
  817. print(user.email.name)
  818. #> Fred Bloggs
  819. user = User(email='fred.bloggs@example.com')
  820. print(user.email)
  821. #> fred.bloggs <fred.bloggs@example.com>
  822. print(user.email.name)
  823. #> fred.bloggs
  824. ```
  825. """ # noqa: D212
  826. __slots__ = 'name', 'email'
  827. def __init__(self, name: str, email: str):
  828. self.name = name
  829. self.email = email
  830. def __eq__(self, other: Any) -> bool:
  831. return isinstance(other, NameEmail) and (self.name, self.email) == (other.name, other.email)
  832. @classmethod
  833. def __get_pydantic_json_schema__(
  834. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  835. ) -> JsonSchemaValue:
  836. field_schema = handler(core_schema)
  837. field_schema.update(type='string', format='name-email')
  838. return field_schema
  839. @classmethod
  840. def __get_pydantic_core_schema__(
  841. cls,
  842. _source: type[Any],
  843. _handler: GetCoreSchemaHandler,
  844. ) -> core_schema.CoreSchema:
  845. import_email_validator()
  846. return core_schema.no_info_after_validator_function(
  847. cls._validate,
  848. core_schema.json_or_python_schema(
  849. json_schema=core_schema.str_schema(),
  850. python_schema=core_schema.union_schema(
  851. [core_schema.is_instance_schema(cls), core_schema.str_schema()],
  852. custom_error_type='name_email_type',
  853. custom_error_message='Input is not a valid NameEmail',
  854. ),
  855. serialization=core_schema.to_string_ser_schema(),
  856. ),
  857. )
  858. @classmethod
  859. def _validate(cls, input_value: Self | str, /) -> Self:
  860. if isinstance(input_value, str):
  861. name, email = validate_email(input_value)
  862. return cls(name, email)
  863. else:
  864. return input_value
  865. def __str__(self) -> str:
  866. if '@' in self.name:
  867. return f'"{self.name}" <{self.email}>'
  868. return f'{self.name} <{self.email}>'
  869. IPvAnyAddressType: TypeAlias = 'IPv4Address | IPv6Address'
  870. IPvAnyInterfaceType: TypeAlias = 'IPv4Interface | IPv6Interface'
  871. IPvAnyNetworkType: TypeAlias = 'IPv4Network | IPv6Network'
  872. if TYPE_CHECKING:
  873. IPvAnyAddress = IPvAnyAddressType
  874. IPvAnyInterface = IPvAnyInterfaceType
  875. IPvAnyNetwork = IPvAnyNetworkType
  876. else:
  877. class IPvAnyAddress:
  878. """Validate an IPv4 or IPv6 address.
  879. ```python
  880. from pydantic import BaseModel
  881. from pydantic.networks import IPvAnyAddress
  882. class IpModel(BaseModel):
  883. ip: IPvAnyAddress
  884. print(IpModel(ip='127.0.0.1'))
  885. #> ip=IPv4Address('127.0.0.1')
  886. try:
  887. IpModel(ip='http://www.example.com')
  888. except ValueError as e:
  889. print(e.errors())
  890. '''
  891. [
  892. {
  893. 'type': 'ip_any_address',
  894. 'loc': ('ip',),
  895. 'msg': 'value is not a valid IPv4 or IPv6 address',
  896. 'input': 'http://www.example.com',
  897. }
  898. ]
  899. '''
  900. ```
  901. """
  902. __slots__ = ()
  903. def __new__(cls, value: Any) -> IPvAnyAddressType:
  904. """Validate an IPv4 or IPv6 address."""
  905. try:
  906. return IPv4Address(value)
  907. except ValueError:
  908. pass
  909. try:
  910. return IPv6Address(value)
  911. except ValueError:
  912. raise PydanticCustomError('ip_any_address', 'value is not a valid IPv4 or IPv6 address')
  913. @classmethod
  914. def __get_pydantic_json_schema__(
  915. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  916. ) -> JsonSchemaValue:
  917. field_schema = {}
  918. field_schema.update(type='string', format='ipvanyaddress')
  919. return field_schema
  920. @classmethod
  921. def __get_pydantic_core_schema__(
  922. cls,
  923. _source: type[Any],
  924. _handler: GetCoreSchemaHandler,
  925. ) -> core_schema.CoreSchema:
  926. return core_schema.no_info_plain_validator_function(
  927. cls._validate, serialization=core_schema.to_string_ser_schema()
  928. )
  929. @classmethod
  930. def _validate(cls, input_value: Any, /) -> IPvAnyAddressType:
  931. return cls(input_value) # type: ignore[return-value]
  932. class IPvAnyInterface:
  933. """Validate an IPv4 or IPv6 interface."""
  934. __slots__ = ()
  935. def __new__(cls, value: NetworkType) -> IPvAnyInterfaceType:
  936. """Validate an IPv4 or IPv6 interface."""
  937. try:
  938. return IPv4Interface(value)
  939. except ValueError:
  940. pass
  941. try:
  942. return IPv6Interface(value)
  943. except ValueError:
  944. raise PydanticCustomError('ip_any_interface', 'value is not a valid IPv4 or IPv6 interface')
  945. @classmethod
  946. def __get_pydantic_json_schema__(
  947. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  948. ) -> JsonSchemaValue:
  949. field_schema = {}
  950. field_schema.update(type='string', format='ipvanyinterface')
  951. return field_schema
  952. @classmethod
  953. def __get_pydantic_core_schema__(
  954. cls,
  955. _source: type[Any],
  956. _handler: GetCoreSchemaHandler,
  957. ) -> core_schema.CoreSchema:
  958. return core_schema.no_info_plain_validator_function(
  959. cls._validate, serialization=core_schema.to_string_ser_schema()
  960. )
  961. @classmethod
  962. def _validate(cls, input_value: NetworkType, /) -> IPvAnyInterfaceType:
  963. return cls(input_value) # type: ignore[return-value]
  964. class IPvAnyNetwork:
  965. """Validate an IPv4 or IPv6 network."""
  966. __slots__ = ()
  967. def __new__(cls, value: NetworkType) -> IPvAnyNetworkType:
  968. """Validate an IPv4 or IPv6 network."""
  969. # Assume IP Network is defined with a default value for `strict` argument.
  970. # Define your own class if you want to specify network address check strictness.
  971. try:
  972. return IPv4Network(value)
  973. except ValueError:
  974. pass
  975. try:
  976. return IPv6Network(value)
  977. except ValueError:
  978. raise PydanticCustomError('ip_any_network', 'value is not a valid IPv4 or IPv6 network')
  979. @classmethod
  980. def __get_pydantic_json_schema__(
  981. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  982. ) -> JsonSchemaValue:
  983. field_schema = {}
  984. field_schema.update(type='string', format='ipvanynetwork')
  985. return field_schema
  986. @classmethod
  987. def __get_pydantic_core_schema__(
  988. cls,
  989. _source: type[Any],
  990. _handler: GetCoreSchemaHandler,
  991. ) -> core_schema.CoreSchema:
  992. return core_schema.no_info_plain_validator_function(
  993. cls._validate, serialization=core_schema.to_string_ser_schema()
  994. )
  995. @classmethod
  996. def _validate(cls, input_value: NetworkType, /) -> IPvAnyNetworkType:
  997. return cls(input_value) # type: ignore[return-value]
  998. def _build_pretty_email_regex() -> re.Pattern[str]:
  999. name_chars = r'[\w!#$%&\'*+\-/=?^_`{|}~]'
  1000. unquoted_name_group = rf'((?:{name_chars}+\s+)*{name_chars}+)'
  1001. quoted_name_group = r'"((?:[^"]|\")+)"'
  1002. email_group = r'<(.+)>'
  1003. return re.compile(rf'\s*(?:{unquoted_name_group}|{quoted_name_group})?\s*{email_group}\s*')
  1004. pretty_email_regex = _build_pretty_email_regex()
  1005. MAX_EMAIL_LENGTH = 2048
  1006. """Maximum length for an email.
  1007. A somewhat arbitrary but very generous number compared to what is allowed by most implementations.
  1008. """
  1009. def validate_email(value: str) -> tuple[str, str]:
  1010. """Email address validation using [email-validator](https://pypi.org/project/email-validator/).
  1011. Returns:
  1012. A tuple containing the local part of the email (or the name for "pretty" email addresses)
  1013. and the normalized email.
  1014. Raises:
  1015. PydanticCustomError: If the email is invalid.
  1016. Note:
  1017. Note that:
  1018. * Raw IP address (literal) domain parts are not allowed.
  1019. * `"John Doe <local_part@domain.com>"` style "pretty" email addresses are processed.
  1020. * Spaces are striped from the beginning and end of addresses, but no error is raised.
  1021. """
  1022. if email_validator is None:
  1023. import_email_validator()
  1024. if len(value) > MAX_EMAIL_LENGTH:
  1025. raise PydanticCustomError(
  1026. 'value_error',
  1027. 'value is not a valid email address: {reason}',
  1028. {'reason': f'Length must not exceed {MAX_EMAIL_LENGTH} characters'},
  1029. )
  1030. m = pretty_email_regex.fullmatch(value)
  1031. name: str | None = None
  1032. if m:
  1033. unquoted_name, quoted_name, value = m.groups()
  1034. name = unquoted_name or quoted_name
  1035. email = value.strip()
  1036. try:
  1037. parts = email_validator.validate_email(email, check_deliverability=False)
  1038. except email_validator.EmailNotValidError as e:
  1039. raise PydanticCustomError(
  1040. 'value_error', 'value is not a valid email address: {reason}', {'reason': str(e.args[0])}
  1041. ) from e
  1042. email = parts.normalized
  1043. assert email is not None
  1044. name = name or parts.local_part
  1045. return name, email
  1046. __getattr__ = getattr_migration(__name__)