Fix type hinting for load_env (#2107)
* Deprecate `load_env` in favor of `env_prefix` `load_env` both enabled/disabled environment variable parsing, while also letting the user modify the env prefix. Deprecate the ability to disable environment parsing, and add a new config variable to track the user's desired prefix for environment-based configuration. Resolves: #2102 * Add a few common .gitignore patterns
This commit is contained in:
committed by
GitHub
parent
30479765cb
commit
e21521f45c
@@ -43,7 +43,7 @@ from sanic.asgi import ASGIApp
|
||||
from sanic.base import BaseSanic
|
||||
from sanic.blueprint_group import BlueprintGroup
|
||||
from sanic.blueprints import Blueprint
|
||||
from sanic.config import BASE_LOGO, Config
|
||||
from sanic.config import BASE_LOGO, SANIC_PREFIX, Config
|
||||
from sanic.exceptions import (
|
||||
InvalidUsage,
|
||||
SanicException,
|
||||
@@ -125,7 +125,8 @@ class Sanic(BaseSanic):
|
||||
router: Optional[Router] = None,
|
||||
signal_router: Optional[SignalRouter] = None,
|
||||
error_handler: Optional[ErrorHandler] = None,
|
||||
load_env: bool = True,
|
||||
load_env: Union[bool, str] = True,
|
||||
env_prefix: Optional[str] = SANIC_PREFIX,
|
||||
request_class: Optional[Type[Request]] = None,
|
||||
strict_slashes: bool = False,
|
||||
log_config: Optional[Dict[str, Any]] = None,
|
||||
@@ -150,7 +151,7 @@ class Sanic(BaseSanic):
|
||||
self._test_manager = None
|
||||
self.asgi = False
|
||||
self.blueprints: Dict[str, Blueprint] = {}
|
||||
self.config = Config(load_env=load_env)
|
||||
self.config = Config(load_env=load_env, env_prefix=env_prefix)
|
||||
self.configure_logging = configure_logging
|
||||
self.ctx = SimpleNamespace()
|
||||
self.debug = None
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from inspect import isclass
|
||||
from os import environ
|
||||
from pathlib import Path
|
||||
from typing import Any, Union
|
||||
from typing import Any, Dict, Optional, Union
|
||||
from warnings import warn
|
||||
|
||||
from .utils import load_module_from_file_location, str_to_bool
|
||||
|
||||
@@ -41,7 +42,13 @@ DEFAULT_CONFIG = {
|
||||
|
||||
|
||||
class Config(dict):
|
||||
def __init__(self, defaults=None, load_env=True, keep_alive=None):
|
||||
def __init__(
|
||||
self,
|
||||
defaults: Dict[str, Union[str, bool, int, float, None]] = None,
|
||||
load_env: Optional[Union[bool, str]] = True,
|
||||
env_prefix: Optional[str] = SANIC_PREFIX,
|
||||
keep_alive: Optional[int] = None,
|
||||
):
|
||||
defaults = defaults or {}
|
||||
super().__init__({**DEFAULT_CONFIG, **defaults})
|
||||
|
||||
@@ -50,9 +57,20 @@ class Config(dict):
|
||||
if keep_alive is not None:
|
||||
self.KEEP_ALIVE = keep_alive
|
||||
|
||||
if load_env:
|
||||
prefix = SANIC_PREFIX if load_env is True else load_env
|
||||
self.load_environment_vars(prefix=prefix)
|
||||
if env_prefix != SANIC_PREFIX:
|
||||
if env_prefix:
|
||||
self.load_environment_vars(env_prefix)
|
||||
elif load_env is not True:
|
||||
if load_env:
|
||||
self.load_environment_vars(prefix=load_env)
|
||||
warn(
|
||||
"Use of load_env is deprecated and will be removed in "
|
||||
"21.12. Modify the configuration prefix by passing "
|
||||
"env_prefix instead.",
|
||||
DeprecationWarning,
|
||||
)
|
||||
else:
|
||||
self.load_environment_vars(SANIC_PREFIX)
|
||||
|
||||
def __getattr__(self, attr):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user