-
Notifications
You must be signed in to change notification settings - Fork 164
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
Specify whether **kwargs is frozen #295
Comments
Note that we cannot deeply freeze |
I think it makes sense to have kwargs mutable, and it doesn't seem worth making a breaking change here (unless someone finds a better rationale?) |
(Weak) rationale for thinking about this:
|
If kwargs is immutable, we can avoid copying.
We could allocate kwargs statically (when optimizer knows the signature of |
The fix in cf94101 made an empty **kwargs immutable when initialized via positionalOnlyCall. Unfortunately, that was an incompatible change (although unlikely to affect anything in practice given how positionalOnlyCall is currently used): the Starlark language spec does not specify whether **kwargs is mutable, but **kwargs is mutable in Python 3 and was always mutable previously in the Starlark interpreter. See bazelbuild/starlark#295 - perhaps it ought to be immutable in Starlark, but that requires a language spec change and an incompatible flag. PiperOrigin-RevId: 712920880 Change-Id: I151bb29e9646b82def8944143198f3dffb398583
Right, it would be shallow only, like how tuples are always at least shallowly immutable.
Yes, if we did it we'd have to make sure that we don't confuse a shallowly immutable dict with a deeply frozen one. (Which makes me realize that our constructors in the Java code for creating frozen dicts/lists might be misused with mutable elements / entries. Or at least, the javadoc doesn't caution against that strongly enough.)
Now that you mention this, this is super common and I've done it myself plenty of times in my limited macro-writing experience. So I think this is a show-stopper for immutable
Yes, but it's Python's wart rather than ours. Then again, Python doesn't have a concept of pervasive freezing, and if we were doing it all over from scratch it wouldn't necessarily be that strange to say that **kwargs's value is a I'm not too moved by the optimization / GC argument, since I think that's secondary to use cases like macro authors mutating |
Discussed offline and we agreed that kwargs should remain mutable. It looks only a very small percentage of .bzl files match a regex looking for assignments to Also, the hypothetical GC improvement only applies to functions that declare a This issue can stay open until the spec is clarified. |
Python lets you mutate
**kwargs
inside the function, but not*args
since it's a tuple.The Java implementation currently lets you mutate
**kwargs
, though a recent change at head removes this ability for certain types of calls. We should fix that for consistency, but also specify it here in the spec.cc @tetromino
The text was updated successfully, but these errors were encountered: