networks.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
  6. from typing import TYPE_CHECKING, Any
  7. from pydantic_core import MultiHostUrl, PydanticCustomError, Url, core_schema
  8. from typing_extensions import Annotated, TypeAlias
  9. from ._internal import _fields, _repr, _schema_generation_shared
  10. from ._migration import getattr_migration
  11. from .annotated_handlers import GetCoreSchemaHandler
  12. from .json_schema import JsonSchemaValue
  13. if TYPE_CHECKING:
  14. import email_validator
  15. NetworkType: TypeAlias = 'str | bytes | int | tuple[str | bytes | int, str | int]'
  16. else:
  17. email_validator = None
  18. __all__ = [
  19. 'AnyUrl',
  20. 'AnyHttpUrl',
  21. 'FileUrl',
  22. 'HttpUrl',
  23. 'UrlConstraints',
  24. 'EmailStr',
  25. 'NameEmail',
  26. 'IPvAnyAddress',
  27. 'IPvAnyInterface',
  28. 'IPvAnyNetwork',
  29. 'PostgresDsn',
  30. 'CockroachDsn',
  31. 'AmqpDsn',
  32. 'RedisDsn',
  33. 'MongoDsn',
  34. 'KafkaDsn',
  35. 'validate_email',
  36. 'MySQLDsn',
  37. 'MariaDBDsn',
  38. ]
  39. @_dataclasses.dataclass
  40. class UrlConstraints(_fields.PydanticMetadata):
  41. """Url constraints.
  42. Attributes:
  43. max_length: The maximum length of the url. Defaults to `None`.
  44. allowed_schemes: The allowed schemes. Defaults to `None`.
  45. host_required: Whether the host is required. Defaults to `None`.
  46. default_host: The default host. Defaults to `None`.
  47. default_port: The default port. Defaults to `None`.
  48. default_path: The default path. Defaults to `None`.
  49. """
  50. max_length: int | None = None
  51. allowed_schemes: list[str] | None = None
  52. host_required: bool | None = None
  53. default_host: str | None = None
  54. default_port: int | None = None
  55. default_path: str | None = None
  56. def __hash__(self) -> int:
  57. return hash(
  58. (
  59. self.max_length,
  60. tuple(self.allowed_schemes) if self.allowed_schemes is not None else None,
  61. self.host_required,
  62. self.default_host,
  63. self.default_port,
  64. self.default_path,
  65. )
  66. )
  67. AnyUrl = Url
  68. """Base type for all URLs.
  69. * Any scheme allowed
  70. * Top-level domain (TLD) not required
  71. * Host required
  72. Assuming an input URL of `http://samuel:pass@example.com:8000/the/path/?query=here#fragment=is;this=bit`,
  73. the types export the following properties:
  74. - `scheme`: the URL scheme (`http`), always set.
  75. - `host`: the URL host (`example.com`), always set.
  76. - `username`: optional username if included (`samuel`).
  77. - `password`: optional password if included (`pass`).
  78. - `port`: optional port (`8000`).
  79. - `path`: optional path (`/the/path/`).
  80. - `query`: optional URL query (for example, `GET` arguments or "search string", such as `query=here`).
  81. - `fragment`: optional fragment (`fragment=is;this=bit`).
  82. """
  83. AnyHttpUrl = Annotated[Url, UrlConstraints(allowed_schemes=['http', 'https'])]
  84. """A type that will accept any http or https URL.
  85. * TLD not required
  86. * Host required
  87. """
  88. HttpUrl = Annotated[Url, UrlConstraints(max_length=2083, allowed_schemes=['http', 'https'])]
  89. """A type that will accept any http or https URL.
  90. * TLD required
  91. * Host required
  92. * Max length 2083
  93. ```py
  94. from pydantic import BaseModel, HttpUrl, ValidationError
  95. class MyModel(BaseModel):
  96. url: HttpUrl
  97. m = MyModel(url='http://www.example.com')
  98. print(m.url)
  99. #> http://www.example.com/
  100. try:
  101. MyModel(url='ftp://invalid.url')
  102. except ValidationError as e:
  103. print(e)
  104. '''
  105. 1 validation error for MyModel
  106. url
  107. URL scheme should be 'http' or 'https' [type=url_scheme, input_value='ftp://invalid.url', input_type=str]
  108. '''
  109. try:
  110. MyModel(url='not a url')
  111. except ValidationError as e:
  112. print(e)
  113. '''
  114. 1 validation error for MyModel
  115. url
  116. Input should be a valid URL, relative URL without a base [type=url_parsing, input_value='not a url', input_type=str]
  117. '''
  118. ```
  119. "International domains" (e.g. a URL where the host or TLD includes non-ascii characters) will be encoded via
  120. [punycode](https://en.wikipedia.org/wiki/Punycode) (see
  121. [this article](https://www.xudongz.com/blog/2017/idn-phishing/) for a good description of why this is important):
  122. ```py
  123. from pydantic import BaseModel, HttpUrl
  124. class MyModel(BaseModel):
  125. url: HttpUrl
  126. m1 = MyModel(url='http://puny£code.com')
  127. print(m1.url)
  128. #> http://xn--punycode-eja.com/
  129. m2 = MyModel(url='https://www.аррӏе.com/')
  130. print(m2.url)
  131. #> https://www.xn--80ak6aa92e.com/
  132. m3 = MyModel(url='https://www.example.珠宝/')
  133. print(m3.url)
  134. #> https://www.example.xn--pbt977c/
  135. ```
  136. !!! warning "Underscores in Hostnames"
  137. In Pydantic, underscores are allowed in all parts of a domain except the TLD.
  138. Technically this might be wrong - in theory the hostname cannot have underscores, but subdomains can.
  139. To explain this; consider the following two cases:
  140. - `exam_ple.co.uk`: the hostname is `exam_ple`, which should not be allowed since it contains an underscore.
  141. - `foo_bar.example.com` the hostname is `example`, which should be allowed since the underscore is in the subdomain.
  142. Without having an exhaustive list of TLDs, it would be impossible to differentiate between these two. Therefore
  143. underscores are allowed, but you can always do further validation in a validator if desired.
  144. Also, Chrome, Firefox, and Safari all currently accept `http://exam_ple.com` as a URL, so we're in good
  145. (or at least big) company.
  146. """
  147. FileUrl = Annotated[Url, UrlConstraints(allowed_schemes=['file'])]
  148. """A type that will accept any file URL.
  149. * Host not required
  150. """
  151. PostgresDsn = Annotated[
  152. MultiHostUrl,
  153. UrlConstraints(
  154. host_required=True,
  155. allowed_schemes=[
  156. 'postgres',
  157. 'postgresql',
  158. 'postgresql+asyncpg',
  159. 'postgresql+pg8000',
  160. 'postgresql+psycopg',
  161. 'postgresql+psycopg2',
  162. 'postgresql+psycopg2cffi',
  163. 'postgresql+py-postgresql',
  164. 'postgresql+pygresql',
  165. ],
  166. ),
  167. ]
  168. """A type that will accept any Postgres DSN.
  169. * User info required
  170. * TLD not required
  171. * Host required
  172. * Supports multiple hosts
  173. If further validation is required, these properties can be used by validators to enforce specific behaviour:
  174. ```py
  175. from pydantic import (
  176. BaseModel,
  177. HttpUrl,
  178. PostgresDsn,
  179. ValidationError,
  180. field_validator,
  181. )
  182. class MyModel(BaseModel):
  183. url: HttpUrl
  184. m = MyModel(url='http://www.example.com')
  185. # the repr() method for a url will display all properties of the url
  186. print(repr(m.url))
  187. #> Url('http://www.example.com/')
  188. print(m.url.scheme)
  189. #> http
  190. print(m.url.host)
  191. #> www.example.com
  192. print(m.url.port)
  193. #> 80
  194. class MyDatabaseModel(BaseModel):
  195. db: PostgresDsn
  196. @field_validator('db')
  197. def check_db_name(cls, v):
  198. assert v.path and len(v.path) > 1, 'database must be provided'
  199. return v
  200. m = MyDatabaseModel(db='postgres://user:pass@localhost:5432/foobar')
  201. print(m.db)
  202. #> postgres://user:pass@localhost:5432/foobar
  203. try:
  204. MyDatabaseModel(db='postgres://user:pass@localhost:5432')
  205. except ValidationError as e:
  206. print(e)
  207. '''
  208. 1 validation error for MyDatabaseModel
  209. db
  210. Assertion failed, database must be provided
  211. assert (None)
  212. + where None = MultiHostUrl('postgres://user:pass@localhost:5432').path [type=assertion_error, input_value='postgres://user:pass@localhost:5432', input_type=str]
  213. '''
  214. ```
  215. """
  216. CockroachDsn = Annotated[
  217. Url,
  218. UrlConstraints(
  219. host_required=True,
  220. allowed_schemes=[
  221. 'cockroachdb',
  222. 'cockroachdb+psycopg2',
  223. 'cockroachdb+asyncpg',
  224. ],
  225. ),
  226. ]
  227. """A type that will accept any Cockroach DSN.
  228. * User info required
  229. * TLD not required
  230. * Host required
  231. """
  232. AmqpDsn = Annotated[Url, UrlConstraints(allowed_schemes=['amqp', 'amqps'])]
  233. """A type that will accept any AMQP DSN.
  234. * User info required
  235. * TLD not required
  236. * Host required
  237. """
  238. RedisDsn = Annotated[
  239. Url,
  240. UrlConstraints(allowed_schemes=['redis', 'rediss'], default_host='localhost', default_port=6379, default_path='/0'),
  241. ]
  242. """A type that will accept any Redis DSN.
  243. * User info required
  244. * TLD not required
  245. * Host required (e.g., `rediss://:pass@localhost`)
  246. """
  247. MongoDsn = Annotated[MultiHostUrl, UrlConstraints(allowed_schemes=['mongodb', 'mongodb+srv'], default_port=27017)]
  248. """A type that will accept any MongoDB DSN.
  249. * User info not required
  250. * Database name not required
  251. * Port not required
  252. * User info may be passed without user part (e.g., `mongodb://mongodb0.example.com:27017`).
  253. """
  254. KafkaDsn = Annotated[Url, UrlConstraints(allowed_schemes=['kafka'], default_host='localhost', default_port=9092)]
  255. """A type that will accept any Kafka DSN.
  256. * User info required
  257. * TLD not required
  258. * Host required
  259. """
  260. MySQLDsn = Annotated[
  261. Url,
  262. UrlConstraints(
  263. allowed_schemes=[
  264. 'mysql',
  265. 'mysql+mysqlconnector',
  266. 'mysql+aiomysql',
  267. 'mysql+asyncmy',
  268. 'mysql+mysqldb',
  269. 'mysql+pymysql',
  270. 'mysql+cymysql',
  271. 'mysql+pyodbc',
  272. ],
  273. default_port=3306,
  274. ),
  275. ]
  276. """A type that will accept any MySQL DSN.
  277. * User info required
  278. * TLD not required
  279. * Host required
  280. """
  281. MariaDBDsn = Annotated[
  282. Url,
  283. UrlConstraints(
  284. allowed_schemes=['mariadb', 'mariadb+mariadbconnector', 'mariadb+pymysql'],
  285. default_port=3306,
  286. ),
  287. ]
  288. """A type that will accept any MariaDB DSN.
  289. * User info required
  290. * TLD not required
  291. * Host required
  292. """
  293. def import_email_validator() -> None:
  294. global email_validator
  295. try:
  296. import email_validator
  297. except ImportError as e:
  298. raise ImportError('email-validator is not installed, run `pip install pydantic[email]`') from e
  299. if TYPE_CHECKING:
  300. EmailStr = Annotated[str, ...]
  301. else:
  302. class EmailStr:
  303. """
  304. Info:
  305. To use this type, you need to install the optional
  306. [`email-validator`](https://github.com/JoshData/python-email-validator) package:
  307. ```bash
  308. pip install email-validator
  309. ```
  310. Validate email addresses.
  311. ```py
  312. from pydantic import BaseModel, EmailStr
  313. class Model(BaseModel):
  314. email: EmailStr
  315. print(Model(email='contact@mail.com'))
  316. #> email='contact@mail.com'
  317. ```
  318. """ # noqa: D212
  319. @classmethod
  320. def __get_pydantic_core_schema__(
  321. cls,
  322. _source: type[Any],
  323. _handler: GetCoreSchemaHandler,
  324. ) -> core_schema.CoreSchema:
  325. import_email_validator()
  326. return core_schema.no_info_after_validator_function(cls._validate, core_schema.str_schema())
  327. @classmethod
  328. def __get_pydantic_json_schema__(
  329. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  330. ) -> JsonSchemaValue:
  331. field_schema = handler(core_schema)
  332. field_schema.update(type='string', format='email')
  333. return field_schema
  334. @classmethod
  335. def _validate(cls, __input_value: str) -> str:
  336. return validate_email(__input_value)[1]
  337. class NameEmail(_repr.Representation):
  338. """
  339. Info:
  340. To use this type, you need to install the optional
  341. [`email-validator`](https://github.com/JoshData/python-email-validator) package:
  342. ```bash
  343. pip install email-validator
  344. ```
  345. Validate a name and email address combination, as specified by
  346. [RFC 5322](https://datatracker.ietf.org/doc/html/rfc5322#section-3.4).
  347. The `NameEmail` has two properties: `name` and `email`.
  348. In case the `name` is not provided, it's inferred from the email address.
  349. ```py
  350. from pydantic import BaseModel, NameEmail
  351. class User(BaseModel):
  352. email: NameEmail
  353. user = User(email='Fred Bloggs <fred.bloggs@example.com>')
  354. print(user.email)
  355. #> Fred Bloggs <fred.bloggs@example.com>
  356. print(user.email.name)
  357. #> Fred Bloggs
  358. user = User(email='fred.bloggs@example.com')
  359. print(user.email)
  360. #> fred.bloggs <fred.bloggs@example.com>
  361. print(user.email.name)
  362. #> fred.bloggs
  363. ```
  364. """ # noqa: D212
  365. __slots__ = 'name', 'email'
  366. def __init__(self, name: str, email: str):
  367. self.name = name
  368. self.email = email
  369. def __eq__(self, other: Any) -> bool:
  370. return isinstance(other, NameEmail) and (self.name, self.email) == (other.name, other.email)
  371. @classmethod
  372. def __get_pydantic_json_schema__(
  373. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  374. ) -> JsonSchemaValue:
  375. field_schema = handler(core_schema)
  376. field_schema.update(type='string', format='name-email')
  377. return field_schema
  378. @classmethod
  379. def __get_pydantic_core_schema__(
  380. cls,
  381. _source: type[Any],
  382. _handler: GetCoreSchemaHandler,
  383. ) -> core_schema.CoreSchema:
  384. import_email_validator()
  385. return core_schema.no_info_after_validator_function(
  386. cls._validate,
  387. core_schema.union_schema(
  388. [core_schema.is_instance_schema(cls), core_schema.str_schema()],
  389. custom_error_type='name_email_type',
  390. custom_error_message='Input is not a valid NameEmail',
  391. ),
  392. serialization=core_schema.to_string_ser_schema(),
  393. )
  394. @classmethod
  395. def _validate(cls, __input_value: NameEmail | str) -> NameEmail:
  396. if isinstance(__input_value, cls):
  397. return __input_value
  398. else:
  399. name, email = validate_email(__input_value) # type: ignore[arg-type]
  400. return cls(name, email)
  401. def __str__(self) -> str:
  402. return f'{self.name} <{self.email}>'
  403. class IPvAnyAddress:
  404. """Validate an IPv4 or IPv6 address.
  405. ```py
  406. from pydantic import BaseModel
  407. from pydantic.networks import IPvAnyAddress
  408. class IpModel(BaseModel):
  409. ip: IPvAnyAddress
  410. print(IpModel(ip='127.0.0.1'))
  411. #> ip=IPv4Address('127.0.0.1')
  412. try:
  413. IpModel(ip='http://www.example.com')
  414. except ValueError as e:
  415. print(e.errors())
  416. '''
  417. [
  418. {
  419. 'type': 'ip_any_address',
  420. 'loc': ('ip',),
  421. 'msg': 'value is not a valid IPv4 or IPv6 address',
  422. 'input': 'http://www.example.com',
  423. }
  424. ]
  425. '''
  426. ```
  427. """
  428. __slots__ = ()
  429. def __new__(cls, value: Any) -> IPv4Address | IPv6Address:
  430. """Validate an IPv4 or IPv6 address."""
  431. try:
  432. return IPv4Address(value)
  433. except ValueError:
  434. pass
  435. try:
  436. return IPv6Address(value)
  437. except ValueError:
  438. raise PydanticCustomError('ip_any_address', 'value is not a valid IPv4 or IPv6 address')
  439. @classmethod
  440. def __get_pydantic_json_schema__(
  441. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  442. ) -> JsonSchemaValue:
  443. field_schema = {}
  444. field_schema.update(type='string', format='ipvanyaddress')
  445. return field_schema
  446. @classmethod
  447. def __get_pydantic_core_schema__(
  448. cls,
  449. _source: type[Any],
  450. _handler: GetCoreSchemaHandler,
  451. ) -> core_schema.CoreSchema:
  452. return core_schema.no_info_plain_validator_function(
  453. cls._validate, serialization=core_schema.to_string_ser_schema()
  454. )
  455. @classmethod
  456. def _validate(cls, __input_value: Any) -> IPv4Address | IPv6Address:
  457. return cls(__input_value) # type: ignore[return-value]
  458. class IPvAnyInterface:
  459. """Validate an IPv4 or IPv6 interface."""
  460. __slots__ = ()
  461. def __new__(cls, value: NetworkType) -> IPv4Interface | IPv6Interface:
  462. """Validate an IPv4 or IPv6 interface."""
  463. try:
  464. return IPv4Interface(value)
  465. except ValueError:
  466. pass
  467. try:
  468. return IPv6Interface(value)
  469. except ValueError:
  470. raise PydanticCustomError('ip_any_interface', 'value is not a valid IPv4 or IPv6 interface')
  471. @classmethod
  472. def __get_pydantic_json_schema__(
  473. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  474. ) -> JsonSchemaValue:
  475. field_schema = {}
  476. field_schema.update(type='string', format='ipvanyinterface')
  477. return field_schema
  478. @classmethod
  479. def __get_pydantic_core_schema__(
  480. cls,
  481. _source: type[Any],
  482. _handler: GetCoreSchemaHandler,
  483. ) -> core_schema.CoreSchema:
  484. return core_schema.no_info_plain_validator_function(
  485. cls._validate, serialization=core_schema.to_string_ser_schema()
  486. )
  487. @classmethod
  488. def _validate(cls, __input_value: NetworkType) -> IPv4Interface | IPv6Interface:
  489. return cls(__input_value) # type: ignore[return-value]
  490. class IPvAnyNetwork:
  491. """Validate an IPv4 or IPv6 network."""
  492. __slots__ = ()
  493. def __new__(cls, value: NetworkType) -> IPv4Network | IPv6Network:
  494. """Validate an IPv4 or IPv6 network."""
  495. # Assume IP Network is defined with a default value for `strict` argument.
  496. # Define your own class if you want to specify network address check strictness.
  497. try:
  498. return IPv4Network(value)
  499. except ValueError:
  500. pass
  501. try:
  502. return IPv6Network(value)
  503. except ValueError:
  504. raise PydanticCustomError('ip_any_network', 'value is not a valid IPv4 or IPv6 network')
  505. @classmethod
  506. def __get_pydantic_json_schema__(
  507. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  508. ) -> JsonSchemaValue:
  509. field_schema = {}
  510. field_schema.update(type='string', format='ipvanynetwork')
  511. return field_schema
  512. @classmethod
  513. def __get_pydantic_core_schema__(
  514. cls,
  515. _source: type[Any],
  516. _handler: GetCoreSchemaHandler,
  517. ) -> core_schema.CoreSchema:
  518. return core_schema.no_info_plain_validator_function(
  519. cls._validate, serialization=core_schema.to_string_ser_schema()
  520. )
  521. @classmethod
  522. def _validate(cls, __input_value: NetworkType) -> IPv4Network | IPv6Network:
  523. return cls(__input_value) # type: ignore[return-value]
  524. def _build_pretty_email_regex() -> re.Pattern[str]:
  525. name_chars = r'[\w!#$%&\'*+\-/=?^_`{|}~]'
  526. unquoted_name_group = fr'((?:{name_chars}+\s+)*{name_chars}+)'
  527. quoted_name_group = r'"((?:[^"]|\")+)"'
  528. email_group = r'<\s*(.+)\s*>'
  529. return re.compile(rf'\s*(?:{unquoted_name_group}|{quoted_name_group})?\s*{email_group}\s*')
  530. pretty_email_regex = _build_pretty_email_regex()
  531. MAX_EMAIL_LENGTH = 2048
  532. """Maximum length for an email.
  533. A somewhat arbitrary but very generous number compared to what is allowed by most implementations.
  534. """
  535. def validate_email(value: str) -> tuple[str, str]:
  536. """Email address validation using [email-validator](https://pypi.org/project/email-validator/).
  537. Note:
  538. Note that:
  539. * Raw IP address (literal) domain parts are not allowed.
  540. * `"John Doe <local_part@domain.com>"` style "pretty" email addresses are processed.
  541. * Spaces are striped from the beginning and end of addresses, but no error is raised.
  542. """
  543. if email_validator is None:
  544. import_email_validator()
  545. if len(value) > MAX_EMAIL_LENGTH:
  546. raise PydanticCustomError(
  547. 'value_error',
  548. 'value is not a valid email address: {reason}',
  549. {'reason': f'Length must not exceed {MAX_EMAIL_LENGTH} characters'},
  550. )
  551. m = pretty_email_regex.fullmatch(value)
  552. name: str | None = None
  553. if m:
  554. unquoted_name, quoted_name, value = m.groups()
  555. name = unquoted_name or quoted_name
  556. email = value.strip()
  557. try:
  558. parts = email_validator.validate_email(email, check_deliverability=False)
  559. except email_validator.EmailNotValidError as e:
  560. raise PydanticCustomError(
  561. 'value_error', 'value is not a valid email address: {reason}', {'reason': str(e.args[0])}
  562. ) from e
  563. email = parts.normalized
  564. assert email is not None
  565. name = name or parts.local_part
  566. return name, email
  567. __getattr__ = getattr_migration(__name__)