glom icon indicating copy to clipboard operation
glom copied to clipboard

Improved error checking of "scope" param on Spec constructor.

Open antoine-gallix opened this issue 6 years ago • 4 comments

Each one of those two spec work separately.

from glom import Spec, T, glom

d = {'a': [1, {'b': 2}, 3], 'c': {'d': [4, 5]}}
spec = Spec(T['a'][1]['b'], T['c']['d'][1])
print(glom(d, spec))

antoine-gallix avatar Jan 19 '19 11:01 antoine-gallix

Ah, this is because the second argument to the Spec constructor is meant to be a "scope" (a concept not yet well-documented, but akin to function scope, but for specs). Since the T object is very permissive, it can cause some interesting error conditions when used outside of places that expect T.

I wonder if we can do any checking on the scope argument in the Spec constructor to fix this in future.

mahmoud avatar Jan 19 '19 19:01 mahmoud

And not to speculate too much, but if you wanted a tuple of these two values, I'd probably just do two separate glom alls for now. We're working on a design for a sequence builder specifier type, but for now two calls shouldn't be too bad, eh? :)

mahmoud avatar Jan 19 '19 19:01 mahmoud

Yes you got it. I was trying to solve https://github.com/mahmoud/glom/issues/77, then I stumble upon this case that make an infinite loop. Just wanted to signal an uncaught error case.

antoine-gallix avatar Jan 20 '19 19:01 antoine-gallix

Appreciate it! I'll leave this issue open in case we can improve the Scope error handling.

mahmoud avatar Jan 20 '19 19:01 mahmoud