Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgresDsn does not accept correct postgresql:///dbname since 2.10, was accepted before #11023

Closed
1 task done
Tracked by #10910
wereii opened this issue Dec 2, 2024 · 2 comments
Closed
1 task done
Tracked by #10910
Assignees
Labels

Comments

@wereii
Copy link

wereii commented Dec 2, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Before 2.10, PostgresDsn would accept postgresql:///dbname which is a valid DSN for postgres where the missing host means use local unix socket connection.
Now I get Input should be a valid URL, empty host

Example Code

import pydantic

class M(pydantic.BaseModel):
  dsn: pydantic.PostgresDsn


M(dsn="postgresql:///dbname")
print(M)
pydantic_core._pydantic_core.ValidationError: 1 validation error for M
dsn
  Input should be a valid URL, empty host [type=url_parsing, input_value='postgresql:///dbname', input_type=str]
    For further information visit https://errors.pydantic.dev/2.10/v/url_parsing

Python, Pydantic & OS Version

pydantic version: 2.10.2
        pydantic-core version: 2.27.1
          pydantic-core build: profile=release pgo=false
                 install path: /home/<user>/Projects/<snip>/<project_dir>/.venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.7 (main, Oct  1 2024, 11:15:50) [GCC 14.2.1 20240910]
                     platform: Linux-6.12.1-arch1-1-x86_64-with-glibc2.40
             related packages: typing_extensions-4.12.2 mypy-1.13.0 pydantic-settings-2.6.1 fastapi-0.115.5
                       commit: unknown
@wereii wereii added bug V2 Bug related to Pydantic V2 pending Is unconfirmed labels Dec 2, 2024
@sydney-runkle
Copy link
Member

Hmm, this is a consequence of pydantic/pydantic-core#1488.

We're now properly enforcing the host_required for PostgresDsn...

@sydney-runkle
Copy link
Member

If you'd like to support a version without a required host, I'd recommend:

from pydantic import BaseModel, PostgresDsn, UrlConstraints
from typing import Annotated

class M(BaseModel):
  dsn: Annotated[PostgresDsn, UrlConstraints(host_required=False)]


m = M(dsn="postgresql:///dbname")
print(repr(m.dsn))
#> PostgresDsn('postgresql:///dbname')

@sydney-runkle sydney-runkle added question and removed bug V2 Bug related to Pydantic V2 pending Is unconfirmed labels Dec 2, 2024
@sydney-runkle sydney-runkle self-assigned this Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants