Skip to content

Commit 17565ff

Browse files
author
Sylvain MARIE
committed
@yaml_info can not be used on subclasses of YamlObject2 anymore as it could lead to an unexpected behaviour. Fixes #15
1 parent 1d0cae0 commit 17565ff

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

yamlable/main.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,14 @@ class Foo(YamlAble):
197197
raise ValueError("One non-None `yaml_tag` or `yaml_tag_ns` must be provided.")
198198

199199
if issubclass(cls, YamlObject2):
200-
if not yaml_tag.startswith('!'):
201-
raise ValueError("When extending YamlObject2, the `yaml_tag` field should contain the full yaml tag, "
202-
"and should therefore start with !")
203-
cls.yaml_tag = yaml_tag
200+
# Do not support this, because the `YamlObject` metaclass needs the tag to be present BEFORE this decorator is
201+
# even called. So if we are here, it means that we are trying to override a yaml_tag that was already registered
202+
# with pyyaml. Too late!
203+
raise TypeError("This is not supported")
204+
# if not yaml_tag.startswith('!'):
205+
# raise ValueError("When extending YamlObject2, the `yaml_tag` field should contain the full yaml tag, "
206+
# "and should therefore start with !")
207+
# cls.yaml_tag = yaml_tag
204208

205209
elif issubclass(cls, YamlAble):
206210
if yaml_tag.startswith('!'):

yamlable/tests/test_yamlobjects.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from copy import copy
22
try: # python 3.5+
33
from typing import Dict, Any
4-
from yamlable import Y
4+
from yamlable import Y, yaml_info
55
except ImportError:
66
pass
77

@@ -86,6 +86,14 @@ class AbstractFooE(YamlObject2):
8686
# e.dumps_yaml()
8787

8888

89+
def test_decorator():
90+
"""Test that the decorator can not be used when subclassing CustomFoo3"""
91+
with pytest.raises(TypeError):
92+
@yaml_info("!hello_dear")
93+
class CustomFoo3(YamlObject2):
94+
pass
95+
96+
8997
def test_abstract_parent():
9098
"""This tests that we can define an abstract parent class with the YamlAble behaviour and inherit it"""
9199

0 commit comments

Comments
 (0)