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

Testing: Add type annotations and refactor didtype.py #6613

Merged
merged 1 commit into from Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 23 additions & 20 deletions lib/rucio/common/didtype.py
Expand Up @@ -16,6 +16,8 @@
DID type to represent a did and to simplify operations on it
"""

from typing import Union

from rucio.common.exception import DIDError


Expand Down Expand Up @@ -53,29 +55,30 @@ def __init__(self, *args, **kwargs):
DID('arg.scope', name='kwarg.name')
DID('arg.name', scope='kwarg.scope')
"""
self.scope = self.name = ''
self.scope: str = ''
self.name: str = ''

num_args = len(args)
num_kwargs = len(kwargs)
if (num_args + num_kwargs) > 2:
raise DIDError('Constructor takes at most 2 arguments. Given number: {}'.format(num_args + num_kwargs))

did = ''
did: Union["DID", str, tuple[str, str], list[str], dict[str, str]] = ''
if num_args == 1:
did = args[0]

if num_kwargs == 1:
if not isinstance(did, str):
if isinstance(did, str):
k, v = next(iter(kwargs.items()))
if k == 'scope':
did = (v, did)
elif k == 'name':
did = (did, v)
else:
raise DIDError('Constructor got unexpected keyword argument: {}'.format(k))
else:
raise DIDError('First argument of constructor is expected to be string type'
'when keyword argument is given. Given type: {}'.format(type(did)))

k, v = next(iter(kwargs.items()))
if k == 'scope':
did = (v, did)
elif k == 'name':
did = (did, v)
else:
raise DIDError('Constructor got unexpected keyword argument: {}'.format(k))
elif num_args == 0:
did = kwargs.get('did', kwargs)
else:
Expand Down Expand Up @@ -113,16 +116,16 @@ def __init__(self, *args, **kwargs):
if not self.is_valid_format():
raise DIDError('Object has invalid format after construction: {}'.format(str(self)))

def update_implicit_scope(self):
def update_implicit_scope(self) -> None:
"""
This method sets the scope if it is implicitly given in self.name
This method sets the scope if it is implicitly given in self.name
"""
did_parts = self.name.split(DID.IMPLICIT_SCOPE_SEPARATOR)
num_scope_parts = DID.IMPLICIT_SCOPE_TO_LEN.get(did_parts[0], 0)
if num_scope_parts > 0:
self.scope = '.'.join(did_parts[0:num_scope_parts])

def is_valid_format(self):
def is_valid_format(self) -> bool:
"""
Method to check if the stored DID has a valid format
:return: bool
Expand All @@ -131,21 +134,21 @@ def is_valid_format(self):
return False
return True

def has_scope(self):
def has_scope(self) -> bool:
"""
Method to check if the scope part was set
:return: bool
"""
return len(self.scope) > 0

def has_name(self):
def has_name(self) -> bool:
"""
Method to check if the name part was set
:return: bool
"""
return len(self.name) > 0

def __str__(self):
def __str__(self) -> str:
"""
Creates the string representation of self
:return: string
Expand All @@ -156,7 +159,7 @@ def __str__(self):
return self.scope
return self.name

def __eq__(self, other):
def __eq__(self, other: Union[str, "DID"]) -> bool:
"""
Equality comparison with another object
:return: bool
Expand All @@ -171,14 +174,14 @@ def __eq__(self, other):

return self.scope == other.scope and self.name == other.name

def __ne__(self, other):
def __ne__(self, other: Union[str, "DID"]) -> bool:
"""
Inequality comparison with another object
:return: bool
"""
return not self.__eq__(other)

def __hash__(self):
def __hash__(self) -> int:
"""
Uses the string representation of self to create a hash
:return: int
Expand Down