From 6821fbc2feaad310f5b89c2eb95175a4e9dce6a9 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sat, 28 Oct 2023 20:21:09 +0100 Subject: [PATCH] core/config: implement a warning if config is imported from the dir other than MY_CONFIG this should help with identifying setup issues --- my/core/init.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/my/core/init.py b/my/core/init.py index 6bf766e..bec3a9a 100644 --- a/my/core/init.py +++ b/my/core/init.py @@ -14,6 +14,7 @@ Please let me know if you are aware of a better way of dealing with this! # separate function to present namespace pollution def setup_config() -> None: + from pathlib import Path import sys import warnings @@ -47,6 +48,23 @@ See https://github.com/karlicoss/HPI/blob/master/doc/SETUP.org#setting-up-the-mo Importing 'my.config' failed! (error: {ex}). This is likely to result in issues. See https://github.com/karlicoss/HPI/blob/master/doc/SETUP.org#setting-up-the-modules for more info. """) + else: + # defensive just in case -- __file__ may not be present if there is some dynamic magic involved + used_config_file = getattr(my.config, '__file__', None) + if used_config_file is not None: + used_config_path = Path(used_config_file) + try: + # will crash if it's imported from other dir? + used_config_path.relative_to(mycfg_dir) + except ValueError: + # TODO maybe implement a strict mode where these warnings will be errors? + warnings.warn( + f""" +Expected my.config to be located at {mycfg_dir}, but instead its path is {used_config_path}. +This will likely cause issues down the line -- double check {mycfg_dir} structure. +See https://github.com/karlicoss/HPI/blob/master/doc/SETUP.org#setting-up-the-modules for more info. +""", + ) setup_config()