Skip to content

Commit bd7475d

Browse files
committed
Organise blob, deps, and outputs
I've moved dependencies into a convenience module `deps`, so now dependencies, outputs, and blob subclasses are imported as `lt.blob.MyBlob` etc. to avoid cluttering the global namespace too much. `deps` is used rather than `dependencies` to avoid circular imports and allow for future rearrangements, as well as keeping type annotations shorter if it's imported as `lt.deps.Whatever`. Test code is updated to use the new imports.
1 parent 5ad1e40 commit bd7475d

File tree

10 files changed

+56
-29
lines changed

10 files changed

+56
-29
lines changed

src/labthings_fastapi/__init__.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66
thing_action,
77
fastapi_endpoint,
88
)
9-
from .dependencies.blocking_portal import BlockingPortal
10-
from .dependencies.invocation import InvocationID, InvocationLogger, CancelHook
11-
from .dependencies.metadata import GetThingStates
12-
from .dependencies.raw_thing import raw_thing_dependency
13-
from .dependencies.thing import direct_thing_client_dependency
14-
from .outputs.mjpeg_stream import MJPEGStream, MJPEGStreamDescriptor
15-
from .outputs.blob import Blob
9+
from . import deps
10+
from . import outputs
11+
from .outputs import blob
1612
from .server import ThingServer
1713

1814
# The symbols in __all__ are part of our public API.
@@ -29,15 +25,8 @@
2925
"thing_setting",
3026
"thing_action",
3127
"fastapi_endpoint",
32-
"BlockingPortal",
33-
"InvocationID",
34-
"InvocationLogger",
35-
"CancelHook",
36-
"GetThingStates",
37-
"raw_thing_dependency",
38-
"direct_thing_client_dependency",
39-
"MJPEGStream",
40-
"MJPEGStreamDescriptor",
41-
"Blob",
28+
"deps",
29+
"outputs",
30+
"blob",
4231
"ThingServer",
4332
]

src/labthings_fastapi/deps.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
FastAPI dependencies for LabThings.
3+
4+
The symbols in this module are type annotations that can be used in
5+
the arguments of Action methods (or FastAPI endpoints) to
6+
automatically supply the required dependencies.
7+
8+
See the documentation on dependencies for more details of how to use
9+
these.
10+
"""
11+
12+
from .dependencies.blocking_portal import BlockingPortal
13+
from .dependencies.invocation import InvocationID, InvocationLogger, CancelHook
14+
from .dependencies.metadata import GetThingStates
15+
from .dependencies.raw_thing import raw_thing_dependency
16+
from .dependencies.thing import direct_thing_client_dependency
17+
18+
# The symbols in __all__ are part of our public API. See note
19+
# in src/labthings_fastapi/__init__.py for more details.
20+
__all__ = [
21+
"BlockingPortal",
22+
"InvocationID",
23+
"InvocationLogger",
24+
"CancelHook",
25+
"GetThingStates",
26+
"raw_thing_dependency",
27+
"direct_thing_client_dependency",
28+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from .mjpeg_stream import MJPEGStream, MJPEGStreamDescriptor
2+
3+
# __all__ enables convenience imports from this module.
4+
# see the note in src/labthings_fastapi/__init__.py for more details.
5+
# `blob` is intentionally missing: it will likely be promoted out of
6+
# `outputs` in the future.
7+
__all__ = [
8+
"MJPEGStream",
9+
"MJPEGStreamDescriptor",
10+
]

tests/test_action_cancel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ThingOne(lt.Thing):
1212
counter = lt.ThingProperty(int, 0, observable=False)
1313

1414
@lt.thing_action
15-
def count_slowly(self, cancel: lt.CancelHook, n: int = 10):
15+
def count_slowly(self, cancel: lt.deps.CancelHook, n: int = 10):
1616
for i in range(n):
1717
cancel.sleep(0.1)
1818
self.counter += 1

tests/test_action_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ThingOne(lt.Thing):
1616
]
1717

1818
@lt.thing_action
19-
def action_one(self, logger: lt.InvocationLogger):
19+
def action_one(self, logger: lt.deps.InvocationLogger):
2020
for m in self.LOG_MESSAGES:
2121
logger.info(m)
2222

tests/test_blob_output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from labthings_fastapi.client import ThingClient
1313

1414

15-
class TextBlob(lt.Blob):
15+
class TextBlob(lt.blob.Blob):
1616
media_type: str = "text/plain"
1717

1818

@@ -49,7 +49,7 @@ def passthrough_blob(self, blob: TextBlob) -> TextBlob:
4949
return blob
5050

5151

52-
ThingOneDep = lt.direct_thing_client_dependency(ThingOne, "/thing_one/")
52+
ThingOneDep = lt.deps.direct_thing_client_dependency(ThingOne, "/thing_one/")
5353

5454

5555
class ThingTwo(lt.Thing):

tests/test_dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
from fastapi import Depends, FastAPI, Request
8-
from labthings_fastapi import InvocationID
8+
from labthings_fastapi.deps import InvocationID
99
from labthings_fastapi.file_manager import FileManagerDep
1010
from fastapi.testclient import TestClient
1111
from module_with_deps import FancyIDDep

tests/test_dependencies_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_invocation_id_alias():
140140
app = FastAPI()
141141

142142
@app.post("/endpoint")
143-
def endpoint(id: lt.InvocationID) -> bool:
143+
def endpoint(id: lt.deps.InvocationID) -> bool:
144144
return True
145145

146146
with TestClient(app) as client:

tests/test_dependency_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def thing_state(self):
2727
return {"a": self.a}
2828

2929

30-
ThingOneDep = lt.direct_thing_client_dependency(ThingOne, "/thing_one/")
30+
ThingOneDep = lt.deps.direct_thing_client_dependency(ThingOne, "/thing_one/")
3131

3232

3333
class ThingTwo(lt.Thing):

tests/test_thing_dependencies.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def action_one_internal(self) -> str:
2424
return self.ACTION_ONE_RESULT
2525

2626

27-
ThingOneDep = lt.direct_thing_client_dependency(ThingOne, "/thing_one/")
27+
ThingOneDep = lt.deps.direct_thing_client_dependency(ThingOne, "/thing_one/")
2828

2929

3030
class ThingTwo(lt.Thing):
@@ -39,7 +39,7 @@ def action_two_a(self, thing_one: ThingOneDep) -> str:
3939
return thing_one.action_one()
4040

4141

42-
ThingTwoDep = lt.direct_thing_client_dependency(ThingTwo, "/thing_two/")
42+
ThingTwoDep = lt.deps.direct_thing_client_dependency(ThingTwo, "/thing_two/")
4343

4444

4545
class ThingThree(lt.Thing):
@@ -113,7 +113,7 @@ def test_raw_interthing_dependency():
113113
114114
This uses the internal thing client mechanism.
115115
"""
116-
ThingOneDep = lt.raw_thing_dependency(ThingOne)
116+
ThingOneDep = lt.deps.raw_thing_dependency(ThingOne)
117117

118118
class ThingTwo(lt.Thing):
119119
@lt.thing_action
@@ -139,7 +139,7 @@ def test_conflicting_dependencies():
139139
This also checks that dependencies on the same class but different
140140
actions are recognised as "different".
141141
"""
142-
ThingTwoDepNoActions = lt.direct_thing_client_dependency(
142+
ThingTwoDepNoActions = lt.deps.direct_thing_client_dependency(
143143
ThingTwo, "/thing_two/", []
144144
)
145145

@@ -153,7 +153,7 @@ def action_five(self, thing_two: ThingTwoDep) -> str:
153153
return thing_two.action_two()
154154

155155
with pytest.raises(ValueError):
156-
lt.direct_thing_client_dependency(ThingFour, "/thing_four/")
156+
lt.deps.direct_thing_client_dependency(ThingFour, "/thing_four/")
157157

158158

159159
def check_request():

0 commit comments

Comments
 (0)