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 multiple hosts without TLD #4541

Closed
6 of 15 tasks
levsh opened this issue Sep 20, 2022 · 2 comments
Closed
6 of 15 tasks

PostgresDsn multiple hosts without TLD #4541

levsh opened this issue Sep 20, 2022 · 2 comments
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@levsh
Copy link

levsh commented Sep 20, 2022

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

Hi!
I have two questions:

  1. From docs "PostgresDsn: user info required, TLD not required, host required, as of V.10 PostgresDsn supports multiple hosts". User info required but postgresql docs has example "postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp"
  2. It seems PostgresDsn doesn't work properly with multiple hosts without TLD

Thank you

Example Code

In [1]: import pydantic
   ...: from pydantic import BaseModel, PostgresDsn
   ...: 
   ...: class M(BaseModel):
   ...:     url: PostgresDsn
   ...: 

# Not ok
In [2]: M(url="postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp")
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In [2], line 1
----> 1 M(url="postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp")

File ~/.local/share/virtualenvs/.../lib/python3.8/site-packages/pydantic/main.py:342, in pydantic.main.BaseModel.__init__()

ValidationError: 1 validation error for M
url
  userinfo required in URL but missing (type=value_error.url.userinfo)

# Not ok
In [3]: M(url="postgresql://u:p@host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp")
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In [3], line 1
----> 1 M(url="postgresql://u:p@host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp")

File ~/.local/share/virtualenvs/.../lib/python3.8/site-packages/pydantic/main.py:342, in pydantic.main.BaseModel.__init__()

ValidationError: 1 validation error for M
url
  build() needs keyword-only argument host (type=type_error)

# Add .com
# Ok
In [4]: M(url="postgresql://u:p@host1.com:123,host2.com:456/somedb?target_session_attrs=any&application_name=myapp")
Out[4]: M(url=PostgresDsn('postgresql://u:p@host1.com:123,host2.com:456/somedb?target_session_attrs=any&application_name=myapp', ))

Python, Pydantic & OS Version

pydantic version: 1.10.2
            pydantic compiled: True
                 install path: /home/vagrant/.local/share/virtualenvs/.../lib/python3.8/site-packages/pydantic
               python version: 3.8.6 (default, Dec 22 2020, 17:04:29)  [GCC 7.5.0]
                     platform: Linux-4.15.0-192-generic-x86_64-with-glibc2.27
     optional deps. installed: ['dotenv', 'email-validator', 'typing-extensions']

Affected Components

@levsh levsh added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Sep 20, 2022
@levsh levsh changed the title PostgresDsn multiple hosts without TDL PostgresDsn multiple hosts without TLD Sep 20, 2022
@samuelcolvin
Copy link
Member

There are a couple of problems here.

  1. PostgresDsn(...) has some slightly weird behaviour and isn't expected to work like this. I agree that this is very confusing, it should either work properly, or fail completely.
  2. There are a whole host of problems with URL types see Revert percent encoding in URLs. #4470 for some explanation, basically we run some different logic only if there's a TLD, the problem as per Revert percent encoding in URLs. #4470 is that if we run that logic on all URLs, a whole bunch of tests fail.

The medium term solutions is to move to a dedicated URL validation library in V2, there is'nt really a good short term solution I'm afraid, you might be best to implement your own validation.

@samuelcolvin samuelcolvin removed the unconfirmed Bug not yet confirmed as valid/applicable label Sep 20, 2022
@Kludex
Copy link
Member

Kludex commented Apr 29, 2023

This is already working on V2. 🙏

@Kludex Kludex closed this as completed Apr 29, 2023
@Kludex Kludex removed their assignment Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

3 participants