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

Added source code link #172

Merged
merged 9 commits into from Oct 14, 2022
Merged

Added source code link #172

merged 9 commits into from Oct 14, 2022

Conversation

ayyucedemirbas
Copy link
Contributor

All of the links take you to the _hf_hub.py file. I must add specific lines.

@ayyucedemirbas ayyucedemirbas marked this pull request as ready for review October 4, 2022 21:55
@adrinjalali
Copy link
Member

Thanks for the pull request @ayyucedemirbas , please run black and isort on your modified python files to format them correctly.

Alternatively, you can enable pre-commit hooks to do that automatically when you commit code: https://github.com/skops-dev/skops/blob/main/CONTRIBUTING.rst#using-condamamba

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

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

Thanks @ayyucedemirbas , this looks quite good already.

docs/conf.py Show resolved Hide resolved
docs/conf.py Outdated
Comment on lines 77 to 78
if not info["module"]:
return None
Copy link
Member

Choose a reason for hiding this comment

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

this is checked bellow again (info.get("module") is safer than info["module"] since the latter can raise an exception if the key doesn't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✅ Done

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now it works, I updated the PR. Could you please take a look at it? Thank you so much for everything!

docs/conf.py Outdated
except Exception:
lineno = ""
return (
"https://github.com/skops-dev/skops/blob/main/skops/" + fn + "#L" + str(lineno)
Copy link
Member

Choose a reason for hiding this comment

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

this shouldn't refer to the main branch, it should refer to the branch for which this documentation is being built. For example, in sklearn in this page for ClassifierMixin you get this link which is not on main.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added these lines from scikit-learn:

REVISION_CMD = "git rev-parse --short HEAD"

def _get_git_revision():
    try:
        revision = subprocess.check_output(REVISION_CMD.split()).strip()
    except (subprocess.CalledProcessError, OSError):
        print("Failed to execute git to get revision")
        return None
    return revision.decode("utf-8")
def linkcode_resolve(domain, info):
   ...
    revision = _get_git_revision()
    if revision is None:
        return
   ...
    return (
        "https://github.com/skops-dev/skops/blob/{revision}/skops/" + fn + "#L" + str(lineno)
    )

However, the source code links became something like this (https://github.com/skops-dev/skops/blob/%7Brevision%7D/skops/hub_utils/_hf_hub.py#L328) and of course, they don't work

Copy link
Member

Choose a reason for hiding this comment

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

Could you try this patch:

diff --git a/docs/conf.py b/docs/conf.py
index 3ed773f..4ed5d3d 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -8,6 +8,7 @@
 
 import inspect
 import os
+import subprocess
 from operator import attrgetter
 
 from packaging.version import parse
@@ -71,6 +72,19 @@ autosummary_generate = True
 issues_github_path = "skops-dev/skops"
 
 
+# -- linkcode configuration --------------------------------------------------
+REVISION_CMD = "git rev-parse --short HEAD"
+
+
+def _get_git_revision():
+    try:
+        revision = subprocess.check_output(REVISION_CMD.split()).strip()
+    except (subprocess.CalledProcessError, OSError):
+        print("Failed to execute git to get revision")
+        return None
+    return revision.decode("utf-8")
+
+
 def linkcode_resolve(domain, info):
     if domain != "py":
         return None
@@ -93,7 +107,7 @@ def linkcode_resolve(domain, info):
     try:
         fn = inspect.getsourcefile(inspect.unwrap(obj))
     except TypeError:
-        try: 
+        try:
             fn = inspect.getsourcefile(inspect.unwrap(obj.fget))
         except (AttributeError, TypeError):
             fn = None
@@ -105,9 +119,16 @@ def linkcode_resolve(domain, info):
         lineno = inspect.getsourcelines(obj)[1]
     except Exception:
         lineno = ""
-    return (
-        "https://github.com/skops-dev/skops/blob/main/skops/" + fn + "#L" + str(lineno)
+    url_fmt = (
+        "https://github.com/scikit-learn/"
+        "scikit-learn/blob/{revision}/"
+        "{package}/{path}#L{lineno}"
     )
+    revision = _get_git_revision()
+    return url_fmt.format(revision=revision, package=package, path=fn, lineno=lineno)
+
+
+# -- linkcode configuration --------------------------------------------------
 
 
 # -- Options for HTML output -------------------------------------------------

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

There are two problems why this doesn't work. First of all it links to sklearn but we want to link to skops (I guess Adrin forgot to adjust the copied code):

    url_fmt = (
        "https://github.com/skops-dev/"
        "skops/blob/{revision}/"
        "{package}/{path}#L{lineno}"
    )

But even after that, the link wouldn't lead anywhere. However, this is probably because the git revision's hash does not lead anywhere (yet). If we replace it with the hash of an actual commit (current HEAD on main branch), it will work, e.g.:

https://github.com/skops-dev/skops/blob/69621cd/skops/hub_utils/_hf_hub.py#L594

where 69621cd is the current git rev-parse --short HEAD. So when you test the link locally, it might not work because the commit doesn't exist on GitHub. But that's no problem, once the code is merged, the links will be correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahahahahhahahhahah I feel like an idiot. Thank you so much!

docs/conf.py Outdated
@@ -65,6 +70,46 @@
# (note that `group` is the GitHub user or organization)
issues_github_path = "skops-dev/skops"


def linkcode_resolve(domain, info):
if domain != "py":
Copy link
Collaborator

Choose a reason for hiding this comment

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

Adrin already mentioned this, but this line and the one below

if domain not in ("py", "pyx"):

do redundant work. Only the latter is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✅ Done

docs/conf.py Outdated
return None
if not info["module"]:
return None
filename = info["module"].replace(".", "/")
Copy link
Collaborator

Choose a reason for hiding this comment

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

filename is not being used and can be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✅ Done

docs/conf.py Outdated
try:
fn = inspect.getsourcefile(inspect.unwrap(obj))
except TypeError:
try:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
try:
try:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✅ Done

docs/conf.py Outdated
except Exception:
lineno = ""
return (
"https://github.com/skops-dev/skops/blob/main/skops/" + fn + "#L" + str(lineno)
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are two problems why this doesn't work. First of all it links to sklearn but we want to link to skops (I guess Adrin forgot to adjust the copied code):

    url_fmt = (
        "https://github.com/skops-dev/"
        "skops/blob/{revision}/"
        "{package}/{path}#L{lineno}"
    )

But even after that, the link wouldn't lead anywhere. However, this is probably because the git revision's hash does not lead anywhere (yet). If we replace it with the hash of an actual commit (current HEAD on main branch), it will work, e.g.:

https://github.com/skops-dev/skops/blob/69621cd/skops/hub_utils/_hf_hub.py#L594

where 69621cd is the current git rev-parse --short HEAD. So when you test the link locally, it might not work because the commit doesn't exist on GitHub. But that's no problem, once the code is merged, the links will be correct.

Copy link
Collaborator

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

It looks good now, the documentation successfully links to the code, thanks a lot. I have only two minor comments, one about the domain check and one would be for you to add an entry to changes.rst.

docs/conf.py Outdated
Comment on lines 80 to 81
if domain not in ("py", "pyx"):
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would keep this check, we just don't need the if domain != "py": too, as it's redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All done! I couldn't edit the changes.rst file in this pr, so I had to create a new one, which is #186

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't edit the changes.rst file in this pr

That's strange. What error did you get? Maybe we can figure it out :)

There is no error. The problem is I don't know how to do that :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, same as with the conf.py? You modify the changes.rst, then git add changes.rst, then git commit -m "blabla" and git push.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I use the browser. Is there any way to do that on the browser? Or I'll use the CLI.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I think it's probably possible in the browser, but I have never used it, so maybe not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I get this error message:

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/ayyucedemirbas/skops.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps, doing that through the browser would be the best.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do a git pull, git will merge the changes made here to your local branch. That should work without further complications because there should be no merge conflicts. After that, try git push again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I did it (via browser) :) Thanks a lot!

@BenjaminBossan
Copy link
Collaborator

I couldn't edit the changes.rst file in this pr

That's strange. What error did you get? Maybe we can figure it out :)

Copy link
Collaborator

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your work and patience. There was a minor merge conflict that I solved on your branch, this should be ready to be merged now.

@ayyucedemirbas
Copy link
Contributor Author

Thank you so much for everything!

@BenjaminBossan BenjaminBossan merged commit 1020c81 into skops-dev:main Oct 14, 2022
@BenjaminBossan BenjaminBossan mentioned this pull request Oct 14, 2022
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.

None yet

3 participants