Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implementation to ua.Boolean, such that it can be instantiated by ua.Boolean(val) #1715

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions asyncua/ua/uatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from datetime import datetime, timedelta, timezone
from enum import IntEnum
from typing import Any, Generic, List, Optional, Union
from typing_extensions import SupportsIndex, SupportsInt

# hack to support python < 3.8
if sys.version_info.minor < 10:
Expand Down Expand Up @@ -52,15 +53,15 @@ def get_args(tp):
MAX_INT64 = 2**63 - 1


def type_is_union(uatype):
def type_is_union(uatype) -> bool:
return get_origin(uatype) == Union


def type_is_list(uatype):
def type_is_list(uatype) -> bool:
return get_origin(uatype) is list


def type_allow_subclass(uatype):
def type_allow_subclass(uatype) -> bool:
return get_origin(uatype) not in [Union, list, None]


Expand Down Expand Up @@ -166,8 +167,42 @@ class UInt64(int):


class Boolean: # Boolean(bool) is not supported in Python
pass
value: bool

def __init__(self, value: Union[str, SupportsInt, SupportsIndex]) -> None:
if isinstance(value, bool):
self.value = value
else:
self.value = bool(int(value))

def __bool__(self) -> bool:
return self.value

def __repr__(self) -> str:
return f"Boolean({self.value})"

def __eq__(self, other: object) -> bool:
if isinstance(other, Boolean):
return self.value == other.value
return self.value == other

def __ne__(self, other: object) -> bool:
return not self.__eq__(other)

def __and__(self, other: "Boolean") -> "Boolean":
return Boolean(self.value and bool(other))

def __or__(self, other: "Boolean") -> "Boolean":
return Boolean(self.value or bool(other))

def __xor__(self, other: "Boolean") -> "Boolean":
return Boolean(self.value ^ bool(other))

def __int__(self) -> int:
return int(self.value)

def __str__(self) -> str:
return str(self.value)

class Double(float):
pass
Expand Down
Loading