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

attr.ibs that will be __name-mangled should be attrs-init-mangled to <name> not ClassName_<name> #619

Open
graingert opened this issue Jan 27, 2020 · 5 comments
Labels

Comments

@graingert
Copy link
Contributor

graingert commented Jan 27, 2020

all attrs does is remove the leading "_" which is a bit unfortunate

import attr

@attr.s
class Breakfast:
    __spam = attr.ib()
    __eggs = attr.ib()

    @classmethod
    def create(cls):
        return cls(Breakfast__spam=1, Breakfast__eggs=2)

    def with_ham(self, ham):
       return attr.evolve(self, Breakfast__spam=process(ham))

I think it might be nicer as:

import attr

@attr.s
class Breakfast:
    __spam = attr.ib()
    __eggs = attr.ib()

    @classmethod
    def create(cls):
        return cls(**{cls.__spam: 1, cls.__eggs: 2})

    def with_ham(self, ham):
       return attr.evolve(self, **{type(self).__spam: process(ham)})

I currently work around it with

import re

import attr

class _Mangle:
    def __getattr__(self, name):
        return re.sub(pattern=r"\A_", repl="", string=name)

M = _Mangle()


@attr.s
class Breakfast:
    __spam = attr.ib()
    __eggs = attr.ib()

    @classmethod
    def create(cls):
        return cls(**{M.__spam: 1, M.__eggs: 2})


    def with_ham(self, ham):
       return attr.evolve(self, **{M.__spam: process(ham)})
@graingert graingert changed the title mangled names don't work well working with mangled names isn't as nice as it could be Jan 27, 2020
@Julian
Copy link
Member

Julian commented Feb 29, 2020

I suspect (and just quickly half-confirmed) the fact that this does anything at all is just "by accident", and that the code just checks the first character of the attribute name.

Personally I'm on the "don't use mangled attributes" team, but I guess if you're going to use them, then yeah the logical thing to do is strip the class name too. Worth noting that the same "issue" happens if you want double-underscore names, i.e.

@attr.s
class Foo(object):
    __foo__ = attr.ib()

has to be initialized via Foo(foo__=12) which is yeah...

@Julian Julian changed the title working with mangled names isn't as nice as it could be attr.ibs that will be __name-mangled should be attrs-init-mangled to <name> not ClassName_<name> Feb 29, 2020
@hynek
Copy link
Member

hynek commented Feb 29, 2020

OK, y'all win: @attr.s(strip_underscores=True) – go! ;)

@graingert
Copy link
Contributor Author

graingert commented Feb 29, 2020

@hynek well I don't that will help

You'd have to use cls(__Breakfast_ham=...) I think it should strip the classname

@asford
Copy link
Contributor

asford commented Apr 2, 2022

OK, y'all win: @attr.s(strip_underscores=True) – go! ;)

@hynek could I read this as... you'd be open to a PR on #945?

@asford asford mentioned this issue Nov 13, 2022
21 tasks
@hynek
Copy link
Member

hynek commented Apr 5, 2023

So I believe the worst brunt of this bug is fixed by #950 and I wonder if it's possible to fix this (i.e. __spam field becoming a spam argument) without breaking the whole ecosystem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants