-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oliver Copping
committed
Apr 16, 2024
1 parent
8886dc5
commit a821452
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import tempfile | ||
|
||
import mypy.api | ||
import pytest | ||
from ophyd import sim | ||
|
||
from ophyd_async import protocols as bs_protocols | ||
|
||
|
||
def test_readable(): | ||
assert isinstance(sim.motor1, bs_protocols.AsyncReadable) | ||
assert isinstance(sim.det1, bs_protocols.AsyncReadable) | ||
assert not isinstance(sim.flyer1, bs_protocols.AsyncReadable) | ||
|
||
|
||
def test_pausable(): | ||
assert isinstance(sim.det1, bs_protocols.AsyncPausable) | ||
|
||
|
||
# I think the commented out tests pass because __getattr__ is implemented, but not sure | ||
@pytest.mark.skip(reason="ophyd missing py.typed to communicate type hints to mypy") | ||
@pytest.mark.parametrize( | ||
"type_, hardware, pass_", | ||
[ | ||
("Readable", "ABDetector(name='hi')", True), | ||
("Readable", "SynAxis(name='motor1')", True), | ||
("Readable", "TrivialFlyer()", False), | ||
("Configurable", "ABDetector(name='hi')", True), | ||
("Pausable", "ABDetector(name='hi')", True), | ||
], | ||
) | ||
def test_mypy(type_, hardware, pass_): | ||
template = f""" | ||
from ophyd_async import protocols as bs_protocols | ||
from ophyd import sim | ||
var: bs_protocols.{type_} = sim.{hardware} | ||
""" | ||
|
||
with tempfile.NamedTemporaryFile("wt") as f: | ||
f.write(template) | ||
f.seek(0) | ||
stdout, stderr, exit = mypy.api.run([f.name]) | ||
# pass true means exit 0, pass false means nonzero exit | ||
try: | ||
assert exit != pass_ | ||
except AssertionError: | ||
print(stdout) | ||
print(stderr) | ||
raise |