1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-12-02 09:43:30 +00:00

Fix type checking - per device type annotations

This commit is contained in:
belono
2023-10-08 21:16:23 +02:00
parent 67c0ab03ba
commit 3f775fbaa8
7 changed files with 46 additions and 20 deletions

View File

@@ -10,7 +10,7 @@
import logging
import socket
from typing import Union
from typing import Literal, Optional, Union
from ..escpos import Escpos
from ..exceptions import DeviceNotFoundError
@@ -48,6 +48,8 @@ class Network(Escpos):
"""
_device: Union[Literal[False], Literal[None], socket.socket] = False
@staticmethod
def is_usable() -> bool:
"""Indicate whether this printer class is usable.
@@ -91,7 +93,9 @@ class Network(Escpos):
try:
# Open device
self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.device: Optional[socket.socket] = socket.socket(
socket.AF_INET, socket.SOCK_STREAM
)
self.device.settimeout(self.timeout)
self.device.connect((self.host, self.port))
except OSError as e:
@@ -124,8 +128,8 @@ class Network(Escpos):
return
logging.info("Closing Network connection to printer %s", self.host)
try:
self.device.shutdown(socket.SHUT_RDWR)
self._device.shutdown(socket.SHUT_RDWR)
except socket.error:
pass
self.device.close()
self._device.close()
self._device = False