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

feat: A way to warn user on default variable usage #454

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class Env:
"simple": "haystack.backends.simple_backend.SimpleEngine",
}
CLOUDSQL = 'cloudsql'
_WARN_ON_DEFAULT_VALUE_USAGE = False

def __init__(self, **scheme):
self.smart_cast = True
Expand All @@ -213,6 +214,8 @@ def str(self, var, default=NOTSET, multiline=False):
if multiline:
return re.sub(r'(\\r)?\\n', r'\n', value)
return value
def warn_on_default_value_usage(self, enabled: bool = True):
self._WARN_ON_DEFAULT_VALUE_USAGE = enabled

def bytes(self, var, default=NOTSET, encoding='utf8'):
"""
Expand Down Expand Up @@ -391,6 +394,8 @@ def get_value(self, var, cast=None, default=NOTSET, parse_default=False):
raise ImproperlyConfigured(error_msg) from exc

value = default
if self._WARN_ON_DEFAULT_VALUE_USAGE:
logger.warn(f"Variable '{var}' is using a default variable.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a tutorial-style HOWTO taking you through the steps in using the logging module. https://docs.python.org/3/howto/logging.html

warnings.warn() in library code if the issue is avoidable and the client application should be modified to eliminate the warning

logging.warning() if there is nothing the client application can do about the situation, but the event should still be noted

Considering the above, do you still think that the logging is the better solution here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it again, I have to agree with you. the pythonic warning package is a better substitute for this case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please made the necessary changes?


# Resolve any proxied values
prefix = b'$' if isinstance(value, bytes) else '$'
Expand Down