Skip to content

Commit 1b2e7e1

Browse files
committed
convert "codes" and "strings" into properties of the class to prevent casual corruption.
RUF012 points out that instances of the class can casually modify an unmutable class variable, eg.: class A: value = [] def f(self,*y): self.value += [*y]
1 parent fb6e373 commit 1b2e7e1

1 file changed

Lines changed: 35 additions & 25 deletions

File tree

irods/access.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,10 @@
2222

2323

2424
class _Access_LookupMeta(type):
25-
def __getitem__(self, key):
26-
return self.codes[key]
27-
28-
def keys(self):
29-
return list(self.codes.keys())
30-
31-
def values(self):
32-
return list(self.codes[k] for k in self.codes.keys())
33-
34-
def items(self):
35-
return list(zip(self.keys(), self.values()))
36-
3725

38-
class iRODSAccess(metaclass=_Access_LookupMeta):
39-
@classmethod
40-
def to_int(cls, key):
41-
return cls.codes[key]
42-
43-
@classmethod
44-
def to_string(cls, key):
45-
return cls.strings[key]
46-
47-
# noqa: RUF012 - Cannot change in minor release
48-
codes = collections.OrderedDict(
26+
@staticmethod
27+
def _codes():
28+
return collections.OrderedDict(
4929
(key_, value_)
5030
for key_, value_ in sorted(
5131
dict(
@@ -75,10 +55,40 @@ def to_string(cls, key):
7555
if key_ in _ichmod_listed_permissions
7656
)
7757

78-
# noqa: RUF012 - Cannot change in minor release
79-
strings = collections.OrderedDict((number, string) for string, number in codes.items())
58+
@property
59+
def codes(metaclass_target): return metaclass_target._codes()
60+
61+
@property
62+
def strings(metaclass_target):
63+
return collections.OrderedDict((number, string) for string, number in
64+
metaclass_target._codes().items())
65+
66+
def __getitem__(self, key):
67+
return self.codes[key]
68+
69+
def keys(self):
70+
return list(self.codes.keys())
71+
72+
def values(self):
73+
return list(self.codes[k] for k in self.codes.keys())
74+
75+
def items(self):
76+
return list(zip(self.keys(), self.values()))
77+
78+
79+
class iRODSAccess(metaclass=_Access_LookupMeta):
80+
@classmethod
81+
def to_int(cls, key):
82+
return cls.codes[key]
83+
84+
@classmethod
85+
def to_string(cls, key):
86+
return cls.strings[key]
87+
8088

8189
def __init__(self, access_name, path, user_name="", user_zone="", user_type=None):
90+
self.codes = self.__class__.codes.copy()
91+
self.strings = self.__class__.strings.copy()
8292
self.access_name = access_name
8393
if isinstance(path, (iRODSCollection, iRODSDataObject)):
8494
self.path = path.path

0 commit comments

Comments
 (0)