mxndtaylor / aliasing

a small utility library to add aliases to python classes
MIT License
1 stars 0 forks source link

Feature: alias names with starting with `__` should be mangled when attached to a class #28

Open mxndtaylor opened 4 weeks ago

mxndtaylor commented 4 weeks ago

Is your feature request related to a problem?

python's mangling system is convenient, but with this method of aliasing member, we do not mangle the alias names.

leading to issues like this:

class Foo:
    @aka.valiases("__mangled_alias")
    def __mangled_initial(self):
        return "val"

print(Foo().__mangled_alias())
# prints "val"

I would expect the above to fail, and leave the alias's name as _Foo__mangled_alias instead, so that this succeeds:

class Foo:
    @aka.valiases("__mangled_alias")
    def __mangled_initial(self):
        return "val"

print(Foo()._Foo__mangled_alias())
# prints "val"

Describe the solution you'd like

if alias.__set_name__ gets a name that has leading __ (with at most 1 trailing _), then mangle the alias's name based on owner.__name__.

ex:

class Foo:
    prop = 1

prop_alias = alias(alias_name="__prop")
prop_alias.attach(Foo)
assert Foo()._Foo__prop == 1

Additional context

We might want to add a parameter no_mangle or such that disables this behavior