Skip to main content

Pydantic URL types that are based on the str class.

Reason this release was yanked:

wrong dependency specification (pydantic version range)

Project description

Tests Linting

pydantic-string-url

Pydantic URL types that are based on the str class.

Introduction

Since Pydantic v2 the types realated to URLs are not based on the standard Python str class any more.

This decision comes with some issues:

  • URLs cannot be directly passed to library packages that expect str, you must use str(url) instead
  • some internal normalization is done, so the input strings are not always preserved when a URL object is created (trailing slashes could be added), which might be problematic for some applications

This package provides direct replacements for the Pydantic types:

  • AnyUrl
  • AnyHttpUrl
  • HttpUrl
  • AnyWebsocketUrl
  • WebsocketUrl
  • FileUrl
  • FtpUrl

Those replacement types are based on strings, so they are also a str, preserve the original input string and use the same validation functions as their Pydantic counterparts. You can still use the replacement type's .url property to access the original Pydantic URL type.

See also the discussions here:

Usage

The package pydantic-string-url is available on PyPi, so it can be installed with Python package managers such as pip or poetry.

Usage example:

"""Example."""

from pydantic import BaseModel, TypeAdapter, ValidationError

from pydantic_string_url import HttpUrl


# Use inside BaseModel
class User(BaseModel):
    """A user."""

    name: str
    age: int
    homepage: HttpUrl


user = {"name": "John Doe", "age": 33, "homepage": "https://example.com"}

invalid_user = {"name": "Alice", "age": 32, "homepage": "not a url"}

john = User.model_validate(user)
assert john.homepage == "https://example.com"  # no trailing slash was added
try:
    alice = User.model_validate(invalid_user)
except ValidationError as e:
    print(str(e))

# Use standalone
urls = ["https://test.com", "some wrong url"]

url_a = TypeAdapter(HttpUrl).validate_python(urls[0])
try:
    url_b = TypeAdapter(HttpUrl).validate_python(urls[1])
except ValidationError as e:
    print(str(e))

# You can still access the Pydantic type by using the string's .url property
assert url_a.url.scheme == "https"
assert john.homepage.url.scheme == "https"

Licence

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_string_url-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

pydantic_string_url-1.0.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_string_url-1.0.0.tar.gz.

File metadata

  • Download URL: pydantic_string_url-1.0.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.9 Darwin/23.6.0

File hashes

Hashes for pydantic_string_url-1.0.0.tar.gz
Algorithm Hash digest
SHA256 da1b18e0acec0c81800ab9522543231330af5efbfdbda12ed8a2ce4e8277e7d9
MD5 bcfe46c168483ad4e425374eb69e6c9d
BLAKE2b-256 7aacada8efb2ae51ecbcdfbc2c51920435751e03554b971f43cfbe66603d6b0b

See more details on using hashes here.

File details

Details for the file pydantic_string_url-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_string_url-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb0833d13d2c6e107e929a12fbca88bd983d121474ec0457c498f24ac75d9376
MD5 3955b9dfefcbb467f58b1da9372fdf1d
BLAKE2b-256 3e4e4791307a2424fc573a6d13e56a44856331157fca736eb804c2ee38c71e26

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page