Repository: joke2k/django-environ

Issue:
Casting to bytes
Add support to cast env vars to bytes.

Discussion:

Patch:
diff --git a/environ/environ.py b/environ/environ.py
index 1365a29..91e93af 100644
--- a/environ/environ.py
+++ b/environ/environ.py
@@ -146,6 +146,12 @@ class Env(object):
         """
         return self.get_value(var, cast=str, default=default)
 
+    def bytes(self, var, default=NOTSET, encoding='utf8'):
+        """
+        :rtype: bytes
+        """
+        return self.get_value(var, cast=str).encode(encoding)
+
     def bool(self, var, default=NOTSET):
         """
         :rtype: bool


Test patch:
diff --git a/environ/test.py b/environ/test.py
index 78a373f..02eca44 100644
--- a/environ/test.py
+++ b/environ/test.py
@@ -88,6 +88,9 @@ class EnvTests(BaseTests):
         self.assertTypeAndValue(str, 'bar', self.env('STR_VAR'))
         self.assertTypeAndValue(str, 'bar', self.env.str('STR_VAR'))
 
+    def test_bytes(self):
+        self.assertTypeAndValue(bytes, b'bar', self.env.bytes('STR_VAR'))
+
     def test_int(self):
         self.assertTypeAndValue(int, 42, self.env('INT_VAR', cast=int))
         self.assertTypeAndValue(int, 42, self.env.int('INT_VAR'))