Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## Pedantic 2.1.7
- add `transformation` parameter to `create_decorator` to make adding custom behavior easier

## Pedantic 2.1.6
- Remove `inspect.getsource()` call from `@overrides`

Expand Down
37 changes: 29 additions & 8 deletions docs/pedantic/mixins/with_decorated_methods.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,23 @@ <h1 class="title">Module <code>pedantic.mixins.with_decorated_methods</code></h1
C = TypeVar(&#39;C&#39;, bound=Callable)


def create_decorator(decorator_type: DecoratorType) -&gt; Callable[[T], Callable[[C], C]]:
&#34;&#34;&#34; Creates a new decorator that is parametrized with one argument of an arbitrary type. &#34;&#34;&#34;
def create_decorator(
decorator_type: DecoratorType,
transformation: Callable[[C], C] = None,
) -&gt; Callable[[T], Callable[[C], C]]:
&#34;&#34;&#34;
Creates a new decorator that is parametrized with one argument of an arbitrary type.
You can also pass an arbitrary [transformation] to add custom behavior to the decorator.
&#34;&#34;&#34;

def decorator(value: T) -&gt; Callable[[C], C]:
def fun(f: C) -&gt; C:
setattr(f, decorator_type, value)
return f

if transformation is None:
return f

return transformation(f)

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

Expand Down Expand Up @@ -122,21 +132,32 @@ <h1 class="title">Module <code>pedantic.mixins.with_decorated_methods</code></h1
<h2 class="section-title" id="header-functions">Functions</h2>
<dl>
<dt id="pedantic.mixins.with_decorated_methods.create_decorator"><code class="name flex">
<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>) ‑> Callable[[~T], Callable[[~C], ~C]]</span>
<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>
</code></dt>
<dd>
<div class="desc"><p>Creates a new decorator that is parametrized with one argument of an arbitrary type.</p></div>
<div class="desc"><p>Creates a new decorator that is parametrized with one argument of an arbitrary type.
You can also pass an arbitrary [transformation] to add custom behavior to the decorator.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def create_decorator(decorator_type: DecoratorType) -&gt; Callable[[T], Callable[[C], C]]:
&#34;&#34;&#34; Creates a new decorator that is parametrized with one argument of an arbitrary type. &#34;&#34;&#34;
<pre><code class="python">def create_decorator(
decorator_type: DecoratorType,
transformation: Callable[[C], C] = None,
) -&gt; Callable[[T], Callable[[C], C]]:
&#34;&#34;&#34;
Creates a new decorator that is parametrized with one argument of an arbitrary type.
You can also pass an arbitrary [transformation] to add custom behavior to the decorator.
&#34;&#34;&#34;

def decorator(value: T) -&gt; Callable[[C], C]:
def fun(f: C) -&gt; C:
setattr(f, decorator_type, value)
return f

if transformation is None:
return f

return transformation(f)

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

Expand Down
99 changes: 97 additions & 2 deletions docs/pedantic/tests/test_with_decorated_methods.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h1 class="title">Module <code>pedantic.tests.test_with_decorated_methods</code>
<span>Expand source code</span>
</summary>
<pre><code class="python">import unittest
from functools import wraps

from pedantic import DecoratorType, create_decorator, WithDecoratedMethods

Expand Down Expand Up @@ -91,7 +92,35 @@ <h1 class="title">Module <code>pedantic.tests.test_with_decorated_methods</code>
instance.m3: 44,
}
}
assert instance.get_decorated_functions() == expected</code></pre>
assert instance.get_decorated_functions() == expected


def test_with_custom_transformation(self):
def my_transformation(f):
@wraps(f)
def wrapper(*args, **kwargs):
f(*args, **kwargs)
return 4422 # we add a return value

return wrapper

my_decorator = create_decorator(decorator_type=Decorators.BAR, transformation=my_transformation)

class MyClass(WithDecoratedMethods[Decorators]):
@my_decorator(42)
def m1(self) -&gt; int:
return 1

instance = MyClass()
expected = {
Decorators.BAR: {
instance.m1: 42,
},
Decorators.FOO: {},
}
assert instance.get_decorated_functions() == expected

assert instance.m1() == 4422 # check that transformation was applied</code></pre>
</details>
</section>
<section>
Expand Down Expand Up @@ -231,14 +260,79 @@ <h3>Class variables</h3>
instance.m3: 44,
}
}
assert instance.get_decorated_functions() == expected</code></pre>
assert instance.get_decorated_functions() == expected


def test_with_custom_transformation(self):
def my_transformation(f):
@wraps(f)
def wrapper(*args, **kwargs):
f(*args, **kwargs)
return 4422 # we add a return value

return wrapper

my_decorator = create_decorator(decorator_type=Decorators.BAR, transformation=my_transformation)

class MyClass(WithDecoratedMethods[Decorators]):
@my_decorator(42)
def m1(self) -&gt; int:
return 1

instance = MyClass()
expected = {
Decorators.BAR: {
instance.m1: 42,
},
Decorators.FOO: {},
}
assert instance.get_decorated_functions() == expected

assert instance.m1() == 4422 # check that transformation was applied</code></pre>
</details>
<h3>Ancestors</h3>
<ul class="hlist">
<li>unittest.case.TestCase</li>
</ul>
<h3>Methods</h3>
<dl>
<dt id="pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods.test_with_custom_transformation"><code class="name flex">
<span>def <span class="ident">test_with_custom_transformation</span></span>(<span>self)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def test_with_custom_transformation(self):
def my_transformation(f):
@wraps(f)
def wrapper(*args, **kwargs):
f(*args, **kwargs)
return 4422 # we add a return value

return wrapper

my_decorator = create_decorator(decorator_type=Decorators.BAR, transformation=my_transformation)

class MyClass(WithDecoratedMethods[Decorators]):
@my_decorator(42)
def m1(self) -&gt; int:
return 1

instance = MyClass()
expected = {
Decorators.BAR: {
instance.m1: 42,
},
Decorators.FOO: {},
}
assert instance.get_decorated_functions() == expected

assert instance.m1() == 4422 # check that transformation was applied</code></pre>
</details>
</dd>
<dt id="pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods.test_with_decorated_methods_async"><code class="name flex">
<span>def <span class="ident">test_with_decorated_methods_async</span></span>(<span>self)</span>
</code></dt>
Expand Down Expand Up @@ -339,6 +433,7 @@ <h4><code><a title="pedantic.tests.test_with_decorated_methods.Decorators" href=
<li>
<h4><code><a title="pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods" href="#pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods">TestWithDecoratedMethods</a></code></h4>
<ul class="">
<li><code><a title="pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods.test_with_custom_transformation" href="#pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods.test_with_custom_transformation">test_with_custom_transformation</a></code></li>
<li><code><a title="pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods.test_with_decorated_methods_async" href="#pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods.test_with_decorated_methods_async">test_with_decorated_methods_async</a></code></li>
<li><code><a title="pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods.test_with_decorated_methods_sync" href="#pedantic.tests.test_with_decorated_methods.TestWithDecoratedMethods.test_with_decorated_methods_sync">test_with_decorated_methods_sync</a></code></li>
</ul>
Expand Down
Loading