Skip to content
Snippets Groups Projects
Commit 7a09e5b8 authored by Vincent Texier's avatar Vincent Texier
Browse files

issue #52 use mypy to check static typing

Update README with make check command
parent 120e7baa
No related branches found
No related tags found
No related merge requests found
Pipeline #2713 passed
.PHONY: docs tests .PHONY: docs tests check
# generate documentation # generate documentation
docs: docs:
...@@ -8,3 +8,6 @@ docs: ...@@ -8,3 +8,6 @@ docs:
tests: tests:
python -m unittest python -m unittest
# check static typing
check:
mypy duniterpy --ignore-missing-imports
...@@ -44,15 +44,21 @@ Development ...@@ -44,15 +44,21 @@ Development
pip install -r requirements.txt pip install -r requirements.txt
* Add PYTHONPATH env var to your shell containing the path to this repository * Add PYTHONPATH env var to your shell containing the path to this repository
* Run unit tests with::
make tests
* Take a look at examples folder * Take a look at examples folder
* Run examples from parent folder:: * Run examples from parent folder::
python examples/request_data.py python examples/request_data.py
* Before submit a merge requests, please check the static typing and tests.
* Check static typing with `mypy <http://mypy-lang.org/>`_::
make check
* Run unit tests with::
make tests
Documentation Documentation
------------- -------------
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# vit # vit
import json import json
import logging import logging
from typing import Callable, Union from typing import Callable, Union, Any
import aiohttp import aiohttp
import jsonschema import jsonschema
...@@ -34,7 +34,7 @@ ERROR_SCHEMA = { ...@@ -34,7 +34,7 @@ ERROR_SCHEMA = {
} }
def parse_text(text: str, schema: dict) -> any: def parse_text(text: str, schema: dict) -> Any:
""" """
Validate and parse the BMA answer from websocket Validate and parse the BMA answer from websocket
...@@ -51,7 +51,7 @@ def parse_text(text: str, schema: dict) -> any: ...@@ -51,7 +51,7 @@ def parse_text(text: str, schema: dict) -> any:
return data return data
def parse_error(text: str) -> any: def parse_error(text: str) -> Any:
""" """
Validate and parse the BMA answer from websocket Validate and parse the BMA answer from websocket
...@@ -67,7 +67,7 @@ def parse_error(text: str) -> any: ...@@ -67,7 +67,7 @@ def parse_error(text: str) -> any:
return data return data
async def parse_response(response: aiohttp.ClientResponse, schema: dict) -> any: async def parse_response(response: aiohttp.ClientResponse, schema: dict) -> Any:
""" """
Validate and parse the BMA answer Validate and parse the BMA answer
...@@ -91,7 +91,7 @@ class API(object): ...@@ -91,7 +91,7 @@ class API(object):
""" """
schema = {} schema = {}
def __init__(self, connection_handler: endpoint.ConnectionHandler, module: str): def __init__(self, connection_handler: endpoint.ConnectionHandler, module: str) -> None:
""" """
Asks a module in order to create the url used then by derivated classes. Asks a module in order to create the url used then by derivated classes.
...@@ -189,7 +189,7 @@ class Client: ...@@ -189,7 +189,7 @@ class Client:
""" """
def __init__(self, _endpoint: Union[str, endpoint.Endpoint], session: aiohttp.ClientSession = None, def __init__(self, _endpoint: Union[str, endpoint.Endpoint], session: aiohttp.ClientSession = None,
proxy: str = None): proxy: str = None) -> None:
""" """
Init Client instance Init Client instance
...@@ -214,7 +214,7 @@ class Client: ...@@ -214,7 +214,7 @@ class Client:
self.session = session self.session = session
self.proxy = proxy self.proxy = proxy
async def get(self, url_path: str, params: dict = None, rtype: str = RESPONSE_JSON, schema: dict = None) -> any: async def get(self, url_path: str, params: dict = None, rtype: str = RESPONSE_JSON, schema: dict = None) -> Any:
""" """
GET request on self.endpoint + url_path GET request on self.endpoint + url_path
...@@ -245,7 +245,7 @@ class Client: ...@@ -245,7 +245,7 @@ class Client:
elif rtype == RESPONSE_JSON: elif rtype == RESPONSE_JSON:
return await response.json() return await response.json()
async def post(self, url_path: str, params: dict = None, rtype: str = RESPONSE_JSON, schema: dict = None) -> any: async def post(self, url_path: str, params: dict = None, rtype: str = RESPONSE_JSON, schema: dict = None) -> Any:
""" """
POST request on self.endpoint + url_path POST request on self.endpoint + url_path
...@@ -294,7 +294,7 @@ class Client: ...@@ -294,7 +294,7 @@ class Client:
""" """
await self.session.close() await self.session.close()
def __call__(self, _function: Callable, *args: any, **kwargs: any) -> any: def __call__(self, _function: Callable, *args: Any, **kwargs: Any) -> Any:
""" """
Call the _function given with the args given Call the _function given with the args given
So we can call many packages wrapping the REST API So we can call many packages wrapping the REST API
......
import re import re
from typing import Any, Optional
import aiohttp import aiohttp
...@@ -9,8 +10,8 @@ from ..documents import MalformedDocumentError ...@@ -9,8 +10,8 @@ from ..documents import MalformedDocumentError
class ConnectionHandler: class ConnectionHandler:
"""Helper class used by other API classes to ease passing server connection information.""" """Helper class used by other API classes to ease passing server connection information."""
def __init__(self, http_scheme: str, ws_scheme: str, server: str, port: int, path: str = "", proxy: str = None, def __init__(self, http_scheme: str, ws_scheme: str, server: str, port: int, path: str,
session: aiohttp.ClientSession = None): session: aiohttp.ClientSession, proxy: Optional[str] = None) -> None:
""" """
Init instance of connection handler Init instance of connection handler
...@@ -18,9 +19,9 @@ class ConnectionHandler: ...@@ -18,9 +19,9 @@ class ConnectionHandler:
:param ws_scheme: Web socket scheme :param ws_scheme: Web socket scheme
:param server: Server IP or domain name :param server: Server IP or domain name
:param port: Port number :param port: Port number
:param port: Url path (optional, default="") :param port: Url path
:param session: Session AIOHTTP
:param proxy: Proxy (optional, default=None) :param proxy: Proxy (optional, default=None)
:param session: Session AIOHTTP (optional, default=None)
""" """
self.http_scheme = http_scheme self.http_scheme = http_scheme
self.ws_scheme = ws_scheme self.ws_scheme = ws_scheme
...@@ -48,14 +49,14 @@ class Endpoint: ...@@ -48,14 +49,14 @@ class Endpoint:
def __str__(self) -> str: def __str__(self) -> str:
raise NotImplementedError("__str__ is not implemented") raise NotImplementedError("__str__ is not implemented")
def __eq__(self, other: any) -> bool: def __eq__(self, other: Any) -> bool:
raise NotImplementedError("__eq__ is not implemented") raise NotImplementedError("__eq__ is not implemented")
class UnknownEndpoint(Endpoint): class UnknownEndpoint(Endpoint):
API = None API = None
def __init__(self, api: str, properties: list): def __init__(self, api: str, properties: list) -> None:
self.api = api self.api = api
self.properties = properties self.properties = properties
...@@ -86,12 +87,12 @@ class UnknownEndpoint(Endpoint): ...@@ -86,12 +87,12 @@ class UnknownEndpoint(Endpoint):
return doc return doc
def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler: def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler:
return ConnectionHandler("", "", "", 0, "") return ConnectionHandler("", "", "", 0, "", aiohttp.ClientSession())
def __str__(self) -> str: def __str__(self) -> str:
return "{0} {1}".format(self.api, ' '.join(["{0}".format(p) for p in self.properties])) return "{0} {1}".format(self.api, ' '.join(["{0}".format(p) for p in self.properties]))
def __eq__(self, other: any) -> bool: def __eq__(self, other: Any) -> bool:
if isinstance(other, UnknownEndpoint): if isinstance(other, UnknownEndpoint):
return self.api == other.api and self.properties == other.properties return self.api == other.api and self.properties == other.properties
else: else:
...@@ -123,7 +124,7 @@ class BMAEndpoint(Endpoint): ...@@ -123,7 +124,7 @@ class BMAEndpoint(Endpoint):
ipv4_regex=IPV4_REGEX, ipv4_regex=IPV4_REGEX,
ipv6_regex=IPV6_REGEX)) ipv6_regex=IPV6_REGEX))
def __init__(self, server: str, ipv4: str, ipv6: str, port: int): def __init__(self, server: str, ipv4: str, ipv6: str, port: int) -> None:
""" """
Init BMAEndpoint instance Init BMAEndpoint instance
...@@ -175,11 +176,11 @@ class BMAEndpoint(Endpoint): ...@@ -175,11 +176,11 @@ class BMAEndpoint(Endpoint):
:return: :return:
""" """
if self.server: if self.server:
return ConnectionHandler("http", "ws", self.server, self.port, "", proxy, session) return ConnectionHandler("http", "ws", self.server, self.port, "", session, proxy)
elif self.ipv6: elif self.ipv6:
return ConnectionHandler("http", "ws", "[{0}]".format(self.ipv6), self.port, "", proxy, session) return ConnectionHandler("http", "ws", "[{0}]".format(self.ipv6), self.port, "", session, proxy)
return ConnectionHandler("http", "ws", self.ipv4, self.port, "", proxy, session) return ConnectionHandler("http", "ws", self.ipv4, self.port, "", session, proxy)
def __str__(self): def __str__(self):
return self.inline() return self.inline()
...@@ -204,7 +205,7 @@ class SecuredBMAEndpoint(BMAEndpoint): ...@@ -204,7 +205,7 @@ class SecuredBMAEndpoint(BMAEndpoint):
ipv6_regex=IPV6_REGEX, ipv6_regex=IPV6_REGEX,
path_regex=PATH_REGEX)) path_regex=PATH_REGEX))
def __init__(self, server: str, ipv4: str, ipv6: str, port: int, path: str): def __init__(self, server: str, ipv4: str, ipv6: str, port: int, path: str) -> None:
""" """
Init SecuredBMAEndpoint instance Init SecuredBMAEndpoint instance
...@@ -255,11 +256,11 @@ class SecuredBMAEndpoint(BMAEndpoint): ...@@ -255,11 +256,11 @@ class SecuredBMAEndpoint(BMAEndpoint):
:return: :return:
""" """
if self.server: if self.server:
return ConnectionHandler("https", "wss", self.server, self.port, self.path, proxy, session) return ConnectionHandler("https", "wss", self.server, self.port, self.path, session, proxy)
elif self.ipv6: elif self.ipv6:
return ConnectionHandler("https", "wss", "[{0}]".format(self.ipv6), self.port, self.path, proxy, session) return ConnectionHandler("https", "wss", "[{0}]".format(self.ipv6), self.port, self.path, session, proxy)
return ConnectionHandler("https", "wss", self.ipv4, self.port, self.path, proxy, session) return ConnectionHandler("https", "wss", self.ipv4, self.port, self.path, session, proxy)
class WS2PEndpoint(Endpoint): class WS2PEndpoint(Endpoint):
...@@ -303,7 +304,7 @@ class WS2PEndpoint(Endpoint): ...@@ -303,7 +304,7 @@ class WS2PEndpoint(Endpoint):
:param str proxy: Proxy url :param str proxy: Proxy url
:rtype: ConnectionHandler :rtype: ConnectionHandler
""" """
return ConnectionHandler("https", "wss", self.server, self.port, self.path, proxy, session) return ConnectionHandler("https", "wss", self.server, self.port, self.path, session, proxy)
def __str__(self): def __str__(self):
return self.inline() return self.inline()
...@@ -350,7 +351,7 @@ class ESCoreEndpoint(Endpoint): ...@@ -350,7 +351,7 @@ class ESCoreEndpoint(Endpoint):
:param str proxy: Proxy url :param str proxy: Proxy url
:rtype: ConnectionHandler :rtype: ConnectionHandler
""" """
return ConnectionHandler("https", "wss", self.server, self.port, "", proxy, session) return ConnectionHandler("https", "wss", self.server, self.port, "", session, proxy)
def __str__(self): def __str__(self):
return self.inline() return self.inline()
...@@ -396,7 +397,7 @@ class ESUserEndpoint(Endpoint): ...@@ -396,7 +397,7 @@ class ESUserEndpoint(Endpoint):
:param str proxy: Proxy url :param str proxy: Proxy url
:rtype: ConnectionHandler :rtype: ConnectionHandler
""" """
return ConnectionHandler("https", "wss", self.server, self.port, "", proxy, session) return ConnectionHandler("https", "wss", self.server, self.port, "", session, proxy)
def __str__(self): def __str__(self):
return self.inline() return self.inline()
...@@ -442,7 +443,7 @@ class ESSubscribtionEndpoint(Endpoint): ...@@ -442,7 +443,7 @@ class ESSubscribtionEndpoint(Endpoint):
:param str proxy: Proxy url :param str proxy: Proxy url
:rtype: ConnectionHandler :rtype: ConnectionHandler
""" """
return ConnectionHandler("https", "wss", self.server, self.port, "", proxy, session) return ConnectionHandler("https", "wss", self.server, self.port, "", session, proxy)
def __str__(self): def __str__(self):
return self.inline() return self.inline()
......
sphinx sphinx
mypy
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment