Pydantic URL types that are based on the str class.
Reason this release was yanked:
wrong dependency specification (pydantic version range)
Project description
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 usestr(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:
- AnyUrl adds trailing slash
- Work around for pydantic_core._pydantic_core.Url in V2 where string is expected
- How can I integrate pydantic v2 URLs in code?
- Url and Dsn types in pydantic.networks no longer inherit from str
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydantic_string_url-1.0.1.tar.gz
.
File metadata
- Download URL: pydantic_string_url-1.0.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.9 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b92585b41b28b5dc2e018a6af6398fdf287167e0fea3ec9f536c402811a9ff2 |
|
MD5 | b7e6d72090829ca555cb07b5740d76d5 |
|
BLAKE2b-256 | 8a6c279a773fcb787e57687e2d501ce382eb90085dd0e572fadfffe9dbffb5e0 |
File details
Details for the file pydantic_string_url-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: pydantic_string_url-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.9 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2ef2352a3d3a620ffcbb2fc324b67ad81839b8a4f03bf17d4e587746537b06a |
|
MD5 | 0761f0f111add3b28bc9f1a4cd36695a |
|
BLAKE2b-256 | d87aa26ed2aaace7ca1b04124a0c440814be81f6f8871873eb569a3ed03544d8 |