Giter Site home page Giter Site logo

Comments (11)

carljm avatar carljm commented on May 17, 2024 2

Yeah, when you run myscript.py as a script, its module name is __main__; when you run monkeytype stub myscript, its module name is myscript. It may be possible to hack around this by mangling the name of traces recorded with module __main__ (if we can figure out the actual module name for the __main__ module, which might not be easy to do reliably). I have mixed feelings about it; monkeytype needs to import the modules it annotates, and importing a script that isn't carefully written could have unintended side effects. I think I'd rather just have MonkeyType clearly intended for annotating the modules used by the script; if you want to annotate myscript.py itself, treat it as a module and make another short script that imports and calls it, and run monkeytype with that script.

For now I'll add a documentation note to that effect. But I'll leave this open as a low-pri enhancement; if someone submits a PR that implements it in a way that looks robust, I'd consider it.

from monkeytype.

carljm avatar carljm commented on May 17, 2024 2

Doc clarification added in de7868a

Thanks for the report! This was definitely an oversight in the documentation, it wasn't at all clear which code monkeytype run was intended to trace.

from monkeytype.

bhrutledge avatar bhrutledge commented on May 17, 2024 1

if you want to annotate myscript.py itself, treat it as a module and make another short script that imports and calls it, and run monkeytype with that script.

Here's a simple but concrete example of this workaround:

https://github.com/bhrutledge/til/blob/main/python/monkeytype-script.md

from monkeytype.

 avatar commented on May 17, 2024

@bhrutledge maybe this workaround not working anymore..?

I have now this,

./
├── src/
│  └── sort.py
└── run_script.py

run_script.py

import sys
sys.path.insert(1,'./src')
import sort
sort.main()

## this also not work
# from src import sort
# sort.main()

then, monkeytype run run_script.py run correcty.

however,

❯ monkeytype -v stub sort
WARNING: Failed decoding trace: No module named 'sort'
WARNING: Failed decoding trace: No module named 'sort'
No traces found for module sort

stub and apply causes error.

from monkeytype.

bhrutledge avatar bhrutledge commented on May 17, 2024

@Ryuta69 monkeytype stub is failing because the src directory isn't in the module search path, so it can't find the sort module. Instead of modifying the path in run_script.py, you could:

  • Move run_script.py to src and execute the monkeytype commands from there

  • Or, add the src directory to your PYTHONPATH prior to running the monkeytype commands, e.g.

$ PYTHONPATH=src:$PYTHONPATH

$ monkeytype run run_script.py

$ monkeytype stub sort

Here's an example of the second option: https://repl.it/@bhrutledge/monkeytyperunscript

from monkeytype.

 avatar commented on May 17, 2024

@bhrutledge oh, great! Now it works. Thank you very much!

from monkeytype.

dgutson avatar dgutson commented on May 17, 2024

What about adding an option so MonkeyType creates a temp python file turning the "main" into a module? That is, to "automate" the workaround and no need for any bash.
Maybe some python parsing is needed to create the temp calling file.

@arieltorti

from monkeytype.

carljm avatar carljm commented on May 17, 2024

What about adding an option so MonkeyType creates a temp python file turning the "main" into a module?

I'm open to something like this in principle as a resolution of this ticket, but I'm not clear how it should work in general. Does it assume that the script module has a main function to call? What if that function takes arguments?

It really seems like the "runner" script has to be created with some knowledge of the script itself.

from monkeytype.

dgutson avatar dgutson commented on May 17, 2024

I think it should parse the file and look for __main__ (the typical if...) and forward the arguments if provided in the cli. This will in term be a validation. In any case I'm convinced that a minimum static analysis is needed.

from monkeytype.

carljm avatar carljm commented on May 17, 2024

I think it should parse the file and look for main (the typical if...) and forward the arguments if provided in the cli.

I'm not sure what this means; we can't "call" an if __name__ == "__main__": clause from another module, so the only way this approach works is if that clause simply calls a main() or similar function, which we can import and call.

TBH the whole thing sounds quite finicky and unreliable to me; I'm not going to work on it, but if somebody has a concrete plan I'm willing to evaluate whether it seems workable and potentially look at a PR.

from monkeytype.

dgutson avatar dgutson commented on May 17, 2024

I think it should parse the file and look for main (the typical if...) and forward the arguments if provided in the cli.

I'm not sure what this means; we can't "call" an if __name__ == "__main__": clause from another module, so the only way this approach works is if that clause simply calls a main() or similar function, which we can import and call.

This was exactly my "parsing the code" proposal.

TBH the whole thing sounds quite finicky and unreliable to me; I'm not going to work on it, but if somebody has a concrete plan I'm willing to evaluate whether it seems workable and potentially look at a PR.

great! let me bring somebody ;)

from monkeytype.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.