Skip to content

Commit

Permalink
Fixed some typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
krister-ts committed Sep 22, 2023
1 parent 284b48f commit 7c45608
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions ftd2xx_aio/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
"""Asyncio transports for FTDI D2XX devices."""
from abc import abstractmethod
from typing import Optional, Union
import asyncio
from ftd2xx import FTD2XX, DeviceError
from ftd2xx.defines import ModemStatus
Expand All @@ -26,7 +27,7 @@ class FTD2xxBaseTransport(asyncio.BaseTransport):
def __init__(
self,
loop: asyncio.BaseEventLoop,
protocol: FTD2xxProtocol,
protocol: Union[asyncio.Protocol, FTD2xxProtocol],
ftd2xx_instance: FTD2XX,
):
super().__init__()
Expand Down Expand Up @@ -79,7 +80,7 @@ def close(self):
if not self._closing:
self._close(None)

def set_protocol(self, protocol):
def set_protocol(self, protocol: asyncio.Protocol):
# TODO: Is there a graceful procedure for setting protocol?
self._protocol = protocol

Expand Down Expand Up @@ -155,9 +156,9 @@ def _call_connection_lost(self, exc):

def _post_connection_lost(self):
self._ftd2xx.close()
self._ftd2xx = None
self._protocol = None
self._loop = None
# self._ftd2xx = None
# self._protocol = None
# self._loop = None


class AbstractFTD2xxReadTransport(FTD2xxBaseTransport, asyncio.ReadTransport):
Expand All @@ -166,8 +167,8 @@ class AbstractFTD2xxReadTransport(FTD2xxBaseTransport, asyncio.ReadTransport):
def __init__(self, loop, protocol_factory, ftd2xx_instance):
super().__init__(loop, protocol_factory, ftd2xx_instance)
self._max_read_size = 1024
self._has_reader = False
self._has_modem = False
self._has_reader: Optional[asyncio.Handle] = None
self._has_modem: Optional[asyncio.Handle] = None
self._is_reading = False
self._modem = ModemStatus(0)
loop.call_soon(self._ensure_reader)
Expand Down Expand Up @@ -226,7 +227,11 @@ def _poll_read(self):
self._fatal_error(exc, "Fatal read error on ftd2xx transport")

def _poll_modem(self):
if self._has_reader and not self._closing:
if (
isinstance(self._protocol, FTD2xxProtocol)
and self._has_reader
and not self._closing
):
self._loop.call_later(self._poll_wait_time, self._poll_modem)
try:
modem = self._ftd2xx.getModemStatus()
Expand All @@ -240,16 +245,16 @@ def _ensure_reader(self):
if not self._closing:
if not self._has_reader:
self._has_reader = self._loop.call_soon(self._poll_read)
if not self._has_modem:
if isinstance(self._protocol, FTD2xxProtocol) and not self._has_modem:
self._has_modem = self._loop.call_soon(self._poll_modem)

def _remove_reader(self):
if self._has_reader:
self._has_reader.cancel()
self._has_reader = False
self._has_reader = None
if self._has_modem:
self._has_modem.cancel()
self._has_modem = False
self._has_modem = None


class AbstractFTD2xxWriteTransport(FTD2xxBaseTransport, asyncio.WriteTransport):
Expand All @@ -259,7 +264,7 @@ def __init__(self, loop, protocol_factory, ftd2xx_instance):
super().__init__(loop, protocol_factory, ftd2xx_instance)
self._write_buffer = []
self._protocol_paused = False
self._has_writer = False
self._has_writer: Optional[asyncio.Handle] = None
self._max_out_waiting = 1024
self._set_write_buffer_limits()

Expand Down Expand Up @@ -457,7 +462,7 @@ def _ensure_writer(self):
def _remove_writer(self):
if self._has_writer:
self._has_writer.cancel()
self._has_writer = False
self._has_writer = None


class FTD2xxReadTransport(AbstractFTD2xxReadTransport):
Expand Down

0 comments on commit 7c45608

Please sign in to comment.