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

Add type hints #546

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
33 changes: 17 additions & 16 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import re
import sys
import warnings
from typing import Union, TypeVar, List, Tuple, Dict
from urllib.parse import (
parse_qs,
ParseResult,
Expand Down Expand Up @@ -214,7 +215,7 @@ def __call__(self, var, cast=None, default=NOTSET, parse_default=False):
def __contains__(self, var):
return var in self.ENVIRON

def str(self, var, default=NOTSET, multiline=False):
def str(self, var, default: Union[str, NoValue] = NOTSET, multiline=False) -> str:
"""
:rtype: str
"""
Expand All @@ -223,7 +224,7 @@ def str(self, var, default=NOTSET, multiline=False):
return re.sub(r'(\\r)?\\n', r'\n', value)
return value

def bytes(self, var, default=NOTSET, encoding='utf8'):
def bytes(self, var, default: Union[bytes, NoValue] = NOTSET, encoding='utf8') -> bytes:
"""
:rtype: bytes
"""
Expand All @@ -232,19 +233,19 @@ def bytes(self, var, default=NOTSET, encoding='utf8'):
return value.encode(encoding)
return value

def bool(self, var, default=NOTSET):
def bool(self, var, default: Union[bool, NoValue] = NOTSET) -> bool:
"""
:rtype: bool
"""
return self.get_value(var, cast=bool, default=default)

def int(self, var, default=NOTSET):
def int(self, var, default: Union[int, NoValue] = NOTSET) -> int:
"""
:rtype: int
"""
return self.get_value(var, cast=int, default=default)

def float(self, var, default=NOTSET):
def float(self, var, default: Union[float, NoValue] = NOTSET) -> float:
"""
:rtype: float
"""
Expand All @@ -256,7 +257,7 @@ def json(self, var, default=NOTSET):
"""
return self.get_value(var, cast=json.loads, default=default)

def list(self, var, cast=None, default=NOTSET):
def list(self, var, cast=None, default=NOTSET) -> List:
"""
:rtype: list
"""
Expand All @@ -266,7 +267,7 @@ def list(self, var, cast=None, default=NOTSET):
default=default
)

def tuple(self, var, cast=None, default=NOTSET):
def tuple(self, var, cast=None, default=NOTSET) -> Tuple:
"""
:rtype: tuple
"""
Expand All @@ -276,13 +277,13 @@ def tuple(self, var, cast=None, default=NOTSET):
default=default
)

def dict(self, var, cast=dict, default=NOTSET):
def dict(self, var, cast=dict, default=NOTSET) -> Dict:
"""
:rtype: dict
"""
return self.get_value(var, cast=cast, default=default)

def url(self, var, default=NOTSET):
def url(self, var, default=NOTSET) -> ParseResult:
"""
:rtype: urllib.parse.ParseResult
"""
Expand All @@ -293,7 +294,7 @@ def url(self, var, default=NOTSET):
parse_default=True
)

def db_url(self, var=DEFAULT_DATABASE_ENV, default=NOTSET, engine=None):
def db_url(self, var=DEFAULT_DATABASE_ENV, default=NOTSET, engine=None) -> Dict:
"""Returns a config dictionary, defaulting to DATABASE_URL.

The db method is an alias for db_url.
Expand All @@ -307,7 +308,7 @@ def db_url(self, var=DEFAULT_DATABASE_ENV, default=NOTSET, engine=None):

db = db_url

def cache_url(self, var=DEFAULT_CACHE_ENV, default=NOTSET, backend=None):
def cache_url(self, var=DEFAULT_CACHE_ENV, default=NOTSET, backend=None) -> Dict:
"""Returns a config dictionary, defaulting to CACHE_URL.

The cache method is an alias for cache_url.
Expand All @@ -321,7 +322,7 @@ def cache_url(self, var=DEFAULT_CACHE_ENV, default=NOTSET, backend=None):

cache = cache_url

def email_url(self, var=DEFAULT_EMAIL_ENV, default=NOTSET, backend=None):
def email_url(self, var=DEFAULT_EMAIL_ENV, default=NOTSET, backend=None) -> Dict:
"""Returns a config dictionary, defaulting to EMAIL_URL.

The email method is an alias for email_url.
Expand All @@ -335,7 +336,7 @@ def email_url(self, var=DEFAULT_EMAIL_ENV, default=NOTSET, backend=None):

email = email_url

def search_url(self, var=DEFAULT_SEARCH_ENV, default=NOTSET, engine=None):
def search_url(self, var=DEFAULT_SEARCH_ENV, default: Union[Dict, NoValue] = NOTSET, engine=None) -> Dict:
"""Returns a config dictionary, defaulting to SEARCH_URL.

:rtype: dict
Expand All @@ -345,8 +346,8 @@ def search_url(self, var=DEFAULT_SEARCH_ENV, default=NOTSET, engine=None):
engine=engine
)

def channels_url(self, var=DEFAULT_CHANNELS_ENV, default=NOTSET,
backend=None):
def channels_url(self, var=DEFAULT_CHANNELS_ENV, default: Union[Dict, NoValue] = NOTSET,
backend=None) -> Dict:
"""Returns a config dictionary, defaulting to CHANNELS_URL.

:rtype: dict
Expand All @@ -358,7 +359,7 @@ def channels_url(self, var=DEFAULT_CHANNELS_ENV, default=NOTSET,

channels = channels_url

def path(self, var, default=NOTSET, **kwargs):
def path(self, var, default: Union['Path', NoValue] = NOTSET, **kwargs) -> 'Path':
"""
:rtype: Path
"""
Expand Down
Loading