Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I love that Python has types available at runtime! The ability to have those type-generated CLIs or APIs is fantastic. I learned from this article about functools.singledispatch, and can't wait to use it. I've always wanted something like in typescript! But `functools.singledispatch` is just not possible in TS.

I think working with dicts is still a little clunky in python, and there are some rough edges (eg mypy being a little weaker), but I genuinely think I like types in python better than TS!



> I genuinely think I like types in python better than TS!

That's a step further than I'm willing to go :D. For example, the other day, I was able to do

    settingToToggle: KeysWithTypeOf<SomeObject, boolean>
which (after properly defining `KeysWithTypeOf`) allowed me to specify that `settingToToggle` should be a string literal that is a key on `SomeObject` whose corresponding value is a boolean.

Typescript comes closest to fully letting me express my intentions statically and curtly, and while I hope Python's static typing will grow to be as expressive as Typescript, I think that day is still afar off.


For me the remaining pieces for Python are much more attainable than the missing pieces for typescript. I think basically all the helper TS things (eg KeyOf, etc) are addable to Python. Runtime types are not addable to TS. It goes against some of the core principles of TS.

But I agree there are loads of missing features in Python :)


You might be (very) interested in this project then: https://deepkit.io/

The guy behind it made a PR to typescript proposing runtime availability of types[0], got rejected, and got going on it by himself. And he already has PoC libraries with the (early) equivalents of Pydantic/FastAPI/SQLModel (using runtime types for (de)serialization).

I really hope this experiment works out and that it gains traction, because it's a massive value add to TS IMO.

[0] https://github.com/microsoft/TypeScript/issues/47658


Oh wow! You are correct, I am very interested in it! Thanks for sharing! Honestly on most of my personal projects, I only use typescript via the jsdoc interface. I just never felt the need to get to 100% type safety for most projects. And I never liked how the types lend themselves to a style of programming which typescript can't support due to lack of runtime typing (eg multiple dispatch, function overloading, serialisation, etc). This project looks very cool, and shifts those tradeoffs for me! I'm going to need to give it a whirl!


What do you use for type-generated CLIs? I've been using Pydantic for config parsing and Cleo for CLI, but I don't yet have a good solution for hoisting/injection of CLI arguments alongside env/file configs.

I really want a declarative solution like Rust's Clap library, but I haven't yet found anything like it out there.


Unfortunately I couldn't find a good package either, so ended up rolling my own script, FnToCLI https://github.com/internetarchive/openlibrary/blob/ccf10246... . It wasn't too hard to get the bones of it working, so if you want to build something similar with built-in env handling, that might not be too bad! Under the hood it converts everything to argparse, so you can always use any complicated argparse options/etc if you want to.

The closest package was Typer, by the creator of FastAPI, but it looked unmaintained, and didn't support Literal types! Or at least that was the case when I was investigating ~year ago.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: