nteract / papermill

📚 Parameterize, execute, and analyze notebooks
http://papermill.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
5.94k stars 427 forks source link

use `ast.literal_eval()` for `-p` params #396

Open casperdcl opened 5 years ago

casperdcl commented 5 years ago

-p seems to imply literal evaluation rather than raw string (-r), thus it should use ast.literal_eval()

Example

papermill ... -p a 1 -r b 2 -p c '[1, 2]'

Current behaviour

# Parameters
a = 1
c = "[1, 2]"
b = "2"

Desired output

# Parameters
a = 1
b = "2"
c = [1, 2]

Any reason why this should not be the case?

MSeal commented 5 years ago

The -y option allows for json/yaml input which includes expanded types. There is some oddities with literal eval in older versions of python (e.g. '1+1' is valid in python < 3.7). But otherwise it'd probably be fine to add that capability to -p. We'd want to be clear about such a change if we did it and bump to 1.1 as I'm pretty sure a number of users weren't aware of -y and pass json then json parse on the other end, and we'd want a minor version bump before changing that behavior.