From 0c824cf295a4640c2fa730e0c63fa3084b7e076b Mon Sep 17 00:00:00 2001 From: Patrick Kanzler <4189642+patkan@users.noreply.github.com> Date: Sat, 16 Dec 2023 23:09:20 +0100 Subject: [PATCH] More mypy (#612) * remove type comment where type is annotated * move function tests * remove six from tests * add none annotations * add more types * change mock (so that mypy understands it) --- src/escpos/cli.py | 4 ++-- test/conftest.py | 12 +++++------ test/test_config.py | 2 +- .../test_function_barcode.py | 0 .../test_function_buzzer.py | 0 .../test_function_cashdraw.py | 2 +- .../test_function_check_barcode.py | 0 .../{ => test_functions}/test_function_cut.py | 2 +- .../test_function_dummy_clear.py | 2 +- .../test_function_image.py | 2 +- .../test_function_linedisplay.py | 0 .../test_function_panel_button.py | 4 ++-- .../test_function_qr_native.py | 20 +++++++++---------- .../test_function_qr_non-native.py | 2 +- .../{ => test_functions}/test_function_set.py | 5 ++--- .../test_function_softbarcode.py | 6 +++--- .../test_function_text.py | 10 +++++----- test/{ => test_functions}/test_functions.py | 6 +++--- test/test_raise_arbitrary_error.py | 2 +- 19 files changed, 40 insertions(+), 41 deletions(-) rename test/{ => test_functions}/test_function_barcode.py (100%) rename test/{ => test_functions}/test_function_buzzer.py (100%) rename test/{ => test_functions}/test_function_cashdraw.py (88%) rename test/{ => test_functions}/test_function_check_barcode.py (100%) rename test/{ => test_functions}/test_function_cut.py (87%) rename test/{ => test_functions}/test_function_dummy_clear.py (77%) rename test/{ => test_functions}/test_function_image.py (99%) rename test/{ => test_functions}/test_function_linedisplay.py (100%) rename test/{ => test_functions}/test_function_panel_button.py (88%) rename test/{ => test_functions}/test_function_qr_native.py (88%) rename test/{ => test_functions}/test_function_qr_non-native.py (99%) rename test/{ => test_functions}/test_function_set.py (98%) rename test/{ => test_functions}/test_function_softbarcode.py (70%) rename test/{ => test_functions}/test_function_text.py (87%) rename test/{ => test_functions}/test_functions.py (82%) diff --git a/src/escpos/cli.py b/src/escpos/cli.py index e7cc826..f70d922 100644 --- a/src/escpos/cli.py +++ b/src/escpos/cli.py @@ -20,7 +20,7 @@ except ImportError: pass # noqa import sys -from . import config +from . import config, escpos from . import printer as escpos_printer_module from . import version @@ -617,7 +617,7 @@ def main() -> None: globals()[target_command](**command_arguments) -def demo(printer, **kwargs) -> None: +def demo(printer: escpos.Escpos, **kwargs) -> None: """Print demos. Called when CLI is passed `demo`. This function diff --git a/test/conftest.py b/test/conftest.py index 61d42ca..f4a58ef 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -5,32 +5,32 @@ from escpos.printer import LP, CupsPrinter, Dummy, File, Network, Serial, Usb, W @pytest.fixture -def driver(): +def driver() -> Dummy: return Dummy() @pytest.fixture -def usbprinter(): +def usbprinter() -> Usb: return Usb() @pytest.fixture -def serialprinter(): +def serialprinter() -> Serial: return Serial() @pytest.fixture -def networkprinter(): +def networkprinter() -> Network: return Network() @pytest.fixture -def fileprinter(): +def fileprinter() -> File: return File() @pytest.fixture -def lpprinter(): +def lpprinter() -> LP: return LP() diff --git a/test/test_config.py b/test/test_config.py index 831c09c..0c5368b 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -74,7 +74,7 @@ def test_config_load_with_missing_config(tmp_path): @pytest.mark.skip( "This test creates in the actual appdir files and is therefore skipped." ) -def test_config_load_from_appdir(): +def test_config_load_from_appdir() -> None: """Test the loading of a config in appdir.""" from escpos import config diff --git a/test/test_function_barcode.py b/test/test_functions/test_function_barcode.py similarity index 100% rename from test/test_function_barcode.py rename to test/test_functions/test_function_barcode.py diff --git a/test/test_function_buzzer.py b/test/test_functions/test_function_buzzer.py similarity index 100% rename from test/test_function_buzzer.py rename to test/test_functions/test_function_buzzer.py diff --git a/test/test_function_cashdraw.py b/test/test_functions/test_function_cashdraw.py similarity index 88% rename from test/test_function_cashdraw.py rename to test/test_functions/test_function_cashdraw.py index e976d0d..652677e 100644 --- a/test/test_function_cashdraw.py +++ b/test/test_functions/test_function_cashdraw.py @@ -6,7 +6,7 @@ import escpos.printer as printer from escpos.exceptions import CashDrawerError -def test_raise_CashDrawerError(): +def test_raise_CashDrawerError() -> None: """should raise an error if the sequence is invalid.""" instance = printer.Dummy() with pytest.raises(CashDrawerError): diff --git a/test/test_function_check_barcode.py b/test/test_functions/test_function_check_barcode.py similarity index 100% rename from test/test_function_check_barcode.py rename to test/test_functions/test_function_check_barcode.py diff --git a/test/test_function_cut.py b/test/test_functions/test_function_cut.py similarity index 87% rename from test/test_function_cut.py rename to test/test_functions/test_function_cut.py index b92c020..c002917 100644 --- a/test/test_function_cut.py +++ b/test/test_functions/test_function_cut.py @@ -2,7 +2,7 @@ import escpos.printer as printer from escpos.constants import GS -def test_cut_without_feed(): +def test_cut_without_feed() -> None: """Test cut without feeding paper""" instance = printer.Dummy() instance.cut(feed=False) diff --git a/test/test_function_dummy_clear.py b/test/test_functions/test_function_dummy_clear.py similarity index 77% rename from test/test_function_dummy_clear.py rename to test/test_functions/test_function_dummy_clear.py index 40ca9d4..b6bdd7b 100644 --- a/test/test_function_dummy_clear.py +++ b/test/test_functions/test_function_dummy_clear.py @@ -1,7 +1,7 @@ from escpos.printer import Dummy -def test_printer_dummy_clear(): +def test_printer_dummy_clear() -> None: printer = Dummy() printer.text("Hello") printer.clear() diff --git a/test/test_function_image.py b/test/test_functions/test_function_image.py similarity index 99% rename from test/test_function_image.py rename to test/test_functions/test_function_image.py index c4b31a5..925536d 100644 --- a/test/test_function_image.py +++ b/test/test_functions/test_function_image.py @@ -48,7 +48,7 @@ def test_bit_image_both() -> None: assert instance.output == b"\x1dv0\x00\x01\x00\x02\x00\xc0\x00" -def test_bit_image_transparent(): +def test_bit_image_transparent() -> None: """ Test printing black/transparent bit image (raster) """ diff --git a/test/test_function_linedisplay.py b/test/test_functions/test_function_linedisplay.py similarity index 100% rename from test/test_function_linedisplay.py rename to test/test_functions/test_function_linedisplay.py diff --git a/test/test_function_panel_button.py b/test/test_functions/test_function_panel_button.py similarity index 88% rename from test/test_function_panel_button.py rename to test/test_functions/test_function_panel_button.py index 5540771..d7507ec 100644 --- a/test/test_function_panel_button.py +++ b/test/test_functions/test_function_panel_button.py @@ -11,14 +11,14 @@ import escpos.printer as printer -def test_function_panel_button_on(): +def test_function_panel_button_on() -> None: """test the panel button function (enabling) by comparing output""" instance = printer.Dummy() instance.panel_buttons() assert instance.output == b"\x1B\x63\x35\x00" -def test_function_panel_button_off(): +def test_function_panel_button_off() -> None: """test the panel button function (disabling) by comparing output""" instance = printer.Dummy() instance.panel_buttons(False) diff --git a/test/test_function_qr_native.py b/test/test_functions/test_function_qr_native.py similarity index 88% rename from test/test_function_qr_native.py rename to test/test_functions/test_function_qr_native.py index e8cac69..bffeffa 100644 --- a/test/test_function_qr_native.py +++ b/test/test_functions/test_function_qr_native.py @@ -14,7 +14,7 @@ import escpos.printer as printer from escpos.constants import QR_ECLEVEL_H, QR_MODEL_1 -def test_defaults(): +def test_defaults() -> None: """Test QR code with defaults""" instance = printer.Dummy() instance.qr("1234", native=True) @@ -25,14 +25,14 @@ def test_defaults(): assert instance.output == expected -def test_empty(): +def test_empty() -> None: """Test QR printing blank code""" instance = printer.Dummy() instance.qr("", native=True) assert instance.output == b"" -def test_ec(): +def test_ec() -> None: """Test QR error correction setting""" instance = printer.Dummy() instance.qr("1234", native=True, ec=QR_ECLEVEL_H) @@ -43,7 +43,7 @@ def test_ec(): assert instance.output == expected -def test_size(): +def test_size() -> None: """Test QR box size""" instance = printer.Dummy() instance.qr("1234", native=True, size=7) @@ -54,7 +54,7 @@ def test_size(): assert instance.output == expected -def test_model(): +def test_model() -> None: """Test QR model""" instance = printer.Dummy() instance.qr("1234", native=True, model=QR_MODEL_1) @@ -65,28 +65,28 @@ def test_model(): assert instance.output == expected -def test_invalid_ec(): +def test_invalid_ec() -> None: """Test invalid QR error correction""" instance = printer.Dummy() with pytest.raises(ValueError): instance.qr("1234", native=True, ec=-1) -def test_invalid_size(): +def test_invalid_size() -> None: """Test invalid QR size""" instance = printer.Dummy() with pytest.raises(ValueError): instance.qr("1234", native=True, size=0) -def test_invalid_model(): +def test_invalid_model() -> None: """Test invalid QR model""" instance = printer.Dummy() with pytest.raises(ValueError): instance.qr("1234", native=True, model="Hello") -def test_image_invalid_model(): +def test_image_invalid_model() -> None: """Test unsupported QR model as image""" instance = printer.Dummy() with pytest.raises(ValueError): @@ -98,6 +98,6 @@ def instance(): return printer.Dummy() -def test_center_not_implementer(instance): +def test_center_not_implementer(instance: printer.Dummy) -> None: with pytest.raises(NotImplementedError): instance.qr("test", center=True, native=True) diff --git a/test/test_function_qr_non-native.py b/test/test_functions/test_function_qr_non-native.py similarity index 99% rename from test/test_function_qr_non-native.py rename to test/test_functions/test_function_qr_non-native.py index 04b768f..dceb747 100644 --- a/test/test_function_qr_non-native.py +++ b/test/test_functions/test_function_qr_non-native.py @@ -18,7 +18,7 @@ from PIL import Image from escpos.printer import Dummy -def test_image(): +def test_image() -> None: """Test QR as image""" instance = Dummy() image_arguments = { diff --git a/test/test_function_set.py b/test/test_functions/test_function_set.py similarity index 98% rename from test/test_function_set.py rename to test/test_functions/test_function_set.py index 93c8f5d..df4fead 100644 --- a/test/test_function_set.py +++ b/test/test_functions/test_function_set.py @@ -1,7 +1,6 @@ from typing import Optional import pytest -import six import escpos.printer as printer from escpos.constants import SET_FONT, TXT_NORMAL, TXT_SIZE, TXT_STYLE @@ -138,7 +137,7 @@ def test_set_size_custom() -> None: expected_sequence = ( TXT_SIZE, # Custom text size, no normal reset - six.int2byte(TXT_STYLE["width"][8] + TXT_STYLE["height"][7]), + bytes((TXT_STYLE["width"][8] + TXT_STYLE["height"][7],)), TXT_STYLE["flip"][False], # Flip OFF TXT_STYLE["smooth"][False], # Smooth OFF TXT_STYLE["bold"][False], # Bold OFF @@ -159,7 +158,7 @@ def test_set_size_custom_no_default(width: int, height: int) -> None: expected_sequence = ( TXT_SIZE, # Custom text size, no normal reset - six.int2byte(TXT_STYLE["width"][width] + TXT_STYLE["height"][height]), + bytes((TXT_STYLE["width"][width] + TXT_STYLE["height"][height],)), ) assert instance.output == b"".join(expected_sequence) diff --git a/test/test_function_softbarcode.py b/test/test_functions/test_function_softbarcode.py similarity index 70% rename from test/test_function_softbarcode.py rename to test/test_functions/test_function_softbarcode.py index e3da685..bffdbac 100644 --- a/test/test_function_softbarcode.py +++ b/test/test_functions/test_function_softbarcode.py @@ -11,16 +11,16 @@ def instance(): return printer.Dummy() -def test_soft_barcode_ean8_invalid(instance): +def test_soft_barcode_ean8_invalid(instance: printer.Dummy) -> None: """test with an invalid barcode""" with pytest.raises(barcode.errors.BarcodeError): instance.barcode("1234", "ean8", force_software=True) -def test_soft_barcode_ean8(instance): +def test_soft_barcode_ean8(instance: printer.Dummy) -> None: """test with a valid ean8 barcode""" instance.barcode("1234567", "ean8", force_software=True) -def test_soft_barcode_ean8_nocenter(instance): +def test_soft_barcode_ean8_nocenter(instance: printer.Dummy) -> None: instance.barcode("1234567", "ean8", align_ct=False, force_software=True) diff --git a/test/test_function_text.py b/test/test_functions/test_function_text.py similarity index 87% rename from test/test_function_text.py rename to test/test_functions/test_function_text.py index 7a72f34..13709ed 100644 --- a/test/test_function_text.py +++ b/test/test_functions/test_function_text.py @@ -15,17 +15,17 @@ from hypothesis import given from escpos.printer import Dummy -def get_printer(): +def get_printer() -> Dummy: return Dummy(magic_encode_args={"disabled": True, "encoding": "CP437"}) @given(text=st.text()) -def test_text(text: str) -> None: +def test_text(text: str): """Test that text() calls the MagicEncode object.""" instance = get_printer() - instance.magic.write = mock.Mock() - instance.text(text) - instance.magic.write.assert_called_with(text) + with mock.patch.object(instance.magic, "write") as write: + instance.text(text) + write.assert_called_with(text) def test_block_text() -> None: diff --git a/test/test_functions.py b/test/test_functions/test_functions.py similarity index 82% rename from test/test_functions.py rename to test/test_functions/test_functions.py index abef814..f364545 100644 --- a/test/test_functions.py +++ b/test/test_functions/test_functions.py @@ -3,19 +3,19 @@ import pytest from escpos.printer import Dummy -def test_line_spacing_code_gen(): +def test_line_spacing_code_gen() -> None: printer = Dummy() printer.line_spacing(10) assert printer.output == b"\x1b3\n" -def test_line_spacing_rest(): +def test_line_spacing_rest() -> None: printer = Dummy() printer.line_spacing() assert printer.output == b"\x1b2" -def test_line_spacing_error_handling(): +def test_line_spacing_error_handling() -> None: printer = Dummy() with pytest.raises(ValueError): printer.line_spacing(99, divisor=44) diff --git a/test/test_raise_arbitrary_error.py b/test/test_raise_arbitrary_error.py index 475dc02..07ad76e 100644 --- a/test/test_raise_arbitrary_error.py +++ b/test/test_raise_arbitrary_error.py @@ -23,7 +23,7 @@ def test_raise_error_wrongly(): raise escpos.Error("This should raise an AttributeError.") -def tests_raise_error(): +def tests_raise_error() -> None: """raise error the right way""" with pytest.raises(escpos.exceptions.Error): raise escpos.exceptions.Error("This should raise an error.")