/
superseded.py
557 lines (447 loc) · 19.7 KB
/
superseded.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""
Handling Superseded Functionality
The main mechanism in Sage to deal with superseded functionality is to
add a deprecation warning. This will be shown once, the first time
that the deprecated function is called.
Note that all doctests in the following use the github issue number
:issue:`13109`, which is where this mandatory argument to
:func:`deprecation` was introduced.
Functions and classes
---------------------
"""
########################################################################
# Copyright (C) 2012 William Stein <wstein@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# https://www.gnu.org/licenses/
########################################################################
from warnings import warn
import inspect
from sage.misc.lazy_attribute import lazy_attribute
def _check_issue_number(issue_number):
"""
Check that the argument is likely to be a valid github issue number.
INPUT:
- ``issue_number`` -- anything.
OUTPUT:
This function returns nothing. A ``ValueError`` or ``TypeError`` is
raised if the argument cannot be a valid issue number.
EXAMPLES::
sage: from sage.misc.superseded import _check_issue_number
sage: _check_issue_number(1)
sage: _check_issue_number(0)
Traceback (most recent call last):
...
ValueError: 0 is not a valid github issue number
sage: _check_issue_number(int(10))
sage: _check_issue_number(10.0)
Traceback (most recent call last):
...
TypeError: 10.0000000000000 is not a valid github issue number
sage: _check_issue_number('10')
Traceback (most recent call last):
...
TypeError: '10' is not a valid github issue number
"""
try:
issue_number = issue_number.__index__()
except Exception:
raise TypeError('%r is not a valid github issue number' % issue_number)
if issue_number <= 0:
raise ValueError('%r is not a valid github issue number' % issue_number)
def deprecation(issue_number, message, stacklevel=4):
r"""
Issue a deprecation warning.
INPUT:
- ``issue_number`` -- integer. The github issue number where the
deprecation is introduced.
- ``message`` -- string. An explanation why things are deprecated
and by what it should be replaced.
- ``stack_level`` -- (default: ``4``) an integer. This is passed on to
:func:`warnings.warn`.
EXAMPLES::
sage: def foo():
....: sage.misc.superseded.deprecation(13109, 'the function foo is replaced by bar')
sage: foo()
doctest:...: DeprecationWarning: the function foo is replaced by bar
See https://github.com/sagemath/sage/issues/13109 for details.
.. SEEALSO::
:func:`experimental`,
:func:`warning`.
"""
warning(issue_number, message, DeprecationWarning, stacklevel)
def deprecation_cython(issue_number, message, stacklevel=3):
r"""
Issue a deprecation warning -- for use in cython functions
TESTS:
We check that `deprecation_cython` in a cython function generates a warning
with the same callsite reference as `deprecation` in a python function, whereas
`deprecation` in a cython function does not::
sage: # needs sage.misc.cython
sage: cython(
....: '''
....: from sage.misc.superseded import deprecation_cython, deprecation
....: def foo1():
....: deprecation_cython(100, "boo")
....: def foo2():
....: deprecation(100, "boo")
....: ''')
sage: def foo3():
....: deprecation(100, "boo")
sage: if True: # Execute the three "with" blocks as one doctest
....: with warnings.catch_warnings(record=True) as w1:
....: warnings.simplefilter("always")
....: foo1()
....: with warnings.catch_warnings(record=True) as w2:
....: warnings.simplefilter("always")
....: foo2()
....: with warnings.catch_warnings(record=True) as w3:
....: warnings.simplefilter("always")
....: foo3()
sage: w1[0].filename == w3[0].filename
True
sage: w2[0].filename == w3[0].filename
False
"""
warning(issue_number, message, DeprecationWarning, stacklevel)
def warning(issue_number, message, warning_class=Warning, stacklevel=3):
r"""
Issue a warning.
INPUT:
- ``issue_number`` -- integer. The github issue number where the
deprecation is introduced.
- ``message`` -- string. An explanation what is going on.
- ``warning_class`` -- (default: ``Warning``) a class inherited
from a Python :class:`~exceptions.Warning`.
- ``stack_level`` -- (default: ``3``) an integer. This is passed on to
:func:`warnings.warn`.
EXAMPLES::
sage: def foo():
....: sage.misc.superseded.warning(
....: 99999,
....: 'The syntax will change in future.',
....: FutureWarning)
sage: foo()
doctest:...: FutureWarning: The syntax will change in future.
See https://github.com/sagemath/sage/issues/99999 for details.
.. SEEALSO::
:func:`deprecation`,
:func:`experimental`,
:class:`exceptions.Warning`.
"""
_check_issue_number(issue_number)
message += '\nSee https://github.com/sagemath/sage/issues/' + str(issue_number) + ' for details.'
# Stack level 3 to get the line number of the code which called
# the deprecated function which called this function.
warn(message, warning_class, stacklevel)
def experimental_warning(issue_number, message, stacklevel=4):
r"""
Issue a warning that the functionality or class is experimental
and might change in future.
INPUT:
- ``issue_number`` -- an integer. The github issue number where the
experimental functionality was introduced.
- ``message`` -- a string. An explanation what is going on.
- ``stack_level`` -- (default: ``4``) an integer. This is passed on to
:func:`warnings.warn`.
EXAMPLES::
sage: def foo():
....: sage.misc.superseded.experimental_warning(
....: 66666, 'This function is experimental and '
....: 'might change in future.')
sage: foo()
doctest:...: FutureWarning: This function is experimental and
might change in future.
See https://github.com/sagemath/sage/issues/66666 for details.
.. SEEALSO::
:class:`mark_as_experimental`,
:func:`warning`,
:func:`deprecation`.
"""
warning(issue_number, message, FutureWarning, stacklevel)
class experimental():
def __init__(self, issue_number, stacklevel=4):
"""
A decorator which warns about the experimental/unstable status of
the decorated class/method/function.
INPUT:
- ``issue_number`` -- an integer. The github issue number where this
code was introduced.
- ``stack_level`` -- (default: ``4``) an integer. This is passed on to
:func:`warnings.warn`.
EXAMPLES::
sage: @sage.misc.superseded.experimental(issue_number=79997)
....: def foo(*args, **kwargs):
....: print("{} {}".format(args, kwargs))
sage: foo(7, what='Hello')
doctest:...: FutureWarning: This class/method/function is
marked as experimental. It, its functionality or its
interface might change without a formal deprecation.
See https://github.com/sagemath/sage/issues/79997 for details.
(7,) {'what': 'Hello'}
::
sage: class bird(SageObject):
....: @sage.misc.superseded.experimental(issue_number=99999)
....: def __init__(self, *args, **kwargs):
....: print("piep {} {}".format(args, kwargs))
sage: _ = bird(99)
doctest:...: FutureWarning: This class/method/function is
marked as experimental. It, its functionality or its
interface might change without a formal deprecation.
See https://github.com/sagemath/sage/issues/99999 for details.
piep (99,) {}
TESTS:
The following test works together with the doc-test for
:meth:`__experimental_self_test` to demonstrate that warnings are issued only
once, even in doc-tests (see :issue:`20601`).
::
sage: from sage.misc.superseded import __experimental_self_test
sage: _ = __experimental_self_test("A")
doctest:...: FutureWarning: This class/method/function is
marked as experimental. It, its functionality or its
interface might change without a formal deprecation.
See https://github.com/sagemath/sage/issues/88888 for details.
I'm A
.. SEEALSO::
:func:`experimental`,
:func:`warning`,
:func:`deprecation`.
"""
self.issue_number = issue_number
self.stacklevel = stacklevel
def __call__(self, func):
"""
Print experimental warning.
INPUT:
- ``func`` -- the function to decorate.
OUTPUT:
The wrapper to this function.
TESTS::
sage: def foo(*args, **kwargs):
....: print("{} {}".format(args, kwargs))
sage: from sage.misc.superseded import experimental
sage: ex_foo = experimental(issue_number=99399)(foo)
sage: ex_foo(3, what='Hello')
doctest:...: FutureWarning: This class/method/function is
marked as experimental. It, its functionality or its
interface might change without a formal deprecation.
See https://github.com/sagemath/sage/issues/99399 for details.
(3,) {'what': 'Hello'}
"""
from sage.misc.decorators import sage_wraps
@sage_wraps(func)
def wrapper(*args, **kwds):
if not wrapper._already_issued:
experimental_warning(self.issue_number,
'This class/method/function is marked as '
'experimental. It, its functionality or its '
'interface might change without a '
'formal deprecation.',
self.stacklevel)
wrapper._already_issued = True
return func(*args, **kwds)
wrapper._already_issued = False
return wrapper
class __experimental_self_test():
r"""
This is a class only to demonstrate with a doc-test that the @experimental
decorator only issues a warning message once (see :issue:`20601`).
The test below does not issue a warning message because that warning has
already been issued by a previous doc-test in the @experimental code. Note
that this behaviour cannot be demonstrated within a single documentation
string: Sphinx will itself supress multiple issued warnings.
TESTS::
sage: from sage.misc.superseded import __experimental_self_test
sage: _ = __experimental_self_test("B")
I'm B
"""
@experimental(issue_number=88888)
def __init__(self, x):
print("I'm " + x)
class DeprecatedFunctionAlias():
"""
A wrapper around methods or functions which automatically prints a
deprecation message. See :func:`deprecated_function_alias`.
AUTHORS:
- Florent Hivert (2009-11-23), with the help of Mike Hansen.
- Luca De Feo (2011-07-11), printing the full module path when different from old path
"""
def __init__(self, issue_number, func, module, instance=None, unbound=None):
r"""
TESTS::
sage: from sage.misc.superseded import deprecated_function_alias
sage: g = deprecated_function_alias(13109, number_of_partitions) # needs sage.combinat
sage: from sage.misc.superseded import deprecated_function_alias
sage: g.__doc__ # needs sage.combinat
'Deprecated: Use :func:`number_of_partitions` instead.\nSee :issue:`13109` for details.\n\n'
"""
_check_issue_number(issue_number)
try:
self.__dict__.update(func.__dict__)
except AttributeError:
pass # Cython classes don't have __dict__
self.func = func
self.issue_number = issue_number
self.instance = instance # for use with methods
self.unbound = unbound
self.__module__ = module
if isinstance(func, type(deprecation)):
sphinxrole = "func"
else:
sphinxrole = "meth"
doc = 'Deprecated: '
doc += 'Use :' + sphinxrole + ':`' + self.func.__name__ + '` instead.\n'
doc += 'See :issue:`' + str(self.issue_number) + '` for details.\n\n'
self.__doc__ = doc
@lazy_attribute
def __name__(self):
r"""
TESTS::
sage: from sage.misc.superseded import deprecated_function_alias
sage: g = deprecated_function_alias(13109, number_of_partitions) # needs sage.combinat
sage: g.__name__ # needs sage.combinat
'g'
sage: from sage.misc.superseded import deprecated_function_alias
sage: class cls():
....: def new_meth(self): return 42
....: old_meth = deprecated_function_alias(13109, new_meth)
sage: cls.old_meth.__name__
'old_meth'
sage: cls().old_meth.__name__
'old_meth'
sage: cython('\n'.join([ # needs sage.misc.cython
....: r"from sage.misc.superseded import deprecated_function_alias",
....: r"cdef class cython_cls():",
....: r" def new_cython_meth(self):",
....: r" return 1",
....: r" old_cython_meth = deprecated_function_alias(13109, new_cython_meth)"
....: ]))
sage: cython_cls().old_cython_meth.__name__ # needs sage.misc.cython
'old_cython_meth'
"""
# first look through variables in stack frames
for frame in inspect.stack():
for name, obj in frame[0].f_globals.items():
if obj is self:
return name
# then search object that contains self as method
import gc
import copy
gc.collect()
def is_class(gc_ref):
if not isinstance(gc_ref, dict):
return False
is_python_class = '__module__' in gc_ref or '__package__' in gc_ref
is_cython_class = '__new__' in gc_ref
return is_python_class or is_cython_class
search_for = self if (self.unbound is None) else self.unbound
for ref in gc.get_referrers(search_for):
if is_class(ref) and ref is not self.__dict__:
ref_copy = copy.copy(ref)
for key, val in ref_copy.items():
if val is search_for:
return key
raise AttributeError("The name of this deprecated function cannot be determined")
def __call__(self, *args, **kwds):
"""
TESTS::
sage: from sage.misc.superseded import deprecated_function_alias
sage: def bla(): return 42
sage: blo = deprecated_function_alias(13109, bla)
sage: blo()
doctest:...: DeprecationWarning: blo is deprecated. Please use bla instead.
See https://github.com/sagemath/sage/issues/13109 for details.
42
"""
if self.instance is None and self.__module__ != self.func.__module__:
other = self.func.__module__ + "." + self.func.__name__
else:
other = self.func.__name__
deprecation(self.issue_number,
"{} is deprecated. Please use {} instead.".format(
self.__name__, other))
if self.instance is None:
return self.func(*args, **kwds)
else:
return self.func(self.instance, *args, **kwds)
def __get__(self, inst, cls=None):
"""
TESTS::
sage: from sage.misc.superseded import deprecated_function_alias
sage: class cls():
....: def new_meth(self): return 42
....: old_meth = deprecated_function_alias(13109, new_meth)
sage: obj = cls()
sage: obj.old_meth.instance is obj
True
:issue:`19125`::
sage: from sage.misc.superseded import deprecated_function_alias
sage: class A:
....: def __init__(self, x):
....: self.x = x
....: def f(self, y):
....: return self.x+y
....: g = deprecated_function_alias(42, f)
sage: a1 = A(1)
sage: a2 = A(2)
sage: a1.g(a2.g(0))
doctest:...: DeprecationWarning: g is deprecated. Please use f instead.
See https://github.com/sagemath/sage/issues/42 for details.
3
sage: a1.f(a2.f(0))
3
"""
if inst is None:
return self # Unbound method lookup on class
else:
# Return a bound method wrapper
return DeprecatedFunctionAlias(self.issue_number, self.func,
self.__module__, instance=inst,
unbound=self)
def deprecated_function_alias(issue_number, func):
"""
Create an aliased version of a function or a method which raises a
deprecation warning message.
If f is a function or a method, write
``g = deprecated_function_alias(issue_number, f)``
to make a deprecated aliased version of f.
INPUT:
- ``issue_number`` -- integer. The github issue number where the
deprecation is introduced.
- ``func`` -- the function or method to be aliased
EXAMPLES::
sage: from sage.misc.superseded import deprecated_function_alias
sage: g = deprecated_function_alias(13109, number_of_partitions) # needs sage.combinat sage.libs.flint
sage: g(5) # needs sage.combinat sage.libs.flint
doctest:...: DeprecationWarning: g is deprecated.
Please use sage.combinat.partition.number_of_partitions instead.
See https://github.com/sagemath/sage/issues/13109 for details.
7
This also works for methods::
sage: class cls():
....: def new_meth(self): return 42
....: old_meth = deprecated_function_alias(13109, new_meth)
sage: cls().old_meth()
doctest:...: DeprecationWarning: old_meth is deprecated. Please use new_meth instead.
See https://github.com/sagemath/sage/issues/13109 for details.
42
:issue:`11585`::
sage: def a(): pass
sage: b = deprecated_function_alias(13109, a)
sage: b()
doctest:...: DeprecationWarning: b is deprecated. Please use a instead.
See https://github.com/sagemath/sage/issues/13109 for details.
AUTHORS:
- Florent Hivert (2009-11-23), with the help of Mike Hansen.
- Luca De Feo (2011-07-11), printing the full module path when different from old path
"""
module_name = None
frame0 = inspect.currentframe()
if frame0:
frame1 = frame0.f_back
if frame1:
module_name = inspect.getmodulename(frame1.f_code.co_filename)
if module_name is None:
module_name = '__main__'
return DeprecatedFunctionAlias(issue_number, func, module_name)