chore(inplacevar): lint

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
This commit is contained in:
Akhil Narang 2025-10-03 00:10:53 +05:30
parent c02a3bf5ce
commit bd60a0a156
No known key found for this signature in database
GPG key ID: 9DCC61E211BF645F

View file

@ -18,110 +18,107 @@ valid_inplace_types = (list, set)
inplace_slots = { inplace_slots = {
'+=': '__iadd__', "+=": "__iadd__",
'-=': '__isub__', "-=": "__isub__",
'*=': '__imul__', "*=": "__imul__",
'/=': (1 / 2 == 0) and '__idiv__' or '__itruediv__', "/=": ((1 / 2 == 0) and "__idiv__") or "__itruediv__",
'//=': '__ifloordiv__', "//=": "__ifloordiv__",
'%=': '__imod__', "%=": "__imod__",
'**=': '__ipow__', "**=": "__ipow__",
'<<=': '__ilshift__', "<<=": "__ilshift__",
'>>=': '__irshift__', ">>=": "__irshift__",
'&=': '__iand__', "&=": "__iand__",
'^=': '__ixor__', "^=": "__ixor__",
'|=': '__ior__', "|=": "__ior__",
} }
def __iadd__(x, y): def __iadd__(x, y):
x += y x += y
return x return x
def __isub__(x, y): def __isub__(x, y):
x -= y x -= y
return x return x
def __imul__(x, y): def __imul__(x, y):
x *= y x *= y
return x return x
def __idiv__(x, y): def __idiv__(x, y):
x /= y x /= y
return x return x
def __ifloordiv__(x, y): def __ifloordiv__(x, y):
x //= y x //= y
return x return x
def __imod__(x, y): def __imod__(x, y):
x %= y x %= y
return x return x
def __ipow__(x, y): def __ipow__(x, y):
x **= y x **= y
return x return x
def __ilshift__(x, y): def __ilshift__(x, y):
x <<= y x <<= y
return x return x
def __irshift__(x, y): def __irshift__(x, y):
x >>= y x >>= y
return x return x
def __iand__(x, y): def __iand__(x, y):
x &= y x &= y
return x return x
def __ixor__(x, y): def __ixor__(x, y):
x ^= y x ^= y
return x return x
def __ior__(x, y): def __ior__(x, y):
x |= y x |= y
return x return x
inplace_ops = { inplace_ops = {
'+=': __iadd__, "+=": __iadd__,
'-=': __isub__, "-=": __isub__,
'*=': __imul__, "*=": __imul__,
'/=': __idiv__, "/=": __idiv__,
'//=': __ifloordiv__, "//=": __ifloordiv__,
'%=': __imod__, "%=": __imod__,
'**=': __ipow__, "**=": __ipow__,
'<<=': __ilshift__, "<<=": __ilshift__,
'>>=': __irshift__, ">>=": __irshift__,
'&=': __iand__, "&=": __iand__,
'^=': __ixor__, "^=": __ixor__,
'|=': __ior__, "|=": __ior__,
} }
def protected_inplacevar(op, var, expr): def protected_inplacevar(op, var, expr):
"""Do an inplace operation """Do an inplace operation
If the var has an inplace slot, then disallow the operation If the var has an inplace slot, then disallow the operation
unless the var an instance of ``valid_inplace_types``. unless the var an instance of ``valid_inplace_types``.
""" """
if hasattr(var, inplace_slots[op]) and \ if hasattr(var, inplace_slots[op]) and not isinstance(var, valid_inplace_types):
not isinstance(var, valid_inplace_types): try:
try: cls = var.__class__
cls = var.__class__ except AttributeError:
except AttributeError: cls = type(var)
cls = type(var) raise TypeError("Augmented assignment to %s objects is not allowed in untrusted code" % cls.__name__)
raise TypeError( return inplace_ops[op](var, expr)
"Augmented assignment to %s objects is not allowed"
" in untrusted code" % cls.__name__)
return inplace_ops[op](var, expr)