From c6600cfc8d24db51dfa722e950be1a121a8b5ffa Mon Sep 17 00:00:00 2001 From: Akhil G <46602926+akhilguruprasad22@users.noreply.github.com> Date: Wed, 20 Mar 2024 21:17:25 +0530 Subject: [PATCH] add __int__, __hash__, __eq__ methods and missing annotations to Token, Local, and Argument classes (#103) * add __int__, __hash__, __eq__ methods and missing annotations to Token, Local, and Argument classes * Removal of redundant cast. Lint checked. --- dncil/clr/argument.py | 9 +++++++++ dncil/clr/local.py | 9 +++++++++ dncil/clr/token.py | 37 ++++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/dncil/clr/argument.py b/dncil/clr/argument.py index bc367ac..5f1bbd2 100644 --- a/dncil/clr/argument.py +++ b/dncil/clr/argument.py @@ -20,3 +20,12 @@ def __str__(self) -> str: def __repr__(self) -> str: return str(self) + + def __int__(self) -> int: + return self.index + + def __eq__(self, other: object) -> bool: + return isinstance(other, Argument) and self.index == other.index + + def __hash__(self) -> int: + return hash(self.index) diff --git a/dncil/clr/local.py b/dncil/clr/local.py index 9595ed1..a061a86 100644 --- a/dncil/clr/local.py +++ b/dncil/clr/local.py @@ -20,3 +20,12 @@ def __str__(self) -> str: def __repr__(self) -> str: return str(self) + + def __int__(self) -> int: + return self.index + + def __eq__(self, other: object) -> bool: + return isinstance(other, Local) and self.index == other.index + + def __hash__(self) -> int: + return hash(self.index) diff --git a/dncil/clr/token.py b/dncil/clr/token.py index 99eeb12..7a4fdc3 100644 --- a/dncil/clr/token.py +++ b/dncil/clr/token.py @@ -6,49 +6,60 @@ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. +from __future__ import annotations + class Token(object): """store managed token""" - RID_MASK = 0x00FFFFFF - RID_MAX = RID_MASK - TABLE_SHIFT = 24 + RID_MASK: int = 0x00FFFFFF + RID_MAX: int = RID_MASK + TABLE_SHIFT: int = 24 - def __init__(self, value): - self.value = value + def __init__(self, value: int): + self.value: int = value @property - def rid(self): + def rid(self) -> int: """get token row index""" return self.value & Token.RID_MASK @property - def table(self): + def table(self) -> int: """get token table index""" return self.value >> Token.TABLE_SHIFT - def __str__(self): + def __str__(self) -> str: return "token(0x%08X)" % self.value - def __repr__(self): + def __repr__(self) -> str: return str(self) + def __int__(self) -> int: + return self.value + + def __eq__(self, other: object) -> bool: + return isinstance(other, Token) and self.value == other.value + + def __hash__(self) -> int: + return hash(self.value) + class InvalidToken(Token): """store invalid managed token""" - def __init__(self, value): + def __init__(self, value: int): super(InvalidToken, self).__init__(value) - def __str__(self): + def __str__(self) -> str: return "invalid token(0x%08X)" % self.value class StringToken(Token): """store string managed token""" - def __init__(self, value): + def __init__(self, value: int): super(StringToken, self).__init__(value) - def __str__(self): + def __str__(self) -> str: return "string token(0x%08X)" % self.value