Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

1313 righe
40 KiB

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