-
Notifications
You must be signed in to change notification settings - Fork 518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ConstructorError - could not determine a constructor for custom tag (5.1) #266
Comments
Try
I'm not sure though if this is intentional. |
It works with |
ok, I think I see it now. From Line 386 in e471e86
|
|
Today I encountered the same problem. Even the example straight from the documentation on the homepage does not work anymore #!/usr/bin/env python
import yaml
class Monster(yaml.YAMLObject):
yaml_tag = u'!Monster'
def __init__(self, name, hp, ac, attacks):
self.name = name
self.hp = hp
self.ac = ac
self.attacks = attacks
def __repr__(self):
return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % (
self.__class__.__name__, self.name, self.hp, self.ac, self.attacks)
yaml.unsafe_load("""
--- !Monster
name: Cave spider
hp: [2,6] # 2d6
ac: 16
attacks: [BITE, HURT]
""") Backtrace:
Edit: yaml.load(..., Loader=yaml.Loader) |
Yeah, the problem is that the default loader for load is now FullLoader, but the YAMLObject still uses yaml.Loader. #271 is similar. |
I created #273 |
@kgutwin, i am facing same issue: Python 2.7 Error: yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:python/name:pymdownx.emoji.to_svg' new_file.yml
parse.py
|
This is a breaking change, and is likely to break a ton of code out there that worked just fine: I spent myself a whole afternoon trying to figure out what I'd changed that broke all my tests. Are you guys going to revert changes, or provide a better error message (at the very least)? |
|
It worked like a charm for my
|
We just released 5.2b1 https://pypi.org/project/PyYAML/5.2b1/ which should fix this. |
None of the workarounds above worked for me, I had to use this loader: (edited) |
@maru-podmast do you have some code to reproduce? |
PS- this seems to work fine for me as of 5.2b1:
|
@maru-podmast sorry, I can't reproduce. the original code works for me with 5.2b1 and 5.2:
|
Released 5.2. Please reopen if necessary |
@perlpunk This isn't working for me using the example you provided. I'm seeing the same results as @maru-podmast. What are we missing? It does work with BaseLoader.
I'm on Python 3.7.4 |
@BonnieH @maru-podmast
Without that |
@perlpunk This was a misunderstanding on my part, I hadn't defined the Ref class in my example. So it works now! And my real code is working now too, thanks for your help. |
I'm using version 5.3 like so: import yaml
yaml.load("mgnl:variationOf: !weakreference 721ec351-e73a-489e-b467-e9d608468ebd", Loader=yaml.Loader) But I still got the same error.
I resolved the issue by following #266 (comment). yaml.load("mgnl:variationOf: !weakreference 721ec351-e73a-489e-b467-e9d608468ebd", Loader=yaml.BaseLoader) |
@ryanbrookepayne |
Same error for me. Worked using:
Python 3.8.2 |
The requirements.txt file has been updated with the "numpy<1.19". Version 1.18.5was the last to support Python 3.5, which we are using. The newer version causes conflicts. Also castro.py causes constructor error due to python pyyaml updates. This was working with pyyaml version 3.13, but not version 5.1 and above. It suggested that I add "Loader=yaml.Loader" to the yaml.load() function and it no longer breaks - checkout yaml/pyyaml#266.
The requirements.txt file has been updated with the "numpy<1.19". Version 1.18.5was the last to support Python 3.5, which we are using. The newer version causes conflicts. Also castro.py causes constructor error due to python pyyaml updates. This was working with pyyaml version 3.13, but not version 5.1 and above. It suggested that I add "Loader=yaml.Loader" to the yaml.load() function and it no longer breaks - checkout yaml/pyyaml#266.
Python 3.6.10 The error is resolved when I use |
Still seems to be true in mid 2022. |
Is there a workaround for ignoring the tags / refs when parsing? |
@vallamost I think the yaml.BaseLoader ignores 'em |
To ignore all tags (not yet defined) in SafeLoader:
|
Here is the solution which worked for me: class Ref(yaml.YAMLObject):
yaml_tag = '!Ref'
yaml_loader = yaml.SafeLoader
def __init__(self, value):
self.value = value
def __repr__(self):
return f'{type(self).__name__}(value={self.value!r})'
# print(yaml.dump({'Foo': Ref('bar')}, default_flow_style=False))
yaml.load('''Foo: !Ref
value: bar''', Loader=yaml.SafeLoader) |
…re found See yaml/pyyaml#266. The new warning also indicates if a dumped object uses internally an object of the namespace, that may not be dumped correctly (such as lambdas)
The following code was working on 3.13 but no longer works in 5.1:
I get the following exception on 5.1:
This is true whether I use
yaml.load
,yaml.full_load
oryaml.unsafe_load
.The text was updated successfully, but these errors were encountered: