Format Python files with ruff

This commit is contained in:
Simon Hausmann 2025-02-12 23:06:38 +01:00 committed by Simon Hausmann
parent 9001966dc9
commit 3358bc24e3
15 changed files with 261 additions and 191 deletions

View file

@ -5,7 +5,6 @@ from slint import slint as native
from slint import Color, Brush
def test_col_default() -> None:
col = Color()
assert col.red == 0
@ -24,7 +23,7 @@ def test_col_from_str() -> None:
def test_col_from_rgb_dict() -> None:
coldict = {'red': 0x12, 'green': 0x34, 'blue': 0x56}
coldict = {"red": 0x12, "green": 0x34, "blue": 0x56}
col = Color(coldict)
assert col.red == 0x12
assert col.green == 0x34
@ -33,7 +32,7 @@ def test_col_from_rgb_dict() -> None:
def test_col_from_rgba_dict() -> None:
coldict = {'red': 0x12, 'green': 0x34, 'blue': 0x56, 'alpha': 128}
coldict = {"red": 0x12, "green": 0x34, "blue": 0x56, "alpha": 128}
col = Color(coldict)
assert col.red == 0x12
assert col.green == 0x34

View file

@ -19,7 +19,7 @@ def base_dir() -> str:
def test_callback_decorators(caplog: pytest.LogCaptureFixture) -> None:
module = load_file(os.path.join(base_dir(), "test-load-file.slint"), quiet=False)
class SubClass(module.App): # type: ignore
class SubClass(module.App): # type: ignore
@slint.callback()
def say_hello_again(self, arg: str) -> str:
return "say_hello_again:" + arg

View file

@ -15,7 +15,8 @@ def test_basic_compiler() -> None:
assert len(compiler.build_from_source("Garbage", "").component_names) == 0
result = compiler.build_from_source("""
result = compiler.build_from_source(
"""
export global TestGlobal {
in property <string> theglobalprop;
callback globallogic();
@ -35,7 +36,9 @@ def test_basic_compiler() -> None:
callback test-callback();
public function ff() {}
}
""", "")
""",
"",
)
assert result.component_names == ["Test"]
compdef = result.component("Test")
@ -44,8 +47,16 @@ def test_basic_compiler() -> None:
assert compdef.name == "Test"
props = [(name, type) for name, type in compdef.properties.items()]
assert props == [('boolprop', ValueType.Bool), ('brushprop', ValueType.Brush), ('colprop', ValueType.Brush), ('floatprop', ValueType.Number),
('imgprop', ValueType.Image), ('intprop', ValueType.Number), ('modelprop', ValueType.Model), ('strprop', ValueType.String)]
assert props == [
("boolprop", ValueType.Bool),
("brushprop", ValueType.Brush),
("colprop", ValueType.Brush),
("floatprop", ValueType.Number),
("imgprop", ValueType.Image),
("intprop", ValueType.Number),
("modelprop", ValueType.Model),
("strprop", ValueType.String),
]
assert compdef.callbacks == ["test-callback"]
assert compdef.functions == ["ff"]
@ -53,8 +64,9 @@ def test_basic_compiler() -> None:
assert compdef.globals == ["TestGlobal"]
assert compdef.global_properties("Garbage") == None
assert [(name, type) for name, type in compdef.global_properties(
"TestGlobal").items()] == [('theglobalprop', ValueType.String)]
assert [
(name, type) for name, type in compdef.global_properties("TestGlobal").items()
] == [("theglobalprop", ValueType.String)]
assert compdef.global_callbacks("Garbage") == None
assert compdef.global_callbacks("TestGlobal") == ["globallogic"]

View file

@ -10,12 +10,15 @@ import typing
def test_callback_gc() -> None:
compiler = native.Compiler()
compdef = compiler.build_from_source("""
compdef = compiler.build_from_source(
"""
export component Test {
out property <string> test-value: "Ok";
callback test-callback(string) -> string;
}
""", "").component("Test")
""",
"",
).component("Test")
assert compdef != None
instance: native.ComponentInstance | None = compdef.create()
@ -30,8 +33,7 @@ def test_callback_gc() -> None:
handler: Handler | None = Handler(instance)
assert handler is not None
instance.set_callback(
"test-callback", handler.python_callback)
instance.set_callback("test-callback", handler.python_callback)
handler = None
assert instance.invoke("test-callback", "World") == "WorldOk"

View file

@ -7,11 +7,11 @@ from slint.slint import ValueType, Image, Color, Brush
import os
def test_property_access() -> None:
compiler = native.Compiler()
compdef = compiler.build_from_source("""
compdef = compiler.build_from_source(
"""
export global TestGlobal {
in property <string> theglobalprop: "Hey";
callback globallogic();
@ -41,7 +41,9 @@ def test_property_access() -> None:
callback test-callback();
}
""", os.path.join(os.path.dirname(__file__), "main.slint")).component("Test")
""",
os.path.join(os.path.dirname(__file__), "main.slint"),
).component("Test")
assert compdef != None
instance = compdef.create()
@ -80,7 +82,8 @@ def test_property_access() -> None:
assert structval.finished == True
assert structval.dash_prop == True
instance.set_property(
"structprop", {'title': 'new', 'finished': False, 'dash_prop': False})
"structprop", {"title": "new", "finished": False, "dash_prop": False}
)
structval = instance.get_property("structprop")
assert structval.title == "new"
assert structval.finished == False
@ -94,14 +97,23 @@ def test_property_access() -> None:
with pytest.raises(RuntimeError, match="The image cannot be loaded"):
Image.load_from_path("non-existent.png")
instance.set_property("imageprop", Image.load_from_path(os.path.join(
os.path.dirname(__file__), "../../../examples/iot-dashboard/images/humidity.png")))
instance.set_property(
"imageprop",
Image.load_from_path(
os.path.join(
os.path.dirname(__file__),
"../../../examples/iot-dashboard/images/humidity.png",
)
),
)
imageval = instance.get_property("imageprop")
assert imageval.size == (36, 36)
assert "humidity.png" in imageval.path
with pytest.raises(TypeError, match="'int' object cannot be converted to 'PyString'"):
instance.set_property("structprop", {42: 'wrong'})
with pytest.raises(
TypeError, match="'int' object cannot be converted to 'PyString'"
):
instance.set_property("structprop", {42: "wrong"})
brushval = instance.get_property("brushprop")
assert str(brushval.color) == "argb(255, 255, 0, 255)"
@ -128,7 +140,8 @@ def test_property_access() -> None:
def test_callbacks() -> None:
compiler = native.Compiler()
compdef = compiler.build_from_source("""
compdef = compiler.build_from_source(
"""
export global TestGlobal {
callback globallogic(string) -> string;
globallogic(value) => {
@ -143,7 +156,9 @@ def test_callbacks() -> None:
}
callback void-callback();
}
""", "").component("Test")
""",
"",
).component("Test")
assert compdef != None
instance = compdef.create()
@ -151,8 +166,7 @@ def test_callbacks() -> None:
assert instance.invoke("test-callback", "foo") == "local foo"
assert instance.invoke_global(
"TestGlobal", "globallogic", "foo") == "global foo"
assert instance.invoke_global("TestGlobal", "globallogic", "foo") == "global foo"
with pytest.raises(RuntimeError, match="no such callback"):
instance.set_callback("non-existent", lambda x: x)
@ -164,9 +178,12 @@ def test_callbacks() -> None:
instance.set_global_callback("TestGlobal", "non-existent", lambda x: x)
instance.set_global_callback(
"TestGlobal", "globallogic", lambda x: "python global " + x)
assert instance.invoke_global(
"TestGlobal", "globallogic", "foo") == "python global foo"
"TestGlobal", "globallogic", lambda x: "python global " + x
)
assert (
instance.invoke_global("TestGlobal", "globallogic", "foo")
== "python global foo"
)
instance.set_callback("void-callback", lambda: None)
instance.invoke("void-callback")
@ -174,9 +191,8 @@ def test_callbacks() -> None:
if __name__ == "__main__":
import slint
module = slint.load_file(
"../../demos/printerdemo/ui/printerdemo.slint")
module = slint.load_file("../../demos/printerdemo/ui/printerdemo.slint")
instance = module.MainWindow()
instance.PrinterQueue.start_job = lambda title: print(
f"new print job {title}")
instance.PrinterQueue.start_job = lambda title: print(f"new print job {title}")
instance.run()

View file

@ -14,10 +14,14 @@ def base_dir() -> str:
assert base_dir is not None
return base_dir
def test_load_file(caplog: pytest.LogCaptureFixture) -> None:
module = load_file(os.path.join(base_dir(), "test-load-file.slint"), quiet=False)
assert "The property 'color' has been deprecated. Please use 'background' instead" in caplog.text
assert (
"The property 'color' has been deprecated. Please use 'background' instead"
in caplog.text
)
assert len(list(module.__dict__.keys())) == 6
assert "App" in module.__dict__

View file

@ -17,8 +17,7 @@ def test_magic_import_path() -> None:
oldsyspath = sys.path
assert loader.printerdemo == None
try:
sys.path.append(os.path.join(os.path.dirname(__file__),
"..", "..", ".."))
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
instance = loader.demos.printerdemo.ui.printerdemo.MainWindow()
del instance
finally:

View file

@ -9,7 +9,8 @@ import typing
def test_model_notify() -> None:
compiler = native.Compiler()
compdef = compiler.build_from_source("""
compdef = compiler.build_from_source(
"""
export component App {
width: 300px;
height: 300px;
@ -29,7 +30,9 @@ def test_model_notify() -> None:
}
}
""", "").component("App")
""",
"",
).component("App")
assert compdef != None
instance = compdef.create()
@ -37,8 +40,7 @@ def test_model_notify() -> None:
model = models.ListModel([100, 0])
instance.set_property(
"fixed-height-model", model)
instance.set_property("fixed-height-model", model)
assert instance.get_property("layout-height") == 100
model.set_row_data(1, 50)
@ -48,18 +50,20 @@ def test_model_notify() -> None:
del model[1:]
assert instance.get_property("layout-height") == 100
assert isinstance(instance.get_property(
"fixed-height-model"), models.ListModel)
assert isinstance(instance.get_property("fixed-height-model"), models.ListModel)
def test_model_from_list() -> None:
compiler = native.Compiler()
compdef = compiler.build_from_source("""
compdef = compiler.build_from_source(
"""
export component App {
in-out property<[int]> data: [1, 2, 3, 4];
}
""", "").component("App")
""",
"",
).component("App")
assert compdef != None
instance = compdef.create()
@ -100,11 +104,14 @@ def test_python_model_iterable() -> None:
def test_rust_model_sequence() -> None:
compiler = native.Compiler()
compdef = compiler.build_from_source("""
compdef = compiler.build_from_source(
"""
export component App {
in-out property<[int]> data: [1, 2, 3, 4, 5];
}
""", "").component("App")
""",
"",
).component("App")
assert compdef != None
instance = compdef.create()
@ -120,7 +127,8 @@ def test_rust_model_sequence() -> None:
def test_model_writeback() -> None:
compiler = native.Compiler()
compdef = compiler.build_from_source("""
compdef = compiler.build_from_source(
"""
export component App {
width: 300px;
height: 300px;
@ -132,7 +140,9 @@ def test_model_writeback() -> None:
}
}
""", "").component("App")
""",
"",
).component("App")
assert compdef != None
instance = compdef.create()
@ -140,8 +150,7 @@ def test_model_writeback() -> None:
model = models.ListModel([100, 0])
instance.set_property(
"model", model)
instance.set_property("model", model)
instance.invoke("write-to-model", 1, 42)
assert list(instance.get_property("model")) == [100, 42]

View file

@ -3,7 +3,7 @@
import pytest
from slint import slint as native
from slint.slint import ValueType;
from slint.slint import ValueType
from datetime import timedelta
counter: int
@ -12,6 +12,7 @@ counter: int
def test_timer() -> None:
global counter
counter = 0
def quit_after_two_invocations() -> None:
global counter
counter = min(counter + 1, 2)
@ -19,10 +20,15 @@ def test_timer() -> None:
native.quit_event_loop()
test_timer = native.Timer()
test_timer.start(native.TimerMode.Repeated, timedelta(milliseconds=100), quit_after_two_invocations)
test_timer.start(
native.TimerMode.Repeated,
timedelta(milliseconds=100),
quit_after_two_invocations,
)
native.run_event_loop()
test_timer.stop()
assert(counter == 2)
assert counter == 2
def test_single_shot() -> None:
native.Timer.single_shot(timedelta(milliseconds=100), native.quit_event_loop)