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

evolve() is broken if dunder attribute names are defined. #1062

Closed
mrolle45 opened this issue Nov 28, 2022 · 1 comment
Closed

evolve() is broken if dunder attribute names are defined. #1062

mrolle45 opened this issue Nov 28, 2022 · 1 comment

Comments

@mrolle45
Copy link

Basically, if @define is applied to a class def with a dunder attribute name, like __x__, then calling evolve on an instance of this class raises an exception. This is because evolve only removes the first_ leading underscore, whereas the generated __init__(self, x__=..., ...) removes all leading underscores.

The fix is to use lstrip(name, "_") in the evolve function.

I gather that Attribute will be getting an alias member, which evolve() will make use of, rather than doing the stripping on its own. This might fix the problem. At least if define() makes use of that feature without the class def having to use it explicitly.

A test case could be:

@attrs.define
class C:
    __x__: int
    __y__: int = 42
    __z: int = 42    # mangled to _C__z.

C(0).evolve()       # Should not raise an exception

See #1060.

@asford
Copy link
Contributor

asford commented Nov 30, 2022

Fixed by #950

@hynek hynek closed this as completed Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants