]> kaliko git repositories - python-musicpd.git/blobdiff - musicpd.py
Some more type hints
[python-musicpd.git] / musicpd.py
index be9b8337fa6b55a87426ff69a613d3691c9da63a..c083206b6e018f709da97e5507f969c520302cc9 100644 (file)
@@ -13,7 +13,7 @@ import os
 from functools import wraps
 # Type hint for python <= 3.8
 from typing import Any, Dict, List, Tuple
-from typing import Literal, Optional, Union
+from typing import Iterator, Optional, Union
 
 HELLO_PREFIX = "OK MPD "
 ERROR_PREFIX = "ACK "
@@ -155,6 +155,8 @@ class MPDClient:
         #: Current connection timeout value, defaults to
         #: :py:obj:`CONNECTION_TIMEOUT` or env. var. ``MPD_TIMEOUT`` if provided
         self.mpd_timeout: Union[None,int] = None
+        #: Protocol version as exposed by the server
+        self.mpd_version: str = ''
         self._reset()
         self._commands = {
             # Status Commands
@@ -392,7 +394,7 @@ class MPDClient:
         self._wfile.write(f"{line!s}\n")
         self._wfile.flush()
 
-    def _write_command(self, command, args=None):
+    def _write_command(self, command, args: Optional[list[str]] = None):
         if args is None:
             args = []
         parts = [command]
@@ -405,7 +407,7 @@ class MPDClient:
             raise CommandError('new line found in the command!')
         self._write_line(" ".join(parts))
 
-    def _read_binary(self, amount):
+    def _read_binary(self, amount: int):
         chunk = bytearray()
         while amount > 0:
             result = self._rbfile.read(amount)
@@ -416,7 +418,7 @@ class MPDClient:
             amount -= len(result)
         return bytes(chunk)
 
-    def _read_line(self, binary: Literal[True,False] = False):
+    def _read_line(self, binary: bool = False):
         if binary:
             line = self._rbfile.readline().decode('utf-8')
         else:
@@ -437,7 +439,7 @@ class MPDClient:
             return None
         return line
 
-    def _read_pair(self, separator: str, binary: Literal[True,False] = False):
+    def _read_pair(self, separator: str, binary: bool = False):
         line = self._read_line(binary=binary)
         if line is None:
             return None
@@ -446,7 +448,7 @@ class MPDClient:
             raise ProtocolError(f"Could not parse pair: '{line}'")
         return pair
 
-    def _read_pairs(self, separator=": ", binary: Literal[True,False] =False):
+    def _read_pairs(self, separator: str =": ", binary: bool =False):
         pair = self._read_pair(separator, binary=binary)
         while pair:
             yield pair
@@ -465,7 +467,7 @@ class MPDClient:
         for _, value in self._read_pairs(":"):
             yield value
 
-    def _read_objects(self, delimiters: Optional[List[str]] = None):
+    def _read_objects(self, delimiters: Optional[List[str]] = None) -> Iterator[Dict]:
         obj: Dict[str,Any] = {}
         if delimiters is None:
             delimiters = []
@@ -512,14 +514,14 @@ class MPDClient:
     def _fetch_playlist(self):
         return self._read_playlist()
 
-    def _fetch_object(self):
+    def _fetch_object(self) -> Dict:
         objs = list(self._read_objects())
         if not objs:
             return {}
         return objs[0]
 
     @iterator_wrapper
-    def _fetch_objects(self, delimiters):
+    def _fetch_objects(self, delimiters: List[str]):
         return self._read_objects(delimiters)
 
     def _fetch_changes(self):
@@ -589,7 +591,7 @@ class MPDClient:
         self.mpd_version = line[len(HELLO_PREFIX):].strip()
 
     def _reset(self):
-        self.mpd_version = None
+        self.mpd_version = ''
         self._iterating = False
         self._pending = []
         self._command_list = None