Repository: reagento/dishka

Issue:
Show an error if `@decorate` is applied to context var

Discussion:

Patch:
diff --git a/src/dishka/registry.py b/src/dishka/registry.py
index 29c75bc..a072052 100644
--- a/src/dishka/registry.py
+++ b/src/dishka/registry.py
@@ -9,6 +9,7 @@ from .dependency_source import (
     ContextVariable,
     Decorator,
     Factory,
+    FactoryType,
 )
 from .entities.component import DEFAULT_COMPONENT, Component
 from .entities.key import DependencyKey
@@ -16,6 +17,7 @@ from .entities.scope import BaseScope, InvalidScopes
 from .exceptions import (
     CycleDependenciesError,
     GraphMissingFactoryError,
+    InvalidGraphError,
     NoFactoryError,
     UnknownScopeError,
 )
@@ -294,6 +296,10 @@ class RegistryBuilder:
         )
         self.decorator_depth[provides] += 1
         old_factory = registry.get_factory(provides)
+        if old_factory.type is FactoryType.CONTEXT:
+            raise InvalidGraphError(
+                f"Cannot apply decorator to context data {provides}",
+            )
         old_factory.provides = DependencyKey(
             undecorated_type, old_factory.provides.component,
         )


Test patch:
diff --git a/tests/unit/container/test_context_vars.py b/tests/unit/container/test_context_vars.py
index 664fb12..0311398 100644
--- a/tests/unit/container/test_context_vars.py
+++ b/tests/unit/container/test_context_vars.py
@@ -3,12 +3,13 @@ import pytest
 from dishka import (
     Provider,
     Scope,
+    decorate,
     make_async_container,
     make_container,
     provide,
 )
 from dishka.dependency_source import from_context
-from dishka.exceptions import NoContextValueError
+from dishka.exceptions import InvalidGraphError, NoContextValueError
 
 
 def test_simple():
@@ -61,10 +62,6 @@ async def test_2components():
 
 @pytest.mark.asyncio
 async def test_2components_factory():
-    class DefaultProvider(Provider):
-        scope = Scope.APP
-        a = from_context(provides=int)
-
     class MyProvider(Provider):
         scope = Scope.APP
         component = "XXX"
@@ -79,3 +76,17 @@ async def test_2components_factory():
 
     container = make_async_container(MyProvider(), context={int: 1})
     assert await container.get(float, component="XXX") == 100
+
+
+def test_decorate():
+    class MyProvider(Provider):
+        scope = Scope.APP
+
+        i = from_context(provides=int)
+
+        @decorate
+        def ii(self, i: int) -> int:
+            return i + 1
+
+    with pytest.raises(InvalidGraphError):
+        make_container(MyProvider(), context={int: 1})