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

Document comparison #768

Merged
merged 9 commits into from
Feb 26, 2021
Merged

Document comparison #768

merged 9 commits into from
Feb 26, 2021

Conversation

hynek
Copy link
Member

@hynek hynek commented Feb 25, 2021

These are some narrative docs for #627, I'm open to suggestions to make it juicier.

Here's the built version: https://attrs--768.org.readthedocs.build/en/768/comparison.html

@Julian @botant

@botant
Copy link
Contributor

botant commented Feb 25, 2021

Looks good to me, simple and direct.

  1. There is a typo in the first sentence:

By default, two instances of attrs classes is equal, if all their fields are equal. For that, attrs writes eq and ne methods for you.

  1. I'd add a case insensitive comparison, since this was one of the requests.

  2. I'd mention pandas DataFrames as well as numpy arrays. Is there a way to provide an example without having to import those libraries?

  3. I'd make it clear that specifying a callable for eq does not mean the same key will be used for order; the key must be specified explicitly.

@hynek
Copy link
Member Author

hynek commented Feb 25, 2021

I believe I have addressed everything except 3. I'd be happy to add a fake doctest but someone else has to write it for me because I've never used neither NumPy nor Pandas. 😳

@botant
Copy link
Contributor

botant commented Feb 25, 2021

I just realized that we were going to provide a wrapping class, since numpy and pandas do not follow the key model. In both cases, you need to use a comparison function that takes 2 arguments. This was discussed in the original PR.

Shall we provide a function like attrs.cmp.using(key: Callable) -> Callable? The output callable is a function that returns a wrapper object that uses key as eq. Or something like that.

Alternatively, and probably easier, we can not mention numpy in this release, and add it back via a different PR.

@hynek
Copy link
Member Author

hynek commented Feb 25, 2021

Hm, I think it would be nicer to publish all at once if it isn't too much work. We had decided on the API IIRC and it shouldn't be too complex to add? 🤔 Just adding the APIs that landed without NumPy support seems to be lacking the main effect? :)

@botant
Copy link
Contributor

botant commented Feb 25, 2021

That works as well. I'll work on it later today, if that is okay?

@botant
Copy link
Contributor

botant commented Feb 25, 2021

There is another point on cmp, and that might be what you meant to ask in the other PR.

Should we de-deprecate it and keep it permanently as syntactic sugar? That would save users from having to wirte attr.ib(eq=int, order=int) for a nicer looking attr.ib(cmp=int).

Just a thought.

@hynek
Copy link
Member Author

hynek commented Feb 25, 2021

That works as well. I'll work on it later today, if that is okay?

It's not like I can cut your salary. ;) If we get it done by tomorrow it's be more than great. RedHat wants a release but there's some minor stuff I need to address first anyways.

Should we de-deprecate it and keep it permanently as syntactic sugar?

Yes we should. It was very obvious to me while writing the docs. :)

@hynek hynek added this to the 21.1.0 milestone Feb 25, 2021
@botant
Copy link
Contributor

botant commented Feb 25, 2021

How about something along these lines? The same logic used to validate cmp, eq and order in attr.ib() would be used here to regulate the usage of the arguments.

attr.cmp.using(eq: Callable = None, order: Callable = None, cmp: Callable = None, enforce_same_class: bool = True) -> Callable

def using(eq: Callable = None, order: Callable = None, cmp: Callable = None, enforce_same_class: bool = True) -> Callable:

    @attr.s(eq=False, order=False)
    class Comparable(object):
        value = attr.ib()
        def __eq__(self, other):
            pass
        def __lt__(self, other):
            pass
        ...

    return Comparable

Note: I think we should let the user choose whether equal classes should be enforced, since the whole point of the PR is to let the user customize the operation.

@hynek
Copy link
Member Author

hynek commented Feb 25, 2021

If everything is done by one function, what's the point of the module? Storing helpers?

@botant
Copy link
Contributor

botant commented Feb 25, 2021

To follow the pattern of validators, etc, but most of all I thought it looked good.

@hynek
Copy link
Member Author

hynek commented Feb 25, 2021

I'm not opposed to it, I'd just like to think it through. I could live with a helper for Numpy/Pandas for instance, if it's possible without importing them.

@botant
Copy link
Contributor

botant commented Feb 25, 2021

I wouldn't add specific helpers for numpy/pandas. Too much trouble and too little added value, if all the user needs is using(key=np.array_equal) or something similar. Plus, I'm afraid that one particular helper might not cover all use cases related to matrices, time series, etc. I think one simple function is the way to go. Maybe attr.cmp_using?

Copy link
Member

@Julian Julian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

hynek and others added 4 commits February 26, 2021 07:42
@hynek hynek merged commit 58d2adc into main Feb 26, 2021
@hynek hynek deleted the cmp-docs branch February 26, 2021 06:54
@hynek
Copy link
Member Author

hynek commented Mar 27, 2021

Maybe attr.cmp_using?

Oooof sorry I lost the thread on this one! Yes please, I’d like to push 21.1.0 but realized this is still open. Any chance to get it done sometime next week?

@botant
Copy link
Contributor

botant commented Apr 10, 2021

Working on this now.

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

Successfully merging this pull request may close these issues.

3 participants