Skip to content

Commit

Permalink
Merge pull request #20 from eigenein/master
Browse files Browse the repository at this point in the history
FIX: overridden FQN isn't propagated to the inner decorator instance
  • Loading branch information
ricardosantosalves authored Mar 31, 2022
2 parents 7e9541a + b241bbb commit 5058a6c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ python:
- "3.6"
- "3.7"
- "3.8"
- "3.9"
install:
- pip install codecov tox-travis
- pip install --upgrade setuptools importlib-metadata codecov tox-travis
script: tox
after_success:
- codecov
Expand Down
11 changes: 10 additions & 1 deletion fqn_decorators/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ def _ensure_decorator_instance(self, *args, **kwargs):
if not self.func:
# Decorator was initialized with arguments
return self.__class__(args[0], **self.params), True
return self.__class__(self.func, _initialized=True, **self.params)(*args, **kwargs), True

# The `self.func` at the caller site is replaced with the `self` decorator instance.
# Should a user override `fqn` on it, the «inner» decorator created for the particular
# function call will still see the FQN as returned by the global `get_fqn()` function.
# In particular, that'd prevent an inherited decorator from seeing the overridden FQN
# since it'd be lost in the «outer» decorator instance.
inner_decorator = self.__class__(self.func, _initialized=True, **self.params)
inner_decorator.fqn = self.get_fqn()
return inner_decorator(*args, **kwargs), True

self.fqn = self.get_fqn()
self.args = args
self.kwargs = kwargs
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.black]
line-length = 120
target_version = ["py36", "py37", "py38"]
target_version = ["py36", "py37", "py38", "py39"]

[tool.isort]
combine_as_imports = true
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Package in develop mode
-e .

# Latest tox (3.20.1 at the moment) requires importlib-metadata<3,>=0.12
importlib-metadata<3

# Distribution
twine
wheel
Expand All @@ -15,8 +12,11 @@ flake8
black

# Tests
tox==3.20.1
tox
pytest
pytest-cov
pytest-asyncio; python_version >= '3.5'
mock

# Typing
types-mock
16 changes: 16 additions & 0 deletions tests/test_fqn_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ def f(a=0, b=0):
assert not f.kwargs
assert f.result is None

def test_overridden_fqn_parameterless(self):
"""
Tests that the inherited decorator is able to retrieve the overridden FQN.
"""

class Decorator(fqn_decorators.Decorator):
def after(self):
assert self.get_fqn() == "bar"

@Decorator
def foo():
pass

foo.fqn = "bar"
foo()


class TestChainedDecorator:
def test_chaining(self):
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
addopts=--tb=short

[tox]
envlist = py36,py37,py38
envlist = py36,py37,py38,py39

[testenv]
download = true
passenv = *
install_command = pip install {opts} {packages}
commands =
Expand Down

0 comments on commit 5058a6c

Please sign in to comment.