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

More informative repr for Repository objects #352

Merged
merged 1 commit into from
Mar 26, 2014

Conversation

takluyver
Copy link
Contributor

Before, with the default Python repr:

<pygit2.repository.Repository at 0x7f3858b1cc00>

After, with a repr that shows how to construct this object (which is recommended where practical):

pygit2.Repository('/home/takluyver/Code/pygit2/.git/')

@pypingou
Copy link

Isn't the standard way of doing it more along the lines of <Object ...> ? So maybe <pygit2.Repository '/home/takluyver/Code/pygit2/.git/'> ?

@takluyver
Copy link
Contributor Author

The docs say "For many types, this function [repr] makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object."

If the object can practically be reconstructed in a single line of code, the constructor form is generally preferred. In the standard library, classes in the decimal, fractions, collections and ast modules work like this, for example.

@takluyver
Copy link
Contributor Author

Actually, the docs for the __repr__ method are even clearer: " If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment)."

@uniphil
Copy link
Contributor

uniphil commented Mar 25, 2014

Does a pygit2.Repository store any mutable state in its instance? Or is
all the state on disc in the git db?

Note that for collections drops the module name

>>> from collections import defaultdict
>>> repr(defaultdict(int))
"defaultdict(<type 'int'>, {})"

so just Repository('/home/takluyver/Code/pygit2/.git/') would be
consistent with that.

On Tue, Mar 25, 2014 at 7:03 PM, Thomas Kluyver notifications@github.comwrote:

Actually, the docs for the repr method are even clearer: " If at all
possible, this should look like a valid Python expression that could be
used to recreate an object with the same value (given an appropriate
environment)."

Reply to this email directly or view it on GitHubhttps://github.com//pull/352#issuecomment-38632372
.

@takluyver
Copy link
Contributor Author

Yes, I was assuming that there's no important state stored in the Repository object.

I feel like I've seen modules that keep the module name in as well, though I can't find any at the moment. It seems better to me to be unambiguous, but I'm happy with dropping the module name if others prefer that.

@takluyver
Copy link
Contributor Author

datetime.datetime is an example of something that puts the module name in the repr.

@jdavid jdavid merged commit af25284 into libgit2:master Mar 26, 2014
@takluyver
Copy link
Contributor Author

Thanks!

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.

4 participants