From c1064673aaa83d52c8542e41941b7ed66bad63ca Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 14 Dec 2020 15:15:08 +0100 Subject: [PATCH] Isolate configuration related code better - create the global `config` object inside the `urban_meal_delivery.configuration` module - streamline documentation and comments --- setup.cfg | 2 +- src/urban_meal_delivery/__init__.py | 16 +++------------- src/urban_meal_delivery/configuration.py | 8 ++++++-- tests/conftest.py | 2 ++ 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/setup.cfg b/setup.cfg index f764328..2a467c6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -272,4 +272,4 @@ console_output_style = count env = TESTING=true markers = - e2e: integration tests, inlc., for example, tests touching a database + e2e: integration tests, incl., for example, tests touching the database diff --git a/src/urban_meal_delivery/__init__.py b/src/urban_meal_delivery/__init__.py index 943ba9b..ad34978 100644 --- a/src/urban_meal_delivery/__init__.py +++ b/src/urban_meal_delivery/__init__.py @@ -5,11 +5,12 @@ Example: >>> umd.__version__ != '0.0.0' True """ +# The config object must come before all other project-internal imports. +from urban_meal_delivery.configuration import config # noqa:F401 isort:skip -import os as _os from importlib import metadata as _metadata -from urban_meal_delivery import configuration as _configuration +from urban_meal_delivery import db # noqa:F401 try: @@ -24,14 +25,3 @@ else: __author__ = _pkg_info['author'] __pkg_name__ = _pkg_info['name'] __version__ = _pkg_info['version'] - - -# Global `config` object to be used in the package. -config: _configuration.Config = _configuration.make_config( - 'testing' if _os.getenv('TESTING') else 'production', -) - - -# Import `db` down here as it depends on `config`. -# pylint:disable=wrong-import-position -from urban_meal_delivery import db # noqa:E402,F401 isort:skip diff --git a/src/urban_meal_delivery/configuration.py b/src/urban_meal_delivery/configuration.py index c4cc451..72c10d3 100644 --- a/src/urban_meal_delivery/configuration.py +++ b/src/urban_meal_delivery/configuration.py @@ -73,7 +73,7 @@ def make_config(env: str = 'production') -> Config: """Create a new `Config` object. Args: - env: either 'production' or 'testing'; defaults to the first + env: either 'production' or 'testing' Returns: config: a namespace with all configurations @@ -81,7 +81,8 @@ def make_config(env: str = 'production') -> Config: Raises: ValueError: if `env` is not as specified """ # noqa:DAR203 - config: Config + config: Config # otherwise mypy is confused + if env.strip().lower() == 'production': config = ProductionConfig() elif env.strip().lower() == 'testing': @@ -94,3 +95,6 @@ def make_config(env: str = 'production') -> Config: warnings.warn('Bad configurartion: no DATABASE_URI set in the environment') return config + + +config = make_config('testing' if os.getenv('TESTING') else 'production') diff --git a/tests/conftest.py b/tests/conftest.py index 1b91688..b58c430 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,6 +5,8 @@ import os from urban_meal_delivery import config +# The TESTING environment variable is set +# in setup.cfg in pytest's config section. if not os.getenv('TESTING'): raise RuntimeError('Tests must be executed with TESTING set in the environment')