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

option for disable strips the underscores #391

Closed
sky-code opened this issue Jun 9, 2018 · 9 comments
Closed

option for disable strips the underscores #391

sky-code opened this issue Jun 9, 2018 · 9 comments
Labels

Comments

@sky-code
Copy link

sky-code commented Jun 9, 2018

Add an option to disable strips the underscores
http://www.attrs.org/en/stable/init.html#private-attributes

I need to create class with _id attribute and this is not possible with attrs at this moment

@hynek
Copy link
Member

hynek commented Jun 9, 2018

You can have an _id attribute just fine; it’s just the __init__ signature is going to be __init__(_id):

In [1]: import attr

In [2]: @attr.s
   ...: class C:
   ...:     _id = attr.ib()
   ...:

In [3]: C(1)
Out[3]: C(_id=1)

@sky-code
Copy link
Author

Problem is in __init__ method

In [1]: import attr

In [2]: @attr.s
   ...: class C:
   ...:     _id = attr.ib()
   ...:

In [3]: C(**{'_id': 1})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument '_id'

How to add _id as is without stripping into __init__ kwargs ?

@decentral1se
Copy link

decentral1se commented Nov 11, 2019

_ stripping should be optional IMHO. I am blocked on adding a _thing to my class which also has a thing attribute (don't ask, I'm doing a source port). Even if you are right about private attributes "not existing", this choice encoded into the software seems contrary to the stated goals of not "slowing down your code".

@decentral1se
Copy link

decentral1se commented Nov 11, 2019

Furthermore, the documentation states that "There really isn’t a right or wrong, it’s a matter of taste." but one way is not allowed in the software so there is clearly a wrong way to do it according to attrs. This is misleading.

@energizah
Copy link

This issue has come up in some generated code before, for example like this.

@attr.s
class A:
    _ = attr.ib()

File "<attrs generated init __main__.A>", line 2
    self._ =
             ^
SyntaxError: invalid syntax

@Jasperscheper
Copy link

Any updates on this topic?

@Tinche
Copy link
Member

Tinche commented Nov 19, 2020

A side note, you can use cattrs to structure classes with underscore attributes:

import attr
import cattr

@attr.s
class C:
  _id = attr.ib()

cattr.structure({'_id': 1}, C)

@MicaelJarniac
Copy link
Contributor

A side note, you can use cattrs to structure classes with underscore attributes:

import attr
import cattr

@attr.s
class C:
  _id = attr.ib()

cattr.structure({'_id': 1}, C)

This still doesn't allow for having both a thing and a _thing.

@wbolster
Copy link
Member

wbolster commented Apr 7, 2022

closing in favour of #945, which is newer, but has many more details and proposals.

@wbolster wbolster closed this as completed Apr 7, 2022
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

9 participants