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

Replace sage_wraps by decorator library #30884

Open
tobiasdiez opened this issue Nov 10, 2020 · 47 comments
Open

Replace sage_wraps by decorator library #30884

tobiasdiez opened this issue Nov 10, 2020 · 47 comments

Comments

@tobiasdiez
Copy link
Contributor

Replace the sage custom sage_wrap and decorator_defaults methods by the well-maintained decorator library https://github.com/micheles/decorator.

My motivation is to replace sage's autodoc sphinx extension with the built-in one. For this, the Phyton introspection code needs to work without sage's customization. This ticket is a first step towards this goal. But independently of this motivation the approach using decorator library already provides cleaner code and better introspection results with respect to annotations (e.g. typing information). See the following test script for a comparison:

import inspect
from pathlib import Path
from sage.misc.decorators import decorator_keywords, sage_wraps
from decorator import decorator, getfullargspec
from sage.misc.sageinspect import sage_getargspec

@decorator
def warn_slow(func, timelimit=60, *args, **kw):
    print('%s took %d seconds', func.__name__, timelimit)
    return func(*args, **kw)

@warn_slow
def preprocess_input_files(inputdir: Path, tempdir):
    pass

@warn_slow(timelimit=600)
def run_calculation(tempdir, outdir):
    pass

print("With decorator")
print(getfullargspec(preprocess_input_files))
# FullArgSpec(args=['inputdir', 'tempdir'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'inputdir': <class 'pathlib.Path'>})
print(inspect.getfullargspec(preprocess_input_files))
# FullArgSpec(args=['inputdir', 'tempdir'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'inputdir': <class 'pathlib.Path'>})
# !! Yields full information as expected
print(sage_getargspec(preprocess_input_files))
# ArgSpec(args=['inputdir', 'tempdir'], varargs=None, keywords=None, defaults=None)
# !! No annotation information
print(getfullargspec(run_calculation))
# FullArgSpec(args=['tempdir', 'outdir'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
print(inspect.getfullargspec(run_calculation))
# FullArgSpec(args=['tempdir', 'outdir'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
print(sage_getargspec(preprocess_input_files))
# ArgSpec(args=['inputdir', 'tempdir'], varargs=None, keywords=None, defaults=None)

@decorator_keywords
def warn_slow_sage(func=None, timelimit=60):
     @sage_wraps(func)
     def wrapper(*args, **kwargs):
         print('%s took %d seconds', func.__name__, timelimit)
         return func(*args, **kw)
     return wrapper

@warn_slow_sage
def preprocess_input_files_sage(inputdir: Path, tempdir):
    pass

@warn_slow_sage(timelimit=600)
def run_calculation_sage(tempdir, outdir):
    pass

print("With sage")
print(getfullargspec(preprocess_input_files_sage))
# FullArgSpec(args=[], varargs='args', varkw='kwargs', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
print(inspect.getfullargspec(preprocess_input_files_sage))
# FullArgSpec(args=[], varargs='args', varkw='kwargs', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
# !! No args nor annotation
print(sage_getargspec(preprocess_input_files_sage))
# ArgSpec(args=['inputdir', 'tempdir'], varargs=None, keywords=None, defaults=None)
print(getfullargspec(run_calculation_sage))
# !! No annotation info
# FullArgSpec(args=[], varargs='args', varkw='kwargs', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
print(inspect.getfullargspec(run_calculation_sage))
# FullArgSpec(args=[], varargs='args', varkw='kwargs', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
print(sage_getargspec(preprocess_input_files_sage))
# ArgSpec(args=['inputdir', 'tempdir'], varargs=None, keywords=None, defaults=None)

Replacing sage_getargspec with inspect.getfullargspec will be done in a follow-up ticket (as this is already big enough).

Todo (as follow-up): Migrate cachefunction etc to decorator framework. The documentation https://github.com/micheles/decorator/blob/master/docs/documentation.md#the-solution contains an example (memoize) of how this may look like.

Depends on #26254

CC: @mkoeppe @kwankyu

Component: doctest framework

Work Issues: Fix patchbot warnings

Author: Tobias Diez

Branch/Commit: public/documentation/replaceDecorator @ 1b656a3

Issue created by migration from https://trac.sagemath.org/ticket/30884

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 10, 2020

Changed commit from b414b8e to dbe456a

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 10, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

dbe456aRemove unused decorator_defaults

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 10, 2020

Changed commit from dbe456a to 0547957

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 10, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

0547957Fix stupid mistakes

@mkoeppe
Copy link
Member

mkoeppe commented Nov 11, 2020

comment:3

Functions such as sage_wraps cannot just be removed, as they may be used by user code. They need to go through deprecation

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 11, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

3a433f7Fix option wrappers
8b05582Readd unused functions, but deprecated them

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 11, 2020

Changed commit from 0547957 to 8b05582

@tobiasdiez
Copy link
Contributor Author

comment:5

Good to know! I've readded and marked as deprecated them.

This should be now ready for review.

@tobiasdiez
Copy link
Contributor Author

Changed work issues from Fix patchbot warnings to none

@mkoeppe
Copy link
Member

mkoeppe commented Nov 11, 2020

comment:8

Better squash changes like this so that git blame is more accurate

@mkoeppe
Copy link
Member

mkoeppe commented Nov 11, 2020

comment:9

Does the decorator library really misspell decorator as decorater?

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 11, 2020

Changed commit from 8b05582 to 2c781d9

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 11, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

2c781d9Fix imports

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 11, 2020

Changed commit from 2c781d9 to 3f04a50

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 11, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

3f04a50Fix spelling

@tobiasdiez
Copy link
Contributor Author

comment:12

Replying to @mkoeppe:

Does the decorator library really misspell decorator as decorater?

No of course not. That was a result of some last minute changes on my side. Fixed now.


New commits:

3f04a50Fix spelling

@tobiasdiez
Copy link
Contributor Author

Work Issues: Fix patchbot warnings

@jhpalmieri
Copy link
Member

comment:15

Can you explain the purpose of the change

diff --git a/src/sage_setup/docbuild/__init__.py b/src/sage_setup/docbuild/__init__.py
index b07e9c1..a4ba68e 100644
--- a/src/sage_setup/docbuild/__init__.py
+++ b/src/sage_setup/docbuild/__init__.py
@@ -564,7 +564,7 @@ class ReferenceBuilder(AllBuilder):
             if not os.path.exists(refdir):
                 continue
             logger.info('Building bibliography')
-            self._build_bibliography(lang, format, *args, **kwds)
+            #self._build_bibliography(lang, format, *args, **kwds)
             logger.info('Bibliography finished, building dependent manuals')
             self._build_everything_except_bibliography(lang, format, *args, **kwds)
             # The html refman must be build at the end to ensure correct

?

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 11, 2020

Changed commit from 3f04a50 to f7c1b7e

@mkoeppe
Copy link
Member

mkoeppe commented Jan 31, 2021

comment:23

What's the status of this ticket? Just came across a sage_getargspec bug (#31309), so it would be great if we can get rid of it

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 2, 2021

Changed commit from fc23e73 to b4f05c7

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 2, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

99002b9Merge branch 'develop' of git://github.com/sagemath/sage into public/documentation/replaceDecorator
b4f05c7Fix compile error

@tobiasdiez
Copy link
Contributor Author

comment:25

It is working in principle, but I still need to fix a few things here and there. The biggest problem is that there are some changes to cython files needed, for which it would be really handy if we could get #30371 merged.

@mkoeppe
Copy link
Member

mkoeppe commented Mar 15, 2021

comment:26

Setting new milestone based on a cursory review of ticket status, priority, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Mar 15, 2021
@mkoeppe
Copy link
Member

mkoeppe commented Jul 19, 2021

comment:27

Setting a new milestone for this ticket based on a cursory review.

@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Jul 19, 2021
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 28, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

73c6544Merge branch 'develop' of https://github.com/sagemath/sage into public/documentation/replaceDecorator
a20f208Fix a few things

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 28, 2021

Changed commit from b4f05c7 to a20f208

@tobiasdiez
Copy link
Contributor Author

Changed work issues from Fix patchbot warnings to Wait for micheles/decorator#137 to be merged and released, update decorator lib, x patchbot warnings

@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 14, 2021
@tobiasdiez
Copy link
Contributor Author

Dependencies: #26254

@tobiasdiez
Copy link
Contributor Author

Changed work issues from Wait for micheles/decorator#137 to be merged and released, update decorator lib, x patchbot warnings to Fix patchbot warnings

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 8, 2022

Changed commit from a20f208 to 89487a7

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 8, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

5751b7dMerge branch 'develop' of https://github.com/sagemath/sage into public/documentation/replaceDecorator
ca86cc9build/pkgs/decorator: Update to 5.1.1
146bc0bRevert changes to cache functions
bff4f3aReplace deprecated decorator by message
e3d5bc0Fix deprecation wrapper
89487a7Activate binding=true compiler directive

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 8, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

6adf9daReadd empty line
1b656a3Readd sage_wraps in deprecated methods

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 8, 2022

Changed commit from 89487a7 to 1b656a3

@tobiasdiez

This comment has been minimized.

@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 Apr 2, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Aug 31, 2022
@mkoeppe mkoeppe removed this from the sage-9.8 milestone Jan 29, 2023
kryzar pushed a commit to kryzar/sage that referenced this issue Feb 6, 2023
We rebase `sage_autodoc` to Sphinx 5.3.0.

This is a step toward eventual removal of sage_autodoc in sagemath#30893, a
customization of Sphinx autodoc extension for Sage objects.

Other related tickets are sagemath#27578. sagemath#30884, sagemath#31309, in this regard.

URL: https://trac.sagemath.org/34730
Reported by: klee
Ticket author(s): Kwankyu Lee
Reviewer(s): Tobias Diez
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants