Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

[Discussion][Enhancement] Argument handling in natively implemented Python functions (or classes) #579

Open
abonie opened this issue Jun 22, 2017 · 0 comments

Comments

@abonie
Copy link
Contributor

abonie commented Jun 22, 2017

While working on native implementation of json module I came across few issues:

  1. There is no way to implement Python class in JavaScript so that its constructor can take keyword arguments. It is possible for regular functions, but not that easy to figure out how to do it.

https://github.com/pybee/batavia/blob/master/batavia/core/native.js#L5 here is relevant piece of code from Batavia that wraps functions with property $pyargs set to true so that they receive an array with positional arguments and an object with keyword arguments. It is useable, but not present in documentation. Builtin functions have their $pyargs set automatically. As you can see in the code, kwargs are discarded for classes and functions without $pyargs.
IMO, at the very least, the $pyargs thing should be mentioned in documentation and there should be an option to get kwargs in class ctor.

  1. There is no tool in place, that could check whether a function was invoked with an appropriate number of parameters etc.

In Python an error will be raised when a function is called with too few or too many arguments, or multiple values for single arguments are given. JavaScript on the other hand will gladly accept more/less arguments (than in function's signature) and has no support for named parameters. As a result I found myself writing similar code to validate arguments passed to a function. I ended up creating a more generic parameter validation function, it is not ideal, but I am linking it for reference: https://github.com/pybee/batavia/blob/master/batavia/modules/json/utils.js. Such function could be invoked "manually" or somehow "automagically" called for certain functions, possibly in getattr (https://github.com/pybee/batavia/blob/master/batavia/core/native.js#L5) (I prefer the more explicit approach).
Or perhaps there is a better solution, @freakboy3742 mentioned that VOC has some sort of function annotations that specify all the details about positional and named arguments and perform validation. I don't know my way around VOC so I couldn't find it, but hopefully Russell can link relevant code.

Any thoughts? Any takers?

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

No branches or pull requests

1 participant