-
Notifications
You must be signed in to change notification settings - Fork 204
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 :symbolize_names option to .safe_load too #337
Conversation
Thanks a lot!
Sounds reasonable. |
Indeed causes the mixing of optional positional arguments and optional keyword arguments problems, see #340. I would argue that calling Psych.load with |
You mean calling {}.to_ruby? It's not handled explicitly and I don't think it's a kind of feature which was maintained. This PR doesn't break existing working application unlike #339 (comment) and is less harmful. Changing or unifying fallback interface is totally separated issue. Anyway, my opinion is here #339 (comment) |
See also #339. Short version: |
I agree for that point. Obviously it was already an existing problem (wether it's a bug or not depends on intention of the person who implemented. It's not necessarily a bug if the behavior is intentional), but I admit adding an option to only some of them would make the matters worse. To be clear, my comment in the PR is NOT against the point. I'm in favor of unifying interfaces, but using keyword arguments for future extensions. |
In the issues related to the fallback option (#149, #264) the single point was to provide a possibility to specify a fallback. There was never any hint that The current situation: someone who wants symbolized keys must use I'm all for using keyword arguments, but in this case either the new option needs to be implemented differently, or older behavior changed (fallback as keyword argument, if that doesn't cause problems in other methods). I'd rather prefer the latter option, if it's acceptable to break compatibility. BTW, this is not about unifying interfaces, it's about keeping the interfaces unified. All those methods ( |
Why do you think you can't use Psych.load(yaml, nil, Psych::FALLBACK.new({})) ? That's the correct usage from previous versions. |
That's certainly not the intended usage: Psych.load_file("empty.yml", {})
# versus:
Psych.load(yaml, nil, Psych::FALLBACK.new({})) # ???? That would be crazy. Psych::FALLBACK is an implementation detail and not part of the public API. It's explicitly not documented. Having the library user care about wrapping his fallback value would be the worst interface design ever. Please have another look at the two fallback issues and the code and evaluate what is more probable: an extremely strange interface relying on the user to care about internal helper structs, a behaviour that was never mentioned or discussed anywhere in the PRs, or a clean, similar interface for the two methods (which essentially do exactly the same thing) and a simple bug, caused by wrapping at the wrong place. |
suggested at #333 (comment)
Summary
I added the same thing as #333 only for
.safe_load
.Notes
For
.load_file
, as it may take Hash object in existing arguments, we can't add:symbolize_names
to it keeping backward compatibility (reason is described below).The reason I didn't add the option to
Psych.load_file
Why adding
:symbolize_names
toPsych.load_file
is incompatible?before
after
After adding
symbolize_names: false
to.load_file
signature,{}
is considered as empty keyword arguments, and second argument ofPsych.load_file
becomesfalse
.Why adding it to
Psych.load
andPsych.safe_load
is safe?That's because
Psych.load
andPsych.safe_load
don't take Hash object for normal usages.Unlike
Psych.load_file
,fallback
argument ofPsych.load
is NOT wrapped withPsych::FALLBACK
. Thus, if you pass{}
asfallback
,{}.to_ruby
will be called and it will be broken. So this method can't take{}
(We should passPsych::FALLBACK.new({})
if we want to have{}
as fallback).