From 420259dd16a678505002d134f23c21f7b7870a44 Mon Sep 17 00:00:00 2001
From: vtexier <vit@free.fr>
Date: Wed, 31 Oct 2018 11:13:16 +0100
Subject: [PATCH] issue #52 add type hinting to peer module

---
 duniterpy/documents/peer.py | 42 ++++++++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 7 deletions(-)

diff --git a/duniterpy/documents/peer.py b/duniterpy/documents/peer.py
index de7bc60f..2542544f 100644
--- a/duniterpy/documents/peer.py
+++ b/duniterpy/documents/peer.py
@@ -1,10 +1,14 @@
 import re
+from typing import TypeVar, List, Type
 
-from duniterpy.api.endpoint import endpoint
-from .document import Document
+from duniterpy.api.endpoint import endpoint, Endpoint
+from .document import Document, MalformedDocumentError
 from .block_uid import BlockUID
 from ..constants import BLOCK_HASH_REGEX, PUBKEY_REGEX
 
+# required to type hint cls in classmethod
+PeerType = TypeVar('PeerType', bound='Peer')
+
 
 class Peer(Document):
     """
@@ -35,8 +39,18 @@ class Peer(Document):
         "Endpoints": re_endpoints
     }}
 
-    def __init__(self, version, currency, pubkey, block_uid,
-                 endpoints, signature):
+    def __init__(self, version: int, currency: str, pubkey: str, block_uid: BlockUID,
+                 endpoints: List[Endpoint], signature: str) -> None:
+        """
+        Init Peer instance
+
+        :param version: Version of the document
+        :param currency: Name of the currency
+        :param pubkey: Public key of the issuer
+        :param block_uid: BlockUID instance timestamp
+        :param endpoints: List of endpoints string
+        :param signature: Signature of the document
+        """
         super().__init__(version, currency, [signature])
 
         self.pubkey = pubkey
@@ -44,7 +58,13 @@ class Peer(Document):
         self.endpoints = endpoints
 
     @classmethod
-    def from_signed_raw(cls, raw):
+    def from_signed_raw(cls: Type[PeerType], raw: str) -> PeerType:
+        """
+        Return a Peer instance from a signed raw format string
+
+        :param raw: Signed raw format string
+        :return:
+        """
         lines = raw.splitlines(True)
         n = 0
 
@@ -71,11 +91,19 @@ class Peer(Document):
             endpoints.append(endpoint(lines[n]))
             n += 1
 
-        signature = Peer.re_signature.match(lines[n]).group(1)
+        data = Peer.re_signature.match(lines[n])
+        if data is None:
+            raise MalformedDocumentError("Peer")
+        signature = data.group(1)
 
         return cls(version, currency, pubkey, block_uid, endpoints, signature)
 
-    def raw(self):
+    def raw(self) -> str:
+        """
+        Return a raw format string of the Peer document
+
+        :return:
+        """
         doc = """Version: {0}
 Type: Peer
 Currency: {1}
-- 
GitLab