Skip to content

Commit 7fcae59

Browse files
add more parameters to transformation in create_decorator to make it more flexible
1 parent 6be03d6 commit 7fcae59

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## Pedantic 2.1.8
3+
- add more parameters to `transformation` in `create_decorator` to make it more flexible
4+
25
## Pedantic 2.1.7
36
- add `transformation` parameter to `create_decorator` to make adding custom behavior easier
47

docs/pedantic/mixins/with_decorated_methods.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h1 class="title">Module <code>pedantic.mixins.with_decorated_methods</code></h1
5252

5353
def create_decorator(
5454
decorator_type: DecoratorType,
55-
transformation: Callable[[C], C] = None,
55+
transformation: Callable[[C, DecoratorType, T], C] = None,
5656
) -&gt; Callable[[T], Callable[[C], C]]:
5757
&#34;&#34;&#34;
5858
Creates a new decorator that is parametrized with one argument of an arbitrary type.
@@ -66,7 +66,7 @@ <h1 class="title">Module <code>pedantic.mixins.with_decorated_methods</code></h1
6666
if transformation is None:
6767
return f
6868

69-
return transformation(f)
69+
return transformation(f, decorator_type, value)
7070

7171
return fun # we do not need functools.wraps, because we return the original function here
7272

@@ -132,7 +132,7 @@ <h1 class="title">Module <code>pedantic.mixins.with_decorated_methods</code></h1
132132
<h2 class="section-title" id="header-functions">Functions</h2>
133133
<dl>
134134
<dt id="pedantic.mixins.with_decorated_methods.create_decorator"><code class="name flex">
135-
<span>def <span class="ident">create_decorator</span></span>(<span>decorator_type: <a title="pedantic.mixins.with_decorated_methods.DecoratorType" href="#pedantic.mixins.with_decorated_methods.DecoratorType">DecoratorType</a>, transformation: Callable[[~C], ~C] = None) ‑> Callable[[~T], Callable[[~C], ~C]]</span>
135+
<span>def <span class="ident">create_decorator</span></span>(<span>decorator_type: <a title="pedantic.mixins.with_decorated_methods.DecoratorType" href="#pedantic.mixins.with_decorated_methods.DecoratorType">DecoratorType</a>, transformation: Callable[[~C<a title="pedantic.mixins.with_decorated_methods.DecoratorType" href="#pedantic.mixins.with_decorated_methods.DecoratorType">DecoratorType</a>, ~T], ~C] = None) ‑> Callable[[~T], Callable[[~C], ~C]]</span>
136136
</code></dt>
137137
<dd>
138138
<div class="desc"><p>Creates a new decorator that is parametrized with one argument of an arbitrary type.
@@ -143,7 +143,7 @@ <h2 class="section-title" id="header-functions">Functions</h2>
143143
</summary>
144144
<pre><code class="python">def create_decorator(
145145
decorator_type: DecoratorType,
146-
transformation: Callable[[C], C] = None,
146+
transformation: Callable[[C, DecoratorType, T], C] = None,
147147
) -&gt; Callable[[T], Callable[[C], C]]:
148148
&#34;&#34;&#34;
149149
Creates a new decorator that is parametrized with one argument of an arbitrary type.
@@ -157,7 +157,7 @@ <h2 class="section-title" id="header-functions">Functions</h2>
157157
if transformation is None:
158158
return f
159159

160-
return transformation(f)
160+
return transformation(f, decorator_type, value)
161161

162162
return fun # we do not need functools.wraps, because we return the original function here
163163

docs/pedantic/tests/test_with_decorated_methods.html

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ <h1 class="title">Module <code>pedantic.tests.test_with_decorated_methods</code>
9696

9797

9898
def test_with_custom_transformation(self):
99-
def my_transformation(f):
99+
def my_transformation(f, decorator_type, value):
100+
assert decorator_type == Decorators.BAR
101+
assert value == 42
102+
100103
@wraps(f)
101104
def wrapper(*args, **kwargs):
102105
f(*args, **kwargs)
@@ -264,7 +267,10 @@ <h3>Class variables</h3>
264267

265268

266269
def test_with_custom_transformation(self):
267-
def my_transformation(f):
270+
def my_transformation(f, decorator_type, value):
271+
assert decorator_type == Decorators.BAR
272+
assert value == 42
273+
268274
@wraps(f)
269275
def wrapper(*args, **kwargs):
270276
f(*args, **kwargs)
@@ -306,7 +312,10 @@ <h3>Methods</h3>
306312
<span>Expand source code</span>
307313
</summary>
308314
<pre><code class="python">def test_with_custom_transformation(self):
309-
def my_transformation(f):
315+
def my_transformation(f, decorator_type, value):
316+
assert decorator_type == Decorators.BAR
317+
assert value == 42
318+
310319
@wraps(f)
311320
def wrapper(*args, **kwargs):
312321
f(*args, **kwargs)

pedantic/mixins/with_decorated_methods.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DecoratorType(StrEnum):
2424

2525
def create_decorator(
2626
decorator_type: DecoratorType,
27-
transformation: Callable[[C], C] = None,
27+
transformation: Callable[[C, DecoratorType, T], C] = None,
2828
) -> Callable[[T], Callable[[C], C]]:
2929
"""
3030
Creates a new decorator that is parametrized with one argument of an arbitrary type.
@@ -38,7 +38,7 @@ def fun(f: C) -> C:
3838
if transformation is None:
3939
return f
4040

41-
return transformation(f)
41+
return transformation(f, decorator_type, value)
4242

4343
return fun # we do not need functools.wraps, because we return the original function here
4444

pedantic/tests/test_with_decorated_methods.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ async def m3(self) -> None:
6868

6969

7070
def test_with_custom_transformation(self):
71-
def my_transformation(f):
71+
def my_transformation(f, decorator_type, value):
72+
assert decorator_type == Decorators.BAR
73+
assert value == 42
74+
7275
@wraps(f)
7376
def wrapper(*args, **kwargs):
7477
f(*args, **kwargs)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def get_content_from_readme(file_name: str = 'README.md') -> str:
1515

1616
setup(
1717
name="pedantic",
18-
version="2.1.7",
18+
version="2.1.8",
1919
python_requires='>=3.11.0',
2020
packages=find_packages(),
2121
install_requires=[],

0 commit comments

Comments
 (0)