Skip to content

Network Types

The networks module contains types for common network-related fields.

AnyUrl module-attribute

AnyUrl = Url

Base type for all URLs.

AnyHttpUrl module-attribute

AnyHttpUrl = Annotated[
    Url, UrlConstraints(allowed_schemes=["http", "https"])
]

A type that will accept any http or https URL.

HttpUrl module-attribute

HttpUrl = Annotated[
    Url,
    UrlConstraints(
        max_length=2083, allowed_schemes=["http", "https"]
    ),
]

A type that will accept any http or https URL with a max length of 2083 characters.

FileUrl module-attribute

FileUrl = Annotated[
    Url, UrlConstraints(allowed_schemes=["file"])
]

A type that will accept any file URL.

PostgresDsn module-attribute

PostgresDsn = Annotated[
    MultiHostUrl,
    UrlConstraints(
        host_required=True,
        allowed_schemes=[
            "postgres",
            "postgresql",
            "postgresql+asyncpg",
            "postgresql+pg8000",
            "postgresql+psycopg",
            "postgresql+psycopg2",
            "postgresql+psycopg2cffi",
            "postgresql+py-postgresql",
            "postgresql+pygresql",
        ],
    ),
]

A type that will accept any Postgres DSN.

CockroachDsn module-attribute

CockroachDsn = Annotated[
    Url,
    UrlConstraints(
        host_required=True,
        allowed_schemes=[
            "cockroachdb",
            "cockroachdb+psycopg2",
            "cockroachdb+asyncpg",
        ],
    ),
]

A type that will accept any Cockroach DSN.

AmqpDsn module-attribute

AmqpDsn = Annotated[
    Url, UrlConstraints(allowed_schemes=["amqp", "amqps"])
]

A type that will accept any AMQP DSN.

RedisDsn module-attribute

RedisDsn = Annotated[
    Url,
    UrlConstraints(
        allowed_schemes=["redis", "rediss"],
        default_host="localhost",
        default_port=6379,
        default_path="/0",
    ),
]

A type that will accept any Redis DSN.

MongoDsn module-attribute

MongoDsn = Annotated[
    MultiHostUrl,
    UrlConstraints(
        allowed_schemes=["mongodb", "mongodb+srv"],
        default_port=27017,
    ),
]

A type that will accept any MongoDB DSN.

KafkaDsn module-attribute

KafkaDsn = Annotated[
    Url,
    UrlConstraints(
        allowed_schemes=["kafka"],
        default_host="localhost",
        default_port=9092,
    ),
]

A type that will accept any Kafka DSN.

MySQLDsn module-attribute

MySQLDsn = Annotated[
    Url,
    UrlConstraints(
        allowed_schemes=[
            "mysql",
            "mysql+mysqlconnector",
            "mysql+aiomysql",
            "mysql+asyncmy",
            "mysql+mysqldb",
            "mysql+pymysql",
            "mysql+cymysql",
            "mysql+pyodbc",
        ],
        default_port=3306,
    ),
]

A type that will accept any MySQL DSN.

MariaDBDsn module-attribute

MariaDBDsn = Annotated[
    Url,
    UrlConstraints(
        allowed_schemes=[
            "mariadb",
            "mariadb+mariadbconnector",
            "mariadb+pymysql",
        ],
        default_port=3306,
    ),
]

A type that will accept any MariaDB DSN.

UrlConstraints dataclass

Bases: _fields.PydanticMetadata

Url constraints.

Attributes:

Name Type Description
max_length int | None

The maximum length of the url. Defaults to None.

allowed_schemes list[str] | None

The allowed schemes. Defaults to None.

host_required bool | None

Whether the host is required. Defaults to None.

default_host str | None

The default host. Defaults to None.

default_port int | None

The default port. Defaults to None.

default_path str | None

The default path. Defaults to None.

EmailStr

Validate email addresses.

Example
from pydantic import BaseModel, EmailStr

class Model(BaseModel):
    email: EmailStr

print(Model(email='[email protected]'))
#> email='[email protected]'

NameEmail

NameEmail(name, email)

Bases: _repr.Representation

Validate a name and email address combination.

Example
from pydantic import BaseModel, NameEmail

class User(BaseModel):
    email: NameEmail

print(User(email='John Doe <[email protected]>'))
#> email=NameEmail(name='John Doe', email='[email protected]')

Attributes:

Name Type Description
name

The name.

email

The email address.

Source code in pydantic/networks.py
227
228
229
def __init__(self, name: str, email: str):
    self.name = name
    self.email = email

IPvAnyAddress

Validate an IPv4 or IPv6 address.

IPvAnyInterface

Validate an IPv4 or IPv6 interface.

IPvAnyNetwork

Validate an IPv4 or IPv6 network.

validate_email

validate_email(value)

Email address validation using https://pypi.org/project/email-validator/.

Note

Note that:

  • Raw IP address (literal) domain parts are not allowed.
  • "John Doe local_part@domain.com" style "pretty" email addresses are processed.
  • Spaces are striped from the beginning and end of addresses, but no error is raised.
Source code in pydantic/networks.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
def validate_email(value: str) -> tuple[str, str]:
    """Email address validation using https://pypi.org/project/email-validator/.

    Note:
        Note that:

        * Raw IP address (literal) domain parts are not allowed.
        * "John Doe <[email protected]>" style "pretty" email addresses are processed.
        * Spaces are striped from the beginning and end of addresses, but no error is raised.
    """
    if email_validator is None:
        import_email_validator()

    m = pretty_email_regex.fullmatch(value)
    name: str | None = None
    if m:
        unquoted_name, quoted_name, value = m.groups()
        name = unquoted_name or quoted_name

    email = value.strip()

    try:
        parts = email_validator.validate_email(email, check_deliverability=False)
    except email_validator.EmailNotValidError as e:
        raise PydanticCustomError(
            'value_error', 'value is not a valid email address: {reason}', {'reason': str(e.args[0])}
        ) from e

    email = parts.normalized
    assert email is not None
    name = name or parts.local_part
    return name, email