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:
AnyUrlAnyHttpUrlHttpUrlAnyWebsocketUrlWebsocketUrlFileUrlFtpUrl
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
Metadata
Release files for pydantic-string-url 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_string_url-1.0.2.tar.gz | 4.0 kB | Without provenance | Details for pydantic_string_url-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_string_url-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for pydantic_string_url-1.0.2-py3-none-any.whl |
Total release size: 8.4 kB
Release files / pydantic_string_url-1.0.2.tar.gz
| Download URL | pydantic_string_url-1.0.2.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa7690f7231b47ac89e2f0546ef1137af182bcaa9c93f9a3c0deaff2afbb79ca
|
|
BLAKE2b-256 checksum How to use checksums |
01b730548d006fb1d57a6b7ec69c4852742669cab49a52daaf8c29dcb210de6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.11.9 Darwin/23.6.0
|
Release files / pydantic_string_url-1.0.2-py3-none-any.whl
| Download URL | pydantic_string_url-1.0.2-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9f97d415aa99f392d08882f2702f7eda9b8f979bb2526dbf75df9bc9ff062f21
|
|
BLAKE2b-256 checksum How to use checksums |
218113a0edf1eb6f4a704fc4f7f2047d5e721c828e381cdb2c5dc7587b90f559
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.11.9 Darwin/23.6.0
|
Security for pydantic-string-url 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.