esciencecenter-digital-skills / cce

Carpentries MD Code Checker
MIT License
0 stars 2 forks source link

Code checker fails for correct code #4

Open dafnevk opened 2 years ago

dafnevk commented 2 years ago

The code checker seems to fail for code that is correct, see this issue. If I use cce with the -o option to write the code content to a file, and then run that file with python, it runs without error. It therefore seems that it goes wrong because cce uses exec(content). The failing line is inside a function in the code block, maybe that's relevant as well.

colinsauze commented 2 years ago

I've been having the same problem and found that it happens whenever I try to call an imported library from within a function. I think the problem is that it is already inside the main function in cce. I've been able to solve this by either placing the import inside the function I'm testing (but this results in some strange looking code that might confuse learners) or moving everything in the main function of cce to outside of a function.

What I'm not sure on is how best to package this in a way that pip/setuptools are happy with. They seem to want programs to have defined entry points that are a function.

For another lesson I'm developing (https://github.com/carpentries-incubator/machine-learning-novice-sklearn) I've taken the approach of not using pip to install and just downloading cce with wget into /usr/local/bin. This works but feels a bit of a hack.

For reference, attached is a minimal test example which fails when run with cce. test.md

colinsauze commented 2 years ago

For reference for my workaround:

Modified code with the main function removed: https://raw.githubusercontent.com/colinsauze/cce/master/cce/main.py

Example github action using the workaround:

https://github.com/carpentries-incubator/machine-learning-novice-sklearn/blob/90f4315e45851fa0303350ff0ac2ed7d9b3b948f/.github/workflows/codecheck.yml#L29