Comments (11)
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.
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.
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.
@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.
@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
tosrc
and execute themonkeytype
commands from there -
Or, add the
src
directory to yourPYTHONPATH
prior to running themonkeytype
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.
@bhrutledge oh, great! Now it works. Thank you very much!
from monkeytype.
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.
from monkeytype.
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.
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.
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.
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 amain()
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)
- Compare observed types to existing type annotations HOT 1
- Empty lists result in overly broad types HOT 1
- Issue with Callable type for < Python 3.9 HOT 3
- Better documentation for how to use MonkeyType with multiple processes/threads HOT 3
- Collect types for non-module functions HOT 1
- provide a type rewriter to find common base class HOT 5
- how open Instagram without password
- Adding an index to monkeytype.sqlite3 speeds up stub generation HOT 1
- Keyword-only arguments are not captured in traces
- Calls to locally defined functions are not captured
- `*args` and `**kwargs` are not captured in traces HOT 1
- Add pre-commit support HOT 1
- `AttributeError: 'ellipsis' object has no attribute '__qualname__'` HOT 1
- pep_563 flag creates imports that don't match the usage of the name HOT 4
- Support PEP 585 - list[int] instead of typing.List[int]
- Stubs with positional-only args output too many slashes and cause a SyntaxError
- Error when parameter in external file is class defined in `__main__` HOT 1
- Return type not traced when returning constant (Python ≥ 3.12)
- monkeytype apply seems to attempt to import my whole program, causing monkeytype: error: unrecognized arguments:
- checking instagrm password
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from monkeytype.