5 ms·
It would be nice if someone created a typescript for python. Something that allows us to wrangle control and sanity over reams of legacy untyped python madness.
by Arbortheus 4y ago
It would be nice if someone created a typescript for python. Something that allows us to wrangle control and sanity over reams of legacy untyped python madness.
- boxed 4y agoMypy?
- tingletech 4y agowhy not use the built in typing? https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html
- Reubensson 4y agoit doesnt enforce the types in any way
- isoprophlex 4y agoYou can run, for example, pre-commit checks such as mypy to enforce some semblance of sanity.
- joshuamorton 4y agoNeither does typescript. All the types are removed at runtime, it's up to you to ensure that your build process and tooling correctly validates the types. The same is true for python, and the ecosystem has really great tools for doing this.
- kortex 4y agoUse pydandic for serde/IO validation, and mypy --strict. And minimize cheating e.g. Any. I think you'll find it extremely stable and a totally different beast than usual python.
- LtWorf 4y agoI wrote typedload before pydantic was a thing. It's faster and actually works well with mypy and python's type system. Pydantic requires inheritance and requires a modified mypy because it uses types its own way rather than the python way. Oh, typedload is faster than pydantic, despite being pure python instead of an .so file.
- davidatbu 4y agoI would be very interested in seeing some benchmarks! I'm pretty sure that serde, the rust library that Pydantic uses, is faster than the JSON parser written in C in the python standard lib, so I'd be very surprised if your pure python validation library beat pydantic.
- LtWorf 4y agohttps://ltworf.github.io/typedload/performance.html https://ltworf.github.io/typedload/performance.html At the bottom there are instructions on how to run the benchmark locally.
- davidatbu 4y agoI read more about your repo, and it looks like typedload only does Python lists/dicts -> typed python objects(like named tuples, data classes, attrs classes, etc). Ie, the benchmarks don't include the time required to parse the JSON. So I modified the benchmarking code to include loading JSON[0], and your library still came out on top! But, it turns out I was wrong about pydantic using serde under the hood. Pydantic version 2 will. And the maintainer aims for it to be about 10x faster [1] than version 1. Nonetheless, this was definitely a surprise for me, and if I ever go back to using Python, I'll definitely check your library out! I'm curious, why do you think pydantic took off and your library didn't? It looks like your library is both faster and easier to use to me. [0] https://github.com/davidatsurge/typedload/pull/1/files?diff=split&w=1 https://github.com/davidatsurge/typedload/pull/1/files?diff=... [1] https://github.com/pydantic/pydantic-core#pydantic-core https://github.com/pydantic/pydantic-core#pydantic-core
- davidatbu 4y agoI think both `erg` and statically typed Python are both attempts at doing exactly that. May be what you mean is a static typing solution for Python whose syntax is a superset of Python syntax (which I don't think `erg` is), but isn't identical to Python's syntax( ie, it is a strict superset, which statically typed Python's syntax isn't). Typescript is exactly that for JS. In that case, I agree with you that such an attempt would indeed be very interesting because it would allow for more aggressive exploration of ergonomic typing constructs while maintaining that any valid Python would be valid in that new language as well (just like TS).
- faizshah 4y agoRelated question, how does typescript’s gradual typing system work? Are there any resources on how they implemented and architected it?
- cellis 4y agoAnd while they're at it, can they unify the pip, egg, conda, miniconda, apt-get dependency management madness? If we're putting in requests for miracles, that's mine.
- nielsbot 4y agoMypy is basically that. https://github.com/python/mypy https://github.com/python/mypy
- Too 4y agoAlso note that while this repo might look like a third party bolton (it started that way), the type hinting syntax is nowadays actually integrated in the language. The only thing you need to do is add appropriate type hints at function definitions and then run mypy in your build and CI and you got something that checks basically as strict as Java. For those saying it’s optional so can’t be trusted, consider that compiling C++ with -Wall -Werror is also optional… adding it is simply part of standard project hygiene.
- cercatrova 4y agoPylance in VSCode also errs on type mismatches.
- mdaniel 4y ago> The only thing you need to do is add appropriate type hints at function definitions and then run mypy in your build and CI and you got something that checks basically as strict as Java. There's another comment making this claim elsewhere and I wonder if perhaps those assertions are coming from someone who doesn't write Java, or if my experience with mypy is just especially bad I'll also point out that easily 90% of my heartburn with trying to typehint python in legacy projects is that python seems to encourage so much introspection and namespace trickery. It feels to me like the mypy audience is saying "well, there's a staticly typed language hiding in python so just don't use unsafe parts of the language and you'll be fine"
- nielsbot 4y agoIf it's anything like TypeScript, you can add type documentation for all the tricks.
- barefeg 4y agoAs others have mentioned, one can get some types in python through its own typing module and use a combination of tools for type enforcing and editor intellisense (mypy, pylance, etc.). However what I’m missing is having more advanced type constructs. For example mapped types, lookup types, etc [1]. That said, it seems like python devs keep adding features over time. For example a couple of years ago it wasn’t possible to specify which fields of a dictionary are required and not required (it required putting required fields in one TypedDict and then subclassing it into another one that had the non required ones with total=False), nor it was possible to “spread” a TypedDict to declare the kwarg types in a function. [1]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html https://www.typescriptlang.org/docs/handbook/release-notes/t...
- deleted 4y ago[deleted]