7 ms·
MonkeyType: A system for Python that automatically generates type annotations
- asplake 7y agoNice! I see some Django-related FAQs. Anyone used this with Flask?
- Bnshsysjab 7y agoIt should be relatively straight forward, it deduces typing based on your unit tests and CI.
- Epskampie 7y agoI think I’ve seen several of these now for python, seems very useful. I hope a good one will be made soon for javascript/typescript. Or maybe it’s needed less there because of typescripts type interference?
- smt88 7y agoYou’re correct that TS doesn’t need it due to its excellent type inference
- heavenlyblue 7y agoWhat are your thoughts on purely static tools on that for Python? Basically my latest project that I was thinking about is to write a symbolic executor for python that would simply preserve all of the required-type information for all of the execution paths of the program. Also the other project is to write a tool that allows you to walk all of the possible executions of a given piece of code to find which functions are being called from it - incredibly useful for refactoring of the old code. Does something like this exists already that you've used and found useful?
- BiteCode_dev 7y agoI'm not sure, but maybe you can have a look at the following projects to have an idea of prior art: - jedi: https://github.com/davidhalter/jedi https://github.com/davidhalter/jedi - nuitka: nuitka.net/ - python language server: https://github.com/palantir/python-language-server https://github.com/palantir/python-language-server - pyright: https://github.com/microsoft/pyright https://github.com/microsoft/pyright - pyre: https://pyre-check.org/ https://pyre-check.org/ - and of course mypy: http://mypy-lang.org/ http://mypy-lang.org/ I don't think any of them qualify, but they may all have little pieces. Also, if you need to analyze Python syntax, baron will help: https://pypi.org/project/baron/ https://pypi.org/project/baron/ Good luck.
- carapace 7y agoGreat list! There's also something called PySonar but you have to dig around for it.
- chrisseaton 7y ago> simply preserve all of the required-type information for all of the execution paths of the program A type-flow analysis? Due to Python language features such a meta-programming and runtime code execution, you may find that at the end of this all your reported types will come out as 'Any' and the tool will be useless!
- heavenlyblue 7y agoa) No they won’t come out as Any. Most of them won’t. b) You’re doing metaprogramming wrong if it goes against the assumptions of the initial language you started from.
- chrisseaton 7y agoWhat depth of context are you expecting to have to use to avoid megamorphisation? The literature tells us more than a couple of levels is likely not tractable. That’s not going to enough is it? I'm not an expert in Python, but I am in dynamic analysis of other similar languages. Python lets you redefine methods, doesn't it? How do you accomodate the fact that while your routine is running someone could be redefining all your methods on another thread? Nobody's yet managed what you're suggesting being able to do. Maybe you've got some new idea that nobody's even thought of before?
- heavenlyblue 7y ago>> How do you accomodate the fact that while your routine is running someone could be redefining all your methods on another thread? You accommodate that by not doing that because you’re not an idiot. It’s like asking “why do you need static analysis tools when at any point in time your RAM bit can flip and now you need to model that, too”.
- chrisseaton 7y ago> You accommodate that by not doing that because you’re not an idiot. There's no need to be abusive. > It’s like asking “why do you need static analysis tools when at any point in time your RAM bit can flip and now you need to model that, too”. It's not like that, because your language semantic model doesn't include random bit flips, so you can ignore that as hardware failure. But your language semantic model does include redefining methods on a concurrent thread. So you need to handle that. Otherwise you aren't statically typing Python, you're statically typing a new language that looks a bit like Python, but really is something else.
- tln 7y agoI wrote something like this. It used the bytecode, simulating types on the stack. Functions with annotations had to be called with compatible types, functions without annotations were symbolically executed for each call site. Constructors were special cased so that the object attributes were set up correctly. Types for a stack slot or local could diverge and be narrowed depending on the path through the program. The amount of metaprogramming used in practice in python made using it for real programs feel insurmountable. Also, I'm pretty sure performance would have been a problem beyond the toy programs I got it working on. But it was a fun exercise. I'll have to dig up that code
- garrettgrimsley 7y agoPrevious discussion: https://news.ycombinator.com/item?id=15982390 https://news.ycombinator.com/item?id=15982390
- rajbiswas125 7y agoWould python finally become statically typed?
- BiteCode_dev 7y agoNo. It's been explicitly stated that Python will: - never become statically typed - never make the type hints mandatory - never enforce the type hints at run time Python is jokingly said to be "the second best language for most things". To achieve this, it has to be able to be decent for many different purposes such as setuping machine learning models, scripting SIG system, doing test automation, powering a web site, provisioning linux servers, scrapping web page, batch renaming files and teaching code, analyzing numbers from a hdf5 file, being the client to a database/ftp/ssh/imap system. It means it can't just be purely functional/OOP/imperative, be must borrow from each paradigm. Be statically typed or not, but gives features from both. Etc. Because you don't want type hints when you are in jupyter or scripting your linux box. But you may want it when your Saas reach 3 millions lines.
- pansa2 7y ago> It's been explicitly stated that Python will never make the type hints mandatory Type hints are already mandatory if you want to use data classes. This is unfortunate - the two features really should be orthogonal. At least when Guido was still BDFL, there was little resistance to type hints encroaching on unrelated areas of the language, and hence becoming less-and-less optional.
- Bnshsysjab 7y agoWhile I really like type annotations I hate that they’re little more than IDE hints. There’s nothing to stop calling functions with incorrect datatypes at runtime and I’d consider this one of the single biggest weaknesses of python right now
- BiteCode_dev 7y agoThe fact you can rely on every API to allow duck typing is a core feature of the language. It has pros and cons, of course, like all features. But it is not an accident, it's by design. And type hints have been designed with this in mind. It will stay that way. If this is feature makes you unhappy, Python will make you unhappy. It's important to know what are the values that are important to you and chose your toolbox accordingly.
- Bnshsysjab 7y agoI think it should be up to the API to specify which types are allowed, not the developer just hoping it works. When you’ve worked on a sufficiently large and complex code base maybe you’ll understand. Edit: coded in python for > 10 years. I prefer it over other languages, warts and all.
- BiteCode_dev 7y ago> I think it should be up to the API to specify which types are allowed, not the developer just hoping it works. This assumes: - the dev is thinking about the API - the dev has the knowledge and experience to do it - the dev has the resource/constraints that allow to do so - dev has an interest in doing so - this is a good investment for that particular project Thinking in black and white does not make for good language design. I'm glad Guido designed Python and said no to so many requests to make it no Python. Because he sure been pressured to. > When you’ve worked on a sufficiently large and complex code base maybe you’ll understand. I do. I also understand Python projects that have a favorable cost/benefit ratio for type hints are not that many. When you're used to hammer you see everything as a nail, and people loving a specific language feature tend to want it everywhere. A huge number of dev in the Python community are not professional nor even experienced programmers. And a equally big number of projects have a size or a nature that makes type hint a bad value. When you’ve worked on a sufficiently diverse project base maybe you’ll understand.
- Browun 7y agoWhat's the benefit of this over simply adding the type annotations directly? I guess this is mostly for those unwilling to understand types? Especially given the admitted limitations of inferring types, such as the add exmaple discussed; this seems to be fixing an anti-pattern problem. As this those who would build a project in Python that would largely benefit from these annotations, would be most suited to just spend the couple of hours needed to truly apply it themselves.
- resoluteteeth 7y agoHave you actually spent time adding type annotations to existing python code? In my experience it's a huge pain compared to other languages. Documentation is sloppy about types because people aren't used to worrying about them, so even looking at the code it's really hard to even determine the actual types of things without just running it and poking around.
- Browun 7y agoNot sure why the initial tone of your response is quite so dismissive. But I do work with Python on a daily basis, even though this shouldn't be the deciding factor on the validity of any point. Discuss the point, not the individual, if it's so easy to disprove. It seems that you're talking about poorly documented code, not how type checks effect this. Python code should have at least docstrings for types, or a scrawl with pen and paper describing this. If not, this is a failing of the project, and it's codebase. Not something you should rely on an outside tool to fix.
- ggregoire 7y agoThat's exactly my experience.
- BiteCode_dev 7y agoOne of the core requirements of the type hints design is that they don't affect the Python language. You can still write python programs without them. You can add them only partially. You can also add them later. Python stays Python. MonkeyType lets you take a code base that is untyped, or partially typed, for historical or because it was not worth it at the time, and turn it into a fully typed project quickly. It makes the transition between the exploration phase into the industrial phase much easier. For me, it's kinda fantastic to be able to fiddle with a design, changing my mind again and again, without having to fight the type system, then once things are settled, add an additional safety net on top. It's also very reassuring knowing I can just hack things, knowing that later on, if I want to take it to the next level, I have the option to change my mind about type hints.
- anentropic 7y agoI will definitely check this out! Very similar is https://google.github.io/pytype/ https://google.github.io/pytype/ which also infers and adds annotations, as well as providing a type checker which in some ways works better than mypy (recursive types!)
- plafl 7y agoAwesome. I will try this next Monday on a code base at work, I hope it will improve readability. I admit I'm a little skeptical about the adoption of optional typing if it's not going to affect performance but maybe this will change my mind.
- FpUser 7y agoNot Python exactly but I have these situation. I am writing business server in C++ that exposes JSON based RPC API and Javascript client library to access said API. I have no idea where is that claim about high productivity of these free form languages came from. In C++ in addition to the nice intellisense compiler checks everything before sending program to debug. So I do not need to do manual testing during runtime to discover typo in my code. In javascript I can write literally any crap and the system lets it go. Intellisense sucks big time as well. As a result of this and usual typos I have to literally go and do a lot of clicking to see if this part blows up at runtime. So all in all I'd say productivity in C++ is way better. And C++ is not a stellar example of easy language. Actually it is easy for me. Not because I am super-duper programmer but your basic coding with the help of standard library is a piece of cake. I do not really need to be an architecture astronaut and dive into any esoteric stuff. Simply not needed
- gcthomas 7y agoTo answer your question, Python is several times more expressive than C++ given that is is a higher level language, and since fewer lines are needed for a given program it is likely to be quicker to write. To quote Martelli from Google talking about how the Youtube startup outran Google Video's might so much that they had to buy them: "Eventually we bought that little start up and we found how 20 developers ran circles around our hundreds of great developers. The solution was simple! Those 20 guys were using Python. We were using C++. So, that was YouTube and still is." [1] Google took months to copy new YouTube features, while YouTube could catch up in a week after Google innovated. [1] Google Books, search term 'python interviews google vs youtube'
- FpUser 7y ago1) A was talking about JavaScript 2) I do use Python but mostly for administrative/deployment/similar tasks as a scripting language. I see no proof that Python is "several times more expressive". I would really appreciate if you could kindly point me to a real study that shows exactly how it is more expressive and on what in particular the time is saved (you can of course omit the examples where Python is used as a glue language to stitch together few math libraries written in C/++). I do have my own experience. Of course it is statistically meaningless. My friend and I have implemented simple GUI desktop app (custom video player with some features) as a bet. My buddy did it in Python and I did it in Delphi. I won: 1 hour vs 4.
- lmeyerov 7y agoIs there any level of growing community consensus on a types/contracts-for-python effort?
- mkchoi212 7y agoHaven’t been involved in the Python community for awhile but isn’t already a thing? Really cool project but thought I saw something like this already existed. If I’m right, is there anything that stands this apart from its counterparts?