-
Notifications
You must be signed in to change notification settings - Fork 507
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
add_implicit_resolver does not match quoted strings #457
Comments
I guess this is described in the YAML spec:
Since quoted strings are non-plain scalars, the spec suggests you do not do matching. This is also part of the PyYAML documentation that states that The YAML spec does go on to say:
It seems like we should be allowed to have a resolver that will tag explicit nodes. |
@att14 Did you find a solution? |
When a yaml loader loads a yaml stream there are these phases under the hood: BTW Try Every node created by the composer is assigned a tag; either the explicit one or an implicit one. Untagged non-plain (quoted) scalars are are assigned The end result in pyyaml is that non-plain scalars are not available for implicit tag resolution since you effectively assigned them a tag by quoting them. PyYAML lets you do this:
but I don't think that complies with the 1.2 spec. This is PyYAML's way of saying treat this quoted thing as unquoted and allow implicit tag resolution. I am in favor of adding this to the spec in a future version of yaml. In your case there was no need to quote
Since plain scalars may not start with The spec allows implicit resolution not just by pattern matching but also by path position. You should be able to use a loader so that:
would always resolve the Unfortunately most current YAML implementations don't offer that kind of fine grained control yet. |
Just to expand on this: When a node is parsed, if it doesn't have an explicit tag, it's assigned one of the two “non-specific tags”. Non-plain scalars (i.e. quoted scalars or block scalars) are assigned the Tag resolution is the process of taking each node with a non-specific tag and assigning it a specific tag. According to the spec:
These are both “should” requirements, so it's valid for an application to resolve the |
Thanks for the clarification. I was reading the specification before and have a related question regarding verbatim tags:
If a verbatim tag must be delivered as-is-to the application and without tag resolution why can't a node then not have multiple tags? Tags is this case doesn't resolve to a datatype per se. E.g. description: !force-inherit !tag2 some string |
It's a fundamental part of the YAML representation model that a node has exactly one tag. This one tag is used to determine the canonical form of scalar content and is used by implementations to choose what native types to construct. For example, the node So I'm not sure how to interpret the example Are you by chance using Would you mind explaining your use case in a bit more detail? |
Of course I can. Like you guessed I thought of using tags as a sort of annotation. I saw that the @ character has been reserved in the YAML specification. I just got involved in the project and we are looking at a version 3 of a YAML config file for GitLabForm [https://github.com/gdubicki/gitlabform/blob/main/config.yml] to a accomodate a bunch of user requests. Use-case:
|
As @Thom1729 said
Tags themselves are annotation strings that can be used by the composition/construction process to configure that process. For example: the This is the PyYAML code that implicitly tags plain scalar values matching certain patterns: to the tag To tell the constructor to transform the canonical form of the scalar using this function: pyyaml/lib/yaml/constructor.py Line 233 in 8cdff2c
to make the python boolean native value. The process of turning a node into a canonical form is not well abstracted or exposed by pyyaml. It is easy to apply multiple functions (tags) to a node's construction, by wrapping calls in sequences.
it also makes the order of operations visually clear. On the subject of tag resolution and canonical form, this is usually seen in the context of scalars but you can also do this with collections. For instance you shouldn't have to tag
and the normalization should be equally powerful. This should be able to produce the same:
If a schema dictates that a given node in the graph is required to have the tag A yaml schema is the entity that defines the tags available to a load operation and how the implicit resolution and canonicalization should work. A schema should be able to be written as a yaml data file and exposed to a YAML API like pyyaml's via something like:
We're not there yet, but we're working on it. |
There's no reason you couldn't do this now using:
where the function you register to Ruamel is a fork of PyYAML. I just took a quick look and all the resolver and constructor functions I mentioned in the previous comment are available in ruamel too. |
* Quoted string in pyyaml are automatically tagged as strings and would bypass the conversion to ipaddress objects. * Ensure that the special syntax using ! or !TAGNAME works and allows to parse as ipaddresses also quoted strings. * See also: yaml/pyyaml#457 Change-Id: I17ccda008f7e1250d293d014c6a9043d0c347984
Any workaround for this issue ? |
IIRC it's not a workaround because it technically the correct implementation. But the solution is in #457 (comment). Use a
|
Maybe this is something I don't understand about the YAML spec, but if you explicitly tag the value it works. I initially thought it was #294, but I am using PyYAML 5.3.1 with Python 3.8.6.
The text was updated successfully, but these errors were encountered: