.sort() keyword parameters are not passed properly
When using list.sort(key=my_key_function), the __kwargtrans__ gets overwritten in __sort__. I believe it will be the same for list.sort(reverse=True).
In transcrypt runtime.js:
Line:795
Array.prototype.py_sort = function () {
__sort__.apply (null, [this].concat ([] .slice.apply (arguments)));
};
Line:1934
var __sort__ = function (iterable, key, reverse) {
### Before:
### iterable == [], <-- OK
### key == __kwargtrans__, <-- OK
### reverse == undefined, <-- OK
### arguments == Array([ [], __kwargtrans__ ]) <-- OK
if (typeof key == 'undefined' || (key != null && key.hasOwnProperty ("__kwargtrans__"))) {;
var key = null;
};
### After:
### iterable == [], <-- OK
### key == null, <-- OK
### reverse == undefined, <-- OK
### arguments == Array([ [], null ]) <-- Woops
if (typeof reverse == 'undefined' || (reverse != null && reverse.hasOwnProperty ("__kwargtrans__"))) {;
var reverse = false;
};
I believe I just ran into this issue as well, though in my case it may be bundler related. Oddly enough, it works correctly with Parcel v1, but not with Parcel v2. There is no significant difference in the transpiled code that ends up in the JS bundle though, so the triggering issue in my case lies somewhere outside of Transcrypt. I'd put an issue in with Parcel, but I think the newer version of Parcel actually has the correct behavior (or at least is consistent with other JS environments).
If I understand how it's supposed to work correctly, this code:
export var __sort__ = function (iterable, key, reverse) {
if (typeof key == 'undefined' || (key != null && key.hasOwnProperty ("__kwargtrans__"))) {;
var key = null;
};
changes the value of the key parameter that was passed in, but it is not supposed to alter the value in the arguments object that holds the original values that may get used later on.
In testing the concept in JavaScript just in general with Node and in a web browser, I found that modifying the function parameter does indeed also affect the value in the arguments object. The exception being if the parameters have default values, in which case changing the value of the parameter does NOT change the value in the arguments object. I assume supplying the params with a default value causes them to be instantiated as local vars instead of refs.
That said, I would think that the fact that the variable key is being re-declared in this case, that it should still work regardless. But that may be a JS quirk because using let or const instead of var to redefine it creates a syntax error. This leads me to believe that it is not actually getting re-defined.
Perhaps using explicitly different variables would make it more reliable:
export var __sort__ = function (iterable, sortkey, reverse) {
if (typeof key == 'undefined' || (sortkey != null && sortkey.hasOwnProperty ("__kwargtrans__"))) {;
var key = null;
else {
var key = sortkey;
};
or, the key and reverse parameters could also have default values defined which may solve the issue as well:
export var __sort__ = function (iterable, key=null, reverse=false) {
It took me a while to narrow all this down so I hope it helps someone.
This has been fixed in the dev_3.9.3 branch to be released in v3.9.3