Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
fslpy
Commits
861582f0
Commit
861582f0
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Simple decorator for deprecating things.
parent
08b6ab36
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/deprecated.py
+60
-0
60 additions, 0 deletions
fsl/utils/deprecated.py
with
60 additions
and
0 deletions
fsl/utils/deprecated.py
0 → 100644
+
60
−
0
View file @
861582f0
#!/usr/bin/env python
#
# deprecated.py - Decorator for deprecating things.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""
This module provides the :func:`deprecated` function, a simple decorator
for deprecating functions and methods.
"""
import
functools
as
ft
import
inspect
import
warnings
_warned_cache
=
set
()
"""
Used by the :func:`deprecated` function to keep track of whether a warning
has already been emitted for the use of a deprecated item.
"""
def
deprecated
(
vin
=
None
,
msg
=
None
):
"""
Decorator to mark a function or method as deprecated. A
``DeprecationWarning`` is raised via the standard ``warnings`` module.
:arg vin: Optional version - the warning message will mention that the
function is deprecated from this version.
:arg msg: Optional message to use in the warning.
"""
if
vin
is
not
None
:
msgfmt
=
'
{{name}} is deprecated from version {vin}.
'
.
format
(
vin
=
vin
)
else
:
msgfmt
=
'
{{name}} is deprecated.
'
if
msg
is
not
None
:
msgfmt
=
msgfmt
+
'
'
+
msg
def
wrapper
(
thing
):
name
=
thing
.
__name__
def
decorator
(
*
args
,
**
kwargs
):
frame
=
inspect
.
stack
()[
1
]
ident
=
'
{}:{}
'
.
format
(
frame
.
filename
,
frame
.
lineno
)
if
ident
not
in
_warned_cache
:
warnings
.
warn
(
msgfmt
.
format
(
name
=
name
),
category
=
DeprecationWarning
,
stacklevel
=
2
)
_warned_cache
.
add
(
ident
)
return
thing
(
*
args
,
**
kwargs
)
return
ft
.
update_wrapper
(
decorator
,
thing
)
return
wrapper
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment