Skip to content

Should env.url return a string instead of a urllib.parse.ParseResult? #402

@sloria

Description

@sloria

the current typing assumes that default is the same type as the "internal type" returned by the parser method. for url, that's a urllib.parse.ParseResult.

therefore, to pass type-checking, you'd need to do:

from typing import reveal_type
from urllib.parse import urlparse

from environs import env

MY_API_URL = env.url(
    "MY_API_URL",
    default=urlparse("http://api.example.com"),
)

print(reveal_type(MY_API_URL))
# Runtime type is 'ParseResult'

If you want a string, you can use env.str with the URL validator.

from typing import reveal_type

from environs import env, validate

MY_API_URL = env.str(
    "MY_API_URL",
    default="http://api.example.com",
    validate=validate.URL(),
)

print(reveal_type(MY_API_URL)) 
# Runtime type is 'str'

i've updated the docs to clarify this.

so while the current behavior is expected, perhaps it isn't desired. should env.url just return a string and do validation? i'm not sure how many users want a ParseResult 🤔 . i'll open a new issue for this to gather feedback.

Originally posted by @sloria in #400

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions