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)
This commit is contained in:
Patrick Kanzler 2023-12-16 23:09:20 +01:00 committed by GitHub
parent 66a2e78e16
commit 0c824cf295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 40 additions and 41 deletions

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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):

View File

@ -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)

View File

@ -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()

View File

@ -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)
"""

View File

@ -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)

View File

@ -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)

View File

@ -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 = {

View File

@ -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)

View File

@ -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)

View File

@ -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:

View File

@ -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)

View File

@ -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.")