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

pydantic initial integration #3143

Closed
wants to merge 1 commit into from

Conversation

07pepa
Copy link

@07pepa 07pepa commented Mar 14, 2024

Summary

i wanted to use URL to be usable in pydantic setting directly see pydantic/pydantic-extra-types#156 this is mainly to entice discussion and is in no way shape or form final (if this may be accepted)

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • [-] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [-] I've updated the documentation accordingly.

@tomchristie
Copy link
Member

Can you describe from a docs pov what you're trying to do?

@07pepa
Copy link
Author

07pepa commented Mar 14, 2024

I am tryiing to acheave compatibility so url can be in setting class (pulled from .env files or enviroment or other sources)

from pydantic_settings import BaseSettings
from httpx import URL


class Settings(BaseSettings):
    url: URL 



CONFIG = Settings()
AsyncClient = AsyncClient(base_url=CONFIG.url)

alternative is to support AnyUrl from pydantic

@tomchristie
Copy link
Member

Okay, can you provide an example of what the above does and then show why httpx would need to be modified in order to accomodate it?

@07pepa
Copy link
Author

07pepa commented Mar 14, 2024

Okay, can you provide an example of what the above does and then show why httpx would need to be modified in order to accomodate it?

Trick is it does not need to modified... it just would be nice to do it Here (and can be done other way around (extending URL from HTTPX in pydantic) it is mostly for better convinience

demo without change

import os

from pydantic_settings import BaseSettings
from pydantic import AnyUrl
from httpx import AsyncClient


class Settings(BaseSettings):
    url: AnyUrl


# assume this is in .env file
os.environ["url"] = "http://localhost"

CONFIG = Settings()

client = AsyncClient(base_url=str(CONFIG.url))

here is demo with change

import os

from pydantic_settings import BaseSettings
from httpx import URL, AsyncClient


class Settings(BaseSettings):
    url: URL


# assume this is in .env file
os.environ["url"] = "http://localhost"

CONFIG = Settings()

client = AsyncClient(base_url=CONFIG.url)

@07pepa 07pepa deleted the pydantic-initial-integration branch March 16, 2024 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants