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

url() does not return a ParseResult when using empty string default value #404

Closed
jnm opened this issue Jun 29, 2022 · 1 comment
Closed
Assignees
Labels
bug Something isn't working

Comments

@jnm
Copy link
Contributor

jnm commented Jun 29, 2022

https://django-environ.readthedocs.io/en/latest/api.html#environ.Env.url indicates that url() returns a urllib.parse.ParseResult, but sometimes it can return a str:

Python 3.10.5 (main, Jun 11 2022, 16:53:24) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import environ
>>> e = environ.Env()
>>> e.url('UNSET_VAR', '')
''

Contrast with the behavior for an empty (but set) environment variable:

>>> e.url('EMPTY_VAR')
ParseResult(scheme='', netloc='', path='', params='', query='', fragment='')

Usually, I want to call geturl() immediately on the result of url(), and it would be convenient if this worked consistently instead of sometimes raising AttributeError: 'str' object has no attribute 'geturl'.

url() does use parse_default=True:

def url(self, var, default=NOTSET):
"""
:rtype: urllib.parse.ParseResult
"""
return self.get_value(
var,
cast=urlparse,
default=default,
parse_default=True
)

However, when default coerces to False, it is not parsed:

if value != default or (parse_default and value):
value = self.parse_value(value, cast)

I see that the and value logic was added as a fix for #44, but I wonder if we could check for and value is not None instead. I'll try this and see if all tests continue to pass.

@sergeyklay
Copy link
Collaborator

Fixed in #405. Thank you for the report!

foarsitter pushed a commit to foarsitter/django-environ that referenced this issue Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants