Skip to content

Commit d1f9097

Browse files
authored
Merge pull request #1215 from appwrite/PLA-3382
feat: add `DeprecationWarning` decorator support Python language
2 parents 0721992 + 5585eb9 commit d1f9097

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

src/SDK/Language/Python.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ public function getFiles(): array
125125
'destination' => '{{ spec.title | caseSnake}}/__init__.py',
126126
'template' => 'python/package/__init__.py.twig',
127127
],
128+
[
129+
'scope' => 'default',
130+
'destination' => '{{ spec.title | caseSnake}}/utils/deprecated.py',
131+
'template' => 'python/package/utils/deprecated.py.twig',
132+
],
133+
[
134+
'scope' => 'default',
135+
'destination' => '{{ spec.title | caseSnake}}/utils/__init__.py',
136+
'template' => 'python/package/utils/__init__.py.twig',
137+
],
128138
[
129139
'scope' => 'default',
130140
'destination' => '{{ spec.title | caseSnake}}/client.py',

templates/python/package/services/service.py.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ..service import Service
22
from typing import List, Dict, Any
33
from ..exception import AppwriteException
4+
from appwrite.utils.deprecated import deprecated
45
{% set added = [] %}
56
{% for method in service.methods %}
67
{% for parameter in method.parameters.all %}
@@ -39,6 +40,13 @@ class {{ service.name | caseUcfirst }}(Service):
3940
{% endif %}
4041
{% if not shouldSkip %}
4142

43+
{% if method.deprecated %}
44+
{% if method.since and method.replaceWith %}
45+
@deprecated("This API has been deprecated since {{ method.since }}. Please use `{{ method.replaceWith | caseSnakeExceptFirstDot }}` instead.")
46+
{% else %}
47+
@deprecated("This API has been deprecated.")
48+
{% endif %}
49+
{% endif %}
4250
def {{ method.name | caseSnake }}(self{% if method.parameters.all|length > 0 %}, {% endif %}{% for parameter in method.parameters.all %}{{ parameter.name | escapeKeyword | caseSnake }}: {{ parameter | getPropertyType(method) | raw }}{% if not parameter.required %} = None{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, on_progress = None{% endif %}) -> {% if method.type == 'webAuth' %}str{% elseif method.type == 'location' %}bytes{% else %}Dict[str, Any]{% endif %}:
4351
"""
4452
{% autoescape false %}{{ method.description | replace({"\n": "\n "}) }}{% endautoescape %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file makes the 'utils' directory a Python package.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
A decorator to mark functions as deprecated.
3+
4+
When the function is called, a DeprecationWarning is emitted.
5+
Compatible with Python 2.7+ and Python 3.x.
6+
"""
7+
8+
import functools
9+
import warnings
10+
11+
def deprecated(reason=None):
12+
"""
13+
Decorator to mark functions as deprecated.
14+
Emits a DeprecationWarning when the function is called.
15+
16+
Args:
17+
reason (str, optional): Reason for deprecation. Defaults to None.
18+
19+
Usage:
20+
@deprecated("Use another_function instead.")
21+
def old_function(...):
22+
...
23+
"""
24+
def decorator(func):
25+
message = "Call to deprecated function '{}'.{}".format(
26+
func.__name__,
27+
" " + reason if reason else ""
28+
)
29+
30+
@functools.wraps(func)
31+
def wrapped(*args, **kwargs):
32+
warnings.simplefilter('always', DeprecationWarning) # show warning every time
33+
try:
34+
warnings.warn(
35+
message,
36+
category=DeprecationWarning,
37+
stacklevel=2
38+
)
39+
return func(*args, **kwargs)
40+
finally:
41+
warnings.simplefilter('default', DeprecationWarning) # reset filter
42+
return wrapped
43+
44+
# Support both @deprecated and @deprecated("reason")
45+
if callable(reason):
46+
# Used as @deprecated without arguments
47+
func = reason
48+
reason = None
49+
return decorator(func)
50+
else:
51+
return decorator

templates/python/setup.py.twig

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ long_description: str
44

55
with open("README.md", "r", encoding="utf-8") as readme_file_desc:
66
long_description = readme_file_desc.read()
7-
7+
88
setuptools.setup(
99
name = '{{spec.title | caseSnake}}',
10-
packages = [
11-
'{{spec.title | caseSnake}}',
12-
'{{spec.title | caseSnake}}/services',
13-
'{{spec.title | caseSnake}}/encoders',
14-
'{{spec.title | caseSnake}}/enums',
15-
],
10+
packages = setuptools.find_packages(),
1611
version = '{{sdk.version}}',
1712
license='{{spec.licenseName}}',
1813
description = '{{sdk.shortDescription}}',

0 commit comments

Comments
 (0)