3 ms·
For the past year I've worked with a large JS/TS project undergoing a slow, gradual transformation into TS - before that, for the better part of past 10 years I
by baq 3y ago
For the past year I've worked with a large JS/TS project undergoing a slow, gradual transformation into TS - before that, for the better part of past 10 years I worked on a Python 3 + some typing, lightly enforced by mypy.
I must say I don't like JS, never have, likely never won't, but Typescript is a really pleasant language, even if JS underneath is more visible than I'd like. The mix of static analysis possible with TS vs added overhead of having to specify types (which can get very complex!) is just right. I welcome any and all efforts to make Python go this way - I'd be perfectly fine with transplanting TS type system as is.
- pjmlp 3y agoThat is basically my sentiment towards C, while using C++. :)
- solarkraft 3y agoI find "dynamic during runtime + static during development" to be a highly potent combination because it largely prevents dumb programming mistakes and enables rich IDE hints while still allowing for enough magic to build beautiful interfaces. While they are usually a great benefit and easy enough to so, sometimes getting types just right can be more of an effort than it's worth - it's beautiful that in those cases you can still go "fuck it, I know what I'm doing". Best of both worlds, really.
- brigadier132 3y ago> I find "dynamic during runtime + static during development" to be a highly potent combination Except you miss out on massive performance gains and your software runs 10x slower because of runtime type-checking. It's actually the worst of both worlds.
- baq 3y agoAnd yet machine code is basically untyped and fully dynamic. The world of computing is a strange place.
- civilitty 3y agoMachine code is type erased, not really untyped.
- nerdponx 3y agoIf you squint, you could say the same about Python code that's been checked with Mypy.
- shadowgovt 3y agoAt the machine layer, the machine is doing symbol manipulation on bit patterns. Whether those symbols ultimately mean anything is entirely up to the eventual observer of the end result. The concept of "types" breaks down at this level much like the concept of "a dog" breaks down at the atomic level.
- baq 3y agoIt's an implementation detail of popular architectures nowadays. It doesn't have to be like this. E.g. https://en.wikipedia.org/wiki/Tagged_architecture https://en.wikipedia.org/wiki/Tagged_architecture
- nerdponx 3y agoThe point is that you don't need runtime type checking if you can trust your type checker.
- frou_dh 3y agoI have seen people point out that this confusing discussion can be avoided by accepting that types are purely a static property of terms (not only identifiers and literals) in source code, and that what many insist on calling dynamic types are tags, not types.
- brigadier132 3y agoNo, dynamically typed languages literally have runtime type checking built in because the interpreter does not know the type of any individual object until runtime. Everything is stored on the heap, there is no cache locality for objects so every object property access has the performance of a linked list (or hash map, not good), and all method invocations require dynamic dispatch. What this means is your code will be slow as fuck for no reason. If your code is 100% typed and you managed to find a fully compatible compiler that actually took advantage of the type annotations you could see massive speedups and significantly lower memory usage (we are talking order of magnitude improvements).
- ska 3y agoThis was something nice about CL implementations like SBCL. When you needed fast you can fully type everything and unbox it effectivly, but it got out of your way when you didn't. Something approximating best of both worlds. Of course the fast code started to look like c, but that's not such a bad thing if you could localize it.
- shadowgovt 3y agoThere are few dynamic languages that go that far. Python isn't currently one of them; the runtime is still doing low-level type-compliance checks under the hood even if the static typing layer "knows" those checks are wasteful. At least in every compiler / runtime I'm familiar with for Python; maybe someone has built one that uses the types to generate faster code that's unsound if the static type assertions are violated. Other dynamic-typed-and-also-static-typed languages do allow for this. Many Common Lisp compiler implementations, for example, respect things like the (the) special form (https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node106.html https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node106.html), which acts as a static type assertion that allows the compiler to actually throw away the runtime sanity-checking (at the cost of producing actual unsafe code if the constraints of the form are violated).
- gaganyaan 3y agoHave you tried pypy? A 10x performance hit is very much an overstatement for lots of code.
- networked 3y agoDo you know about Pyright? It is a Python type checker implemented in, and apparently influenced by, TypeScript. It is from Microsoft, so the influence is not that surprising. There was a recent HN submission about it: https://news.ycombinator.com/item?id=34222407 https://news.ycombinator.com/item?id=34222407. I use Pyright as my primary type checker and mypy as a secondary. (For example, Pyright won't run in Termux on my phone. I could likely fix this, but mypy just installs with pipx.) There is a document comparing Pyright and mypy in the Pyright repository: https://github.com/microsoft/pyright/blob/b09f35889a9cc9b262467097c9b027b6c3939870/docs/mypy-comparison.md https://github.com/microsoft/pyright/blob/b09f35889a9cc9b262....
- VeejayRampay 3y agoI think by design, pyright works on single files, not whole codebases, unless you add some scripting on top of it
- mjr00 3y agoPython's typing ecosystem has come leaps and bounds, quickly. About 5 years ago I had to do an evaluation of Python+mypy and found that while mypy was a great idea, support by major libraries was abysmal and mypy itself was immature, so it wasn't suitable for real use. 2 years later, I took another look and the ecosystem had evolved massively, most libraries I cared about had type annotations added, PyCharm/VSCode had added support for type checking in the IDE, etc. These days all of my use of Python is with type annotations that are validated by mypy as part of CI. It's very rare that I run into a Python library that doesn't have type annotations available. It's massively improved the stability of the Python code I'm working on. One indirect side effect is that static typing actively discourages people from poor design patterns such as **kwargs or passing around dicts/dataframes of data, since there's an obvious downside to doing so now.
- bjornasm 3y agoI agree with avoiding *kwargs, but I wonder what we should do rather than to pass around dataframes if we have a program that works with dataframes? I get that there is a downside as the actual types are hidden inside the dataframes, but I am unsure if the tradeoff is worth it by unpacking the dataframe into arrays or objects for every function call and return, if the functions require them to be dataframes?
- sgarland 3y agoYou can define nested types for args and return values: def foo(bar: dict[int, dict[int, str]]) -> list[tuple[str]] In older (< 3.10 ?) versions you have to import the classes from the typing module, but other than that it’s the same. You can also define types with the Generic class if you’d rather. With live checks while you write, it’s pretty easy to do this as you go.
- mjr00 3y agoSadly I haven't found any good ways to strongly type dataframes, so my solution is mainly cultural: just make people aware that they shouldn't use dataframes unless it makes sense, as opposed to Python codebases where dataframes are the default data type that gets passed around. I've tried to limit dataframe usage only to things like reading data from CSVs, then converting them to typed dataclasses before further processing. It also depends on your use case. pandas is obviously very flexible and easy to use, so it might be worth the tradeoff to keep using it if your main use case is interactive Jupyter notebooks run by data scientists, or dealing with highly variable input data. But most of my Python is backend data pipelines that work on predictable input data, where reliability is important, so I default to dataclasses, only using pandas in rare situations with a lot of validation code surrounding it.
- deleted 3y ago[deleted]