Skip to content

Commit f70bccd

Browse files
authored
Move reactpy.backend.hooks module into reactpy.core.hooks (#1210)
1 parent 3dc0c23 commit f70bccd

File tree

8 files changed

+67
-23
lines changed

8 files changed

+67
-23
lines changed

docs/source/about/changelog.rst

+3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ Unreleased
4040
fragment to conditionally render an element by writing
4141
``something if condition else html._()``. Now you can simply write
4242
``something if condition else None``.
43+
- :pull:`1210` - Move hooks from ``reactpy.backend.hooks`` into ``reactpy.core.hooks``.
4344

4445
**Deprecated**
4546

4647
- :pull:`1171` - The ``Stop`` exception. Recent releases of ``anyio`` have made this
4748
exception difficult to use since it now raises an ``ExceptionGroup``. This exception
4849
was primarily used for internal testing purposes and so is now deprecated.
50+
- :pull:`1210` - Deprecate ``reactpy.backend.hooks`` since the hooks have been moved into
51+
``reactpy.core.hooks``.
4952

5053

5154
v1.0.2

src/py/reactpy/reactpy/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
from reactpy import backend, config, html, logging, sample, svg, types, web, widgets
2-
from reactpy.backend.hooks import use_connection, use_location, use_scope
32
from reactpy.backend.utils import run
43
from reactpy.core import hooks
54
from reactpy.core.component import component
65
from reactpy.core.events import event
76
from reactpy.core.hooks import (
87
create_context,
98
use_callback,
9+
use_connection,
1010
use_context,
1111
use_debug_value,
1212
use_effect,
13+
use_location,
1314
use_memo,
1415
use_reducer,
1516
use_ref,
17+
use_scope,
1618
use_state,
1719
)
1820
from reactpy.core.layout import Layout

src/py/reactpy/reactpy/backend/flask.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
safe_client_build_dir_path,
3636
safe_web_modules_dir_path,
3737
)
38-
from reactpy.backend.hooks import ConnectionContext
39-
from reactpy.backend.hooks import use_connection as _use_connection
4038
from reactpy.backend.types import Connection, Location
39+
from reactpy.core.hooks import ConnectionContext
40+
from reactpy.core.hooks import use_connection as _use_connection
4141
from reactpy.core.serve import serve_layout
4242
from reactpy.core.types import ComponentType, RootComponentConstructor
4343
from reactpy.utils import Ref
+28-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
1-
from __future__ import annotations
1+
from __future__ import annotations # nocov
22

3-
from collections.abc import MutableMapping
4-
from typing import Any
3+
from collections.abc import MutableMapping # nocov
4+
from typing import Any # nocov
55

6-
from reactpy.backend.types import Connection, Location
7-
from reactpy.core.hooks import create_context, use_context
8-
from reactpy.core.types import Context
6+
from reactpy._warnings import warn # nocov
7+
from reactpy.backend.types import Connection, Location # nocov
8+
from reactpy.core.hooks import ConnectionContext, use_context # nocov
99

10-
# backend implementations should establish this context at the root of an app
11-
ConnectionContext: Context[Connection[Any] | None] = create_context(None)
1210

13-
14-
def use_connection() -> Connection[Any]:
11+
def use_connection() -> Connection[Any]: # nocov
1512
"""Get the current :class:`~reactpy.backend.types.Connection`."""
13+
warn(
14+
"The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ",
15+
"Call reactpy.use_connection instead.",
16+
DeprecationWarning,
17+
)
18+
1619
conn = use_context(ConnectionContext)
17-
if conn is None: # nocov
20+
if conn is None:
1821
msg = "No backend established a connection."
1922
raise RuntimeError(msg)
2023
return conn
2124

2225

23-
def use_scope() -> MutableMapping[str, Any]:
26+
def use_scope() -> MutableMapping[str, Any]: # nocov
2427
"""Get the current :class:`~reactpy.backend.types.Connection`'s scope."""
28+
warn(
29+
"The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ",
30+
"Call reactpy.use_scope instead.",
31+
DeprecationWarning,
32+
)
33+
2534
return use_connection().scope
2635

2736

28-
def use_location() -> Location:
37+
def use_location() -> Location: # nocov
2938
"""Get the current :class:`~reactpy.backend.types.Connection`'s location."""
39+
warn(
40+
"The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ",
41+
"Call reactpy.use_location instead.",
42+
DeprecationWarning,
43+
)
44+
3045
return use_connection().location

src/py/reactpy/reactpy/backend/sanic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
safe_web_modules_dir_path,
2525
serve_with_uvicorn,
2626
)
27-
from reactpy.backend.hooks import ConnectionContext
28-
from reactpy.backend.hooks import use_connection as _use_connection
2927
from reactpy.backend.types import Connection, Location
28+
from reactpy.core.hooks import ConnectionContext
29+
from reactpy.core.hooks import use_connection as _use_connection
3030
from reactpy.core.layout import Layout
3131
from reactpy.core.serve import RecvCoroutine, SendCoroutine, Stop, serve_layout
3232
from reactpy.core.types import RootComponentConstructor

src/py/reactpy/reactpy/backend/starlette.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
read_client_index_html,
2525
serve_with_uvicorn,
2626
)
27-
from reactpy.backend.hooks import ConnectionContext
28-
from reactpy.backend.hooks import use_connection as _use_connection
2927
from reactpy.backend.types import Connection, Location
3028
from reactpy.config import REACTPY_WEB_MODULES_DIR
29+
from reactpy.core.hooks import ConnectionContext
30+
from reactpy.core.hooks import use_connection as _use_connection
3131
from reactpy.core.layout import Layout
3232
from reactpy.core.serve import RecvCoroutine, SendCoroutine, serve_layout
3333
from reactpy.core.types import RootComponentConstructor

src/py/reactpy/reactpy/backend/tornado.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
CommonOptions,
2525
read_client_index_html,
2626
)
27-
from reactpy.backend.hooks import ConnectionContext
28-
from reactpy.backend.hooks import use_connection as _use_connection
2927
from reactpy.backend.types import Connection, Location
3028
from reactpy.config import REACTPY_WEB_MODULES_DIR
29+
from reactpy.core.hooks import ConnectionContext
30+
from reactpy.core.hooks import use_connection as _use_connection
3131
from reactpy.core.layout import Layout
3232
from reactpy.core.serve import serve_layout
3333
from reactpy.core.types import ComponentConstructor

src/py/reactpy/reactpy/core/hooks.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4-
from collections.abc import Coroutine, Sequence
4+
from collections.abc import Coroutine, MutableMapping, Sequence
55
from logging import getLogger
66
from types import FunctionType
77
from typing import (
@@ -17,6 +17,7 @@
1717

1818
from typing_extensions import TypeAlias
1919

20+
from reactpy.backend.types import Connection, Location
2021
from reactpy.config import REACTPY_DEBUG_MODE
2122
from reactpy.core._life_cycle_hook import current_hook
2223
from reactpy.core.types import Context, Key, State, VdomDict
@@ -248,6 +249,29 @@ def use_context(context: Context[_Type]) -> _Type:
248249
return provider.value
249250

250251

252+
# backend implementations should establish this context at the root of an app
253+
ConnectionContext: Context[Connection[Any] | None] = create_context(None)
254+
255+
256+
def use_connection() -> Connection[Any]:
257+
"""Get the current :class:`~reactpy.backend.types.Connection`."""
258+
conn = use_context(ConnectionContext)
259+
if conn is None: # nocov
260+
msg = "No backend established a connection."
261+
raise RuntimeError(msg)
262+
return conn
263+
264+
265+
def use_scope() -> MutableMapping[str, Any]:
266+
"""Get the current :class:`~reactpy.backend.types.Connection`'s scope."""
267+
return use_connection().scope
268+
269+
270+
def use_location() -> Location:
271+
"""Get the current :class:`~reactpy.backend.types.Connection`'s location."""
272+
return use_connection().location
273+
274+
251275
class _ContextProvider(Generic[_Type]):
252276
def __init__(
253277
self,

0 commit comments

Comments
 (0)