python-attrs / python-attrs/attrs

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

Open
#619 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Description

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)})

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named in the issue. Start by locating attrs' handling of double-underscore attribute names and constructor or evolve argument names, then compare the current ClassName_ prefix with the proposed name-mangled form; done means the desired names work consistently for initialization and attr.evolve without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.