Skip to content
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

Surprisingly, with_fallback() mutates parameter #95

Open
twm opened this issue Sep 9, 2016 · 0 comments
Open

Surprisingly, with_fallback() mutates parameter #95

twm opened this issue Sep 9, 2016 · 0 comments
Labels

Comments

@twm
Copy link

twm commented Sep 9, 2016

It's not in the docs proper, but with_fallback() is described in the readme:

Usage: config3 = config1.with_fallback(config2) or config3 = config1.with_fallback('samples/aws.conf')

I read this to mean that config3 is a new object, independent of config1 or config2—a copy.

That's not what happens, though. Actually, config3 is config2!

For example:

In [1]: from pyhocon import ConfigTree

In [2]: a = ConfigTree()

In [3]: a.put('foo.bar', ['a'])

In [4]: b = ConfigTree()

In [5]: b.put('foo.bar', ['b'])

In [6]: a
Out[6]: ConfigTree([('foo', ConfigTree([('bar', ['a'])]))])

In [7]: b
Out[7]: ConfigTree([('foo', ConfigTree([('bar', ['b'])]))])

In [8]: c = a.with_fallback(b)

In [9]: a
Out[9]: ConfigTree([('foo', ConfigTree([('bar', ['a'])]))])

In [10]: b
Out[10]: ConfigTree([('foo', ConfigTree([('bar', ['a'])]))])

In [11]: c
Out[11]: ConfigTree([('foo', ConfigTree([('bar', ['a'])]))])

In [12]: c is a
Out[12]: False

In [13]: c is b
Out[13]: True

I would expect a method on a to either return a copy of a, or mutate a (and return None). Mutating and returning b is quite surprising and at the least should be called out explicitly in the docs! Returning a new ConfigTree instance would be best, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants