Skip to content

Commit 5058a6c

Browse files
Merge pull request #20 from eigenein/master
FIX: overridden FQN isn't propagated to the inner decorator instance
2 parents 7e9541a + b241bbb commit 5058a6c

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ python:
33
- "3.6"
44
- "3.7"
55
- "3.8"
6+
- "3.9"
67
install:
7-
- pip install codecov tox-travis
8+
- pip install --upgrade setuptools importlib-metadata codecov tox-travis
89
script: tox
910
after_success:
1011
- codecov

fqn_decorators/decorators.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,16 @@ def _ensure_decorator_instance(self, *args, **kwargs):
7070
if not self.func:
7171
# Decorator was initialized with arguments
7272
return self.__class__(args[0], **self.params), True
73-
return self.__class__(self.func, _initialized=True, **self.params)(*args, **kwargs), True
73+
74+
# The `self.func` at the caller site is replaced with the `self` decorator instance.
75+
# Should a user override `fqn` on it, the «inner» decorator created for the particular
76+
# function call will still see the FQN as returned by the global `get_fqn()` function.
77+
# In particular, that'd prevent an inherited decorator from seeing the overridden FQN
78+
# since it'd be lost in the «outer» decorator instance.
79+
inner_decorator = self.__class__(self.func, _initialized=True, **self.params)
80+
inner_decorator.fqn = self.get_fqn()
81+
return inner_decorator(*args, **kwargs), True
82+
7483
self.fqn = self.get_fqn()
7584
self.args = args
7685
self.kwargs = kwargs

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.black]
22
line-length = 120
3-
target_version = ["py36", "py37", "py38"]
3+
target_version = ["py36", "py37", "py38", "py39"]
44

55
[tool.isort]
66
combine_as_imports = true

requirements.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Package in develop mode
22
-e .
33

4-
# Latest tox (3.20.1 at the moment) requires importlib-metadata<3,>=0.12
5-
importlib-metadata<3
6-
74
# Distribution
85
twine
96
wheel
@@ -15,8 +12,11 @@ flake8
1512
black
1613

1714
# Tests
18-
tox==3.20.1
15+
tox
1916
pytest
2017
pytest-cov
2118
pytest-asyncio; python_version >= '3.5'
2219
mock
20+
21+
# Typing
22+
types-mock

tests/test_fqn_decorators.py

+16
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ def f(a=0, b=0):
197197
assert not f.kwargs
198198
assert f.result is None
199199

200+
def test_overridden_fqn_parameterless(self):
201+
"""
202+
Tests that the inherited decorator is able to retrieve the overridden FQN.
203+
"""
204+
205+
class Decorator(fqn_decorators.Decorator):
206+
def after(self):
207+
assert self.get_fqn() == "bar"
208+
209+
@Decorator
210+
def foo():
211+
pass
212+
213+
foo.fqn = "bar"
214+
foo()
215+
200216

201217
class TestChainedDecorator:
202218
def test_chaining(self):

tox.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
addopts=--tb=short
33

44
[tox]
5-
envlist = py36,py37,py38
5+
envlist = py36,py37,py38,py39
66

77
[testenv]
8+
download = true
89
passenv = *
910
install_command = pip install {opts} {packages}
1011
commands =

0 commit comments

Comments
 (0)