From ab217163c7d6378e20323306082cccaa60dd2744 Mon Sep 17 00:00:00 2001 From: Nick Cannariato <434063+nickcannariato@users.noreply.github.com> Date: Thu, 19 Mar 2020 02:03:25 -0500 Subject: [PATCH] Stop raising UserWarning if .env file isn't found Currently, if a `.env` file isn't found by the read_env() classmethod, it raises a UserWarning. Based on Python's logging guide, warnings.warn() is only appropriate to be raised in library code if the issue is avoidable and the client application should be modified to eliminate the warning. Conversely, it recommends logging.warning() if there is nothing the client application can do about the situation, but the event should still be noted (see ref1). In this particular case, the function is working with `.env` files that should only ever exist in development environments, which means that the function will erroneously warn the consumer in production and staging environments. With this in mind, I believe the most sensible thing to do would be to convert read_env from using the warnings module to using the logging module. Additionally, since the lack of a `.env` file is going to occur regularly during normal operation of the program, I also believe that this should be downgraded to an INFO level message from a WARNING level message. joke2k/django-environ#243 requests that the .env file be made optional, and as far as I can the .env file is indeed optional, it's just that a UserWarning is being raised instead of a logged message. With that in mind, I'm referencing it as being closed by this change. ref1: https://docs.python.org/3/howto/logging.html#when-to-use-logging fixes: joke2k/django-environ#243 --- environ/environ.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/environ/environ.py b/environ/environ.py index b91d3ff8..0b97c5e9 100644 --- a/environ/environ.py +++ b/environ/environ.py @@ -749,7 +749,7 @@ def read_env(cls, env_file=None, **overrides): '.env' ) if not os.path.exists(env_file): - warnings.warn( + logger.info( "%s doesn't exist - if you're not configuring your " "environment separately, create one." % env_file) return @@ -762,8 +762,8 @@ def read_env(cls, env_file=None, **overrides): with env_file as f: content = f.read() except OSError: - warnings.warn( - "Error reading %s - if you're not configuring your " + logger.info( + "%s not found - if you're not configuring your " "environment separately, check this." % env_file) return