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

Feature/fields properties #88

Merged
merged 2 commits into from Sep 10, 2016
Merged

Conversation

@Tinche
Copy link
Member

@Tinche Tinche commented Sep 10, 2016

You're gonna loooove the implementation of this.

Well, you might actually love the API.

>>> from attr import Factory, fields
>>> fields(Factory)
(Attribute(name='factory', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None),)
>>> fields(Factory).factory
Attribute(name='factory', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None)
>>>

The way this is done is, for each attrs class we generate a tiny subclass of tuple. The subclass basically looks like this:

class FactoryAttributes(tuple):
    factory = property(itemgetter(0))

That's all.

No docs/changelog or anything until you say you like it.

@codecov-io
Copy link

@codecov-io codecov-io commented Sep 10, 2016

Current coverage is 100% (diff: 100%)

Merging #88 into master will not change coverage

@@           master   #88   diff @@
===================================
  Files           8     8          
  Lines         428   443    +15   
  Methods         0     0          
  Messages        0     0          
  Branches       94    98     +4   
===================================
+ Hits          428   443    +15   
  Misses          0     0          
  Partials        0     0          

Powered by Codecov. Last update dcfbb7b...3581a57

Loading

@hynek
Copy link
Member

@hynek hynek commented Sep 10, 2016

Jesus where do you get these ideas. 😮

attrs' new tagline: “we subclass tuple so you don‘t have to”

how does not everything break!?

Loading

@hynek
Copy link
Member

@hynek hynek commented Sep 10, 2016

On a more serious note: does this handle well if you define two classes with the same name in different namespaces?

Loading

@Tinche
Copy link
Member Author

@Tinche Tinche commented Sep 10, 2016

Nah I think it's fine.

got some classes lying around from the pickle thing:

$ cat t.py
import attr

@attr.s(slots=True, frozen=True)
class A(object):
    y = attr.ib()

    def __getstate__(self):
        return (self.y, )

    def __setstate__(self, state):
        self.__init__(*state)

> cat tt.py
import attr


@attr.s(slots=True, frozen=True)
class A(object):
    z = attr.ib()

    def __getstate__(self):
        return (self.z, )

    def __setstate__(self, state):
        self.__init__(*state)
> python
Python 3.5.2 (default, Jul  5 2016, 12:43:10) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from t import A as TA
>>> from tt import A as TTA
>>> from attr import fields
>>> fields(TA)
(Attribute(name='y', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None),)
>>> fields(TTA)
(Attribute(name='z', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None),)
>>>

"We do stupid shit and test it so you don't have to" is actually an OK tagline :)

Loading

@hynek hynek merged commit 024a16c into python-attrs:master Sep 10, 2016
2 checks passed
Loading
@hynek
Copy link
Member

@hynek hynek commented Sep 10, 2016

I don't think we need any docs/CL since these are implementation details.

Loading

@Tinche
Copy link
Member Author

@Tinche Tinche commented Sep 10, 2016

You asked where I got the idea: namedtuple! That's basically how it's
implemented. It's in Python, you can read the source easily.

I thought this would be the new name-based API. You know, for
include/exclude? Instead of deprecated class attributes. No?

On 17:51, Sat, Sep 10, 2016 Hynek Schlawack notifications@github.com
wrote:

I don't think we need any docs/CL since these ate implementation details.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#88 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AB0h8bISbfxQOVBay3r86BRsqug9h2nbks5qotHkgaJpZM4J5uox
.

Loading

@hynek
Copy link
Member

@hynek hynek commented Sep 10, 2016

Hmmmm I thought this was more to simplify the planned implementations and pre-computing the attribute map.

But yeah the API is nicer this way.

Loading

@Tinche Tinche deleted the feature/fields-properties branch Jan 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants