Skip to content

Commit fc1ba7c

Browse files
committed
Tidy up imports in tests, docstrings, and code
Imports of `labthings_fastapi` within the package are now consistently changed to relative (.) imports. Tests now use `import labthings_fastapi as lt` wherever possible. Documentation has been updated to use the new import style.
1 parent ee24aa2 commit fc1ba7c

File tree

14 files changed

+36
-28
lines changed

14 files changed

+36
-28
lines changed

src/labthings_fastapi/actions/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from fastapi.responses import FileResponse
1212
from pydantic import BaseModel
1313

14-
from labthings_fastapi.utilities import model_to_dict
15-
from labthings_fastapi.utilities.introspection import EmptyInput
14+
from ..utilities import model_to_dict
15+
from ..utilities.introspection import EmptyInput
1616
from ..thing_description.model import LinkElement
1717
from ..file_manager import FileManager
1818
from .invocation_model import InvocationModel, InvocationStatus

src/labthings_fastapi/actions/invocation_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pydantic import BaseModel, ConfigDict, model_validator
99

10-
from labthings_fastapi.thing_description.model import Links
10+
from ..thing_description.model import Links
1111

1212

1313
class InvocationStatus(Enum):

src/labthings_fastapi/client/in_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
import logging
1515
from typing import Any, Mapping, Optional, Union
1616
from pydantic import BaseModel
17-
from labthings_fastapi.descriptors.action import ActionDescriptor
17+
from ..descriptors.action import ActionDescriptor
1818

19-
from labthings_fastapi.descriptors.property import ThingProperty
20-
from labthings_fastapi.utilities import attributes
19+
from ..descriptors.property import ThingProperty
20+
from ..utilities import attributes
2121
from . import PropertyClientDescriptor
2222
from ..thing import Thing
2323
from ..dependencies.thing_server import find_thing_server

src/labthings_fastapi/dependencies/thing_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from fastapi import FastAPI, Request
1313

1414
if TYPE_CHECKING:
15-
from labthings_fastapi.server import ThingServer
15+
from ..server import ThingServer
1616

1717
_thing_servers: WeakSet[ThingServer] = WeakSet()
1818

src/labthings_fastapi/descriptors/endpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22
from functools import partial, wraps
33

4-
from labthings_fastapi.utilities.introspection import get_docstring, get_summary
4+
from ..utilities.introspection import get_docstring, get_summary
55

66
from typing import (
77
Callable,

src/labthings_fastapi/server/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections.abc import Mapping
1111
from types import MappingProxyType
1212

13-
from labthings_fastapi.utilities.object_reference_to_object import (
13+
from ..utilities.object_reference_to_object import (
1414
object_reference_to_object,
1515
)
1616
from ..actions import ActionManager

src/labthings_fastapi/server/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional
33
import json
44

5-
from labthings_fastapi.utilities.object_reference_to_object import (
5+
from ..utilities.object_reference_to_object import (
66
object_reference_to_object,
77
)
88
import uvicorn

src/labthings_fastapi/utilities/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pydantic import BaseModel, ConfigDict, Field, RootModel, create_model
55
from pydantic.dataclasses import dataclass
66
from anyio.from_thread import BlockingPortal
7-
from labthings_fastapi.utilities.introspection import EmptyObject
7+
from .introspection import EmptyObject
88

99
if TYPE_CHECKING:
1010
from ..thing import Thing

tests/test_blob_output.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from fastapi.testclient import TestClient
99
import pytest
1010
import labthings_fastapi as lt
11-
from labthings_fastapi.outputs.blob import blob_type
12-
from labthings_fastapi.client import ThingClient
1311

1412

1513
class TextBlob(lt.blob.Blob):
@@ -71,8 +69,8 @@ def check_passthrough(self, thing_one: ThingOneDep) -> bool:
7169
def test_blob_type():
7270
"""Check we can't put dodgy values into a blob output model"""
7371
with pytest.raises(ValueError):
74-
blob_type(media_type="text/plain\\'DROP TABLES")
75-
M = blob_type(media_type="text/plain")
72+
lt.blob.blob_type(media_type="text/plain\\'DROP TABLES")
73+
M = lt.blob.blob_type(media_type="text/plain")
7674
assert M.from_bytes(b"").media_type == "text/plain"
7775

7876

@@ -99,7 +97,7 @@ def test_blob_output_client():
9997
server = lt.ThingServer()
10098
server.add_thing(ThingOne(), "/thing_one")
10199
with TestClient(server.app) as client:
102-
tc = ThingClient.from_url("/thing_one/", client=client)
100+
tc = lt.ThingClient.from_url("/thing_one/", client=client)
103101
check_actions(tc)
104102

105103

@@ -115,7 +113,7 @@ def test_blob_output_inserver():
115113
server.add_thing(ThingOne(), "/thing_one")
116114
server.add_thing(ThingTwo(), "/thing_two")
117115
with TestClient(server.app) as client:
118-
tc = ThingClient.from_url("/thing_two/", client=client)
116+
tc = lt.ThingClient.from_url("/thing_two/", client=client)
119117
output = tc.check_both()
120118
assert output is True
121119

@@ -145,7 +143,7 @@ def test_blob_input():
145143
server.add_thing(ThingOne(), "/thing_one")
146144
server.add_thing(ThingTwo(), "/thing_two")
147145
with TestClient(server.app) as client:
148-
tc = ThingClient.from_url("/thing_one/", client=client)
146+
tc = lt.ThingClient.from_url("/thing_one/", client=client)
149147
output = tc.action_one()
150148
print(f"Output is {output}")
151149
assert output is not None
@@ -157,7 +155,7 @@ def test_blob_input():
157155
assert passthrough.content == ThingOne.ACTION_ONE_RESULT
158156

159157
# Check that the same thing works on the server side
160-
tc2 = ThingClient.from_url("/thing_two/", client=client)
158+
tc2 = lt.ThingClient.from_url("/thing_two/", client=client)
161159
assert tc2.check_passthrough() is True
162160

163161

tests/test_dependencies_2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
33
Class-based dependencies in modules with `from __future__ import annotations`
44
fail if they have sub-dependencies, because the global namespace is not found by
5-
pydantic. The work-around is to add a line to each class definition:
5+
pydantic. The work-around was to add a line to each class definition:
66
```
77
__globals__ = globals()
88
```
99
This bakes in the global namespace of the module, and allows FastAPI to correctly
1010
traverse the dependency tree.
1111
12+
This is related to https://github.com/fastapi/fastapi/issues/4557 and may have
13+
been fixed upstream in FastAPI.
14+
1215
The tests in this module were written while I was figuring this out: they mostly
1316
test things from FastAPI that obviously work, but I will leave them in here as
1417
mitigation against something changing in the future.
@@ -19,9 +22,7 @@
1922
from fastapi.testclient import TestClient
2023
from module_with_deps import FancyIDDep, FancyID, ClassDependsOnFancyID
2124
import labthings_fastapi as lt
22-
from labthings_fastapi.dependencies.invocation import invocation_id
2325
from labthings_fastapi.file_manager import FileManager
24-
from uuid import UUID
2526

2627

2728
def test_dep_from_module():
@@ -127,7 +128,7 @@ def test_invocation_id():
127128
app = FastAPI()
128129

129130
@app.post("/endpoint")
130-
def invoke_fancy(id: Annotated[UUID, Depends(invocation_id)]) -> bool:
131+
def invoke_fancy(id: lt.deps.InvocationID) -> bool:
131132
return True
132133

133134
with TestClient(app) as client:

0 commit comments

Comments
 (0)