Repository: Zapix/mtpylon (language: python)

Issue:
Split context vars to avoid circular dependency issue
There is circular dependency issue coz mtpylon stores on contextvars in same module:
```
tests/schema/functions/test_login.py:None (tests/schema/functions/test_login.py)
ImportError while importing test module '/Users/aleksandraibulatov/projects/mtpylon-samples/todo-list/backend/tests/schema/functions/test_login.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../venv/lib/python3.9/site-packages/_pytest/python.py:578: in _importtestmodule
    mod = import_path(self.fspath, mode=importmode)
../venv/lib/python3.9/site-packages/_pytest/pathlib.py:524: in import_path
    importlib.import_module(module_name)
../../../../../.pyenv/versions/3.9.0/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1030: in _gcd_import
    ???
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
../venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:170: in exec_module
    exec(co, module.__dict__)
schema/functions/test_login.py:8: in <module>
    from schema.functions.login_func import login
../schema/__init__.py:6: in <module>
    from .functions import register, login
../schema/functions/__init__.py:3: in <module>
    from .login_func import login
../schema/functions/login_func.py:4: in <module>
    from mtpylon.contextvars import auth_key_var
../venv/lib/python3.9/site-packages/mtpylon/contextvars.py:9: in <module>
    from .income_message import IncomeMessage
../venv/lib/python3.9/site-packages/mtpylon/income_message.py:4: in <module>
    from .messages.types import MtprotoMessage
../venv/lib/python3.9/site-packages/mtpylon/messages/__init__.py:7: in <module>
    from .mtproto_message import (
../venv/lib/python3.9/site-packages/mtpylon/messages/mtproto_message.py:9: in <module>
    from .unencrypted_message import (
../venv/lib/python3.9/site-packages/mtpylon/messages/unencrypted_message.py:5: in <module>
    from mtpylon.service_schema import load as load_schema, dump as dump_schema
../venv/lib/python3.9/site-packages/mtpylon/service_schema/__init__.py:2: in <module>
    from .service_schema import service_schema
../venv/lib/python3.9/site-packages/mtpylon/service_schema/service_schema.py:31: in <module>
    from .functions import (
../venv/lib/python3.9/site-packages/mtpylon/service_schema/functions/__init__.py:2: in <module>
    from .req_pq_func import req_pq
../venv/lib/python3.9/site-packages/mtpylon/service_schema/functions/req_pq_func.py:9: in <module>
    from mtpylon.contextvars import server_nonce_var
E   ImportError: cannot import name 'server_nonce_var' from partially initialized module 'mtpylon.contextvars' (most likely due to a circular import) (/Users/aleksandraibulatov/projects/mtpylon-samples/todo-list/backend/venv/lib/python3.9/site-packages/mtpylon/contextvars.py)
```



Discussion / hints:

Fix patch:
diff --git a/mtpylon/contextvars.py b/mtpylon/contextvars.py
index 90b90f1..cf1c7ff 100644
--- a/mtpylon/contextvars.py
+++ b/mtpylon/contextvars.py
@@ -2,11 +2,16 @@
 """
 Declared contextvars that will be used in mtpylon
 """
+from typing import TYPE_CHECKING
 from contextvars import ContextVar
 
 from .types import int128, int256, long
 from .crypto import AuthKey
-from .income_message import IncomeMessage
+
+
+if TYPE_CHECKING:  # pragma: nocover
+    from .income_message import IncomeMessage
+
 
 """
 Stores server_nonces for authorization key creation process
@@ -47,4 +52,6 @@
 """
 Stores message that has been received
 """
-income_message_var: ContextVar[IncomeMessage] = ContextVar('income_message')
+income_message_var: ContextVar[
+    'IncomeMessage'
+] = ContextVar('income_message')


Test patch:
diff --git a/tests/test_import_contextvars.py b/tests/test_import_contextvars.py
new file mode 100644
index 0000000..f79e137
--- /dev/null
+++ b/tests/test_import_contextvars.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+from mtpylon.contextvars import auth_key_var
+
+
+def test_import_vars():
+    assert auth_key_var is not None