Commit b80cd4d7 authored by Yuta Okamoto's avatar Yuta Okamoto Committed by oroulet

add type hints to struct_from_binary()

parent 4cfe6969
...@@ -681,7 +681,7 @@ class Client: ...@@ -681,7 +681,7 @@ class Client:
def get_server_node(self): def get_server_node(self):
return self.get_node(ua.FourByteNodeId(ua.ObjectIds.Server)) return self.get_node(ua.FourByteNodeId(ua.ObjectIds.Server))
def get_node(self, nodeid: Union[ua.NodeId, str]) -> Node: def get_node(self, nodeid: Union[Node, ua.NodeId, str, bytes, int]) -> Node:
""" """
Get node using NodeId object or a string representing a NodeId. Get node using NodeId object or a string representing a NodeId.
""" """
......
...@@ -46,7 +46,7 @@ class Node: ...@@ -46,7 +46,7 @@ class Node:
OPC-UA protocol. Feel free to look at the code of this class and call OPC-UA protocol. Feel free to look at the code of this class and call
directly UA services methods to optimize your code directly UA services methods to optimize your code
""" """
def __init__(self, session: AbstractSession, nodeid: Union[ua.NodeId, str, int]): def __init__(self, session: AbstractSession, nodeid: Union["Node", ua.NodeId, str, bytes, int]):
self.session = session self.session = session
self.nodeid: ua.NodeId self.nodeid: ua.NodeId
if isinstance(nodeid, Node): if isinstance(nodeid, Node):
......
...@@ -205,6 +205,7 @@ class _SubHandler: ...@@ -205,6 +205,7 @@ class _SubHandler:
def status_change_notification(self, status: ua.StatusChangeNotification): def status_change_notification(self, status: ua.StatusChangeNotification):
self.sync_handler.status_change_notification(status) self.sync_handler.status_change_notification(status)
class Client: class Client:
def __init__(self, url: str, timeout: int = 4, tloop=None): def __init__(self, url: str, timeout: int = 4, tloop=None):
self.tloop = tloop self.tloop = tloop
...@@ -291,8 +292,9 @@ class Client: ...@@ -291,8 +292,9 @@ class Client:
def get_namespace_index(self, url): def get_namespace_index(self, url):
pass pass
def get_node(self, nodeid): def get_node(self, nodeid: Union["SyncNode", ua.NodeId, str, bytes, int]):
return SyncNode(self.tloop, self.aio_obj.get_node(nodeid)) aio_nodeid = nodeid.aio_obj if isinstance(nodeid, SyncNode) else nodeid
return SyncNode(self.tloop, self.aio_obj.get_node(aio_nodeid))
def get_root_node(self): def get_root_node(self):
return SyncNode(self.tloop, self.aio_obj.get_root_node()) return SyncNode(self.tloop, self.aio_obj.get_root_node())
...@@ -437,7 +439,6 @@ class Server: ...@@ -437,7 +439,6 @@ class Server:
return Subscription(self.tloop, aio_sub) return Subscription(self.tloop, aio_sub)
class EventGenerator: class EventGenerator:
def __init__(self, tloop, aio_evgen): def __init__(self, tloop, aio_evgen):
self.aio_obj = aio_evgen self.aio_obj = aio_evgen
...@@ -459,7 +460,7 @@ def new_node(sync_node, nodeid): ...@@ -459,7 +460,7 @@ def new_node(sync_node, nodeid):
class SyncNode: class SyncNode:
def __init__(self, tloop, aio_node): def __init__(self, tloop: ThreadLoop, aio_node: node.Node):
self.aio_obj = aio_node self.aio_obj = aio_node
self.tloop = tloop self.tloop = tloop
......
...@@ -5,7 +5,7 @@ Binary protocol specific functions and constants ...@@ -5,7 +5,7 @@ Binary protocol specific functions and constants
import functools import functools
import struct import struct
import logging import logging
from typing import Any, Callable from typing import IO, Any, Callable, Optional, Sequence, Type, TypeVar, Union
import typing import typing
import uuid import uuid
from enum import Enum, IntFlag from enum import Enum, IntFlag
...@@ -17,6 +17,8 @@ from .uatypes import type_from_optional, type_is_list, type_is_union, type_from_ ...@@ -17,6 +17,8 @@ from .uatypes import type_from_optional, type_is_list, type_is_union, type_from_
logger = logging.getLogger('__name__') logger = logging.getLogger('__name__')
T = TypeVar('T')
def test_bit(data, offset): def test_bit(data, offset):
mask = 1 << offset mask = 1 << offset
...@@ -373,7 +375,7 @@ def to_binary(uatype, val): ...@@ -373,7 +375,7 @@ def to_binary(uatype, val):
@functools.lru_cache(maxsize=None) @functools.lru_cache(maxsize=None)
def create_list_serializer(uatype, recursive: bool = False) -> Callable[[Any], bytes]: def create_list_serializer(uatype, recursive: bool = False) -> Callable[[Optional[Sequence[Any]]], bytes]:
""" """
Given a type, return a function that takes a list of instances Given a type, return a function that takes a list of instances
of that type and serializes it. of that type and serializes it.
...@@ -391,6 +393,7 @@ def create_list_serializer(uatype, recursive: bool = False) -> Callable[[Any], b ...@@ -391,6 +393,7 @@ def create_list_serializer(uatype, recursive: bool = False) -> Callable[[Any], b
return recursive_serialize return recursive_serialize
type_serializer = create_type_serializer(uatype) type_serializer = create_type_serializer(uatype)
def serialize(val): def serialize(val):
if val is None: if val is None:
return none_val return none_val
...@@ -686,7 +689,7 @@ def _create_dataclass_deserializer(objtype): ...@@ -686,7 +689,7 @@ def _create_dataclass_deserializer(objtype):
return decode return decode
def struct_from_binary(objtype, data): def struct_from_binary(objtype: Union[Type[T], str], data: IO) -> T:
""" """
unpack an ua struct. Arguments are an objtype as Python dataclass or string unpack an ua struct. Arguments are an objtype as Python dataclass or string
""" """
......
...@@ -915,7 +915,7 @@ class Variant: ...@@ -915,7 +915,7 @@ class Variant:
# FIXME: typing is wrong here # FIXME: typing is wrong here
Value: Any = None Value: Any = None
VariantType: VariantType = None VariantType: "VariantType" = None
Dimensions: Optional[List[Int32]] = None Dimensions: Optional[List[Int32]] = None
is_array: Optional[bool] = None is_array: Optional[bool] = None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment