3 ms·
I used to dislike type hints for Python because: - I was afraid it would change the language culture - It was not comfortable to use - The idea and design wa
by BiteCode_dev 7y ago
I used to dislike type hints for Python because:
- I was afraid it would change the language culture
- It was not comfortable to use
- The idea and design was, as you said, bizarre
- Use use a dynamic language, why the hell would I want that?
Years after the fact, I'm really happy about it.
First, most of my Python code don't use them, so Python stays python. But if I ever need them, there are here.
Of course, you could argue I should just use a language that has been designed for that since I need type. But that's missing the point: I don't usually start using hints. I code regular Python, because it's awesome. I just sometimes add type hints after the fact as a bonus.
The ability to do that is fantastic, even if the type system is far from perfect.
I want to code with Python most of the time anyway. I personally have very few use cases for another language, doing mostly web stuff.
Type hints are still very verbose, and convoluted, although it will get better with 3.9 thanks to 2 PEP targeting type hints ergonomics. So sure, it's never going to fit perfectly, but I'm glad it's here.
Now for a JSON schema, I guess it's the same deal. You use JSON because it fits your use case, and one day you realize your situation could now benefit from more robustness, and you add it to the mix.
Again, you could argue you should have though of that at the beginning, but projects don't have frozen requirements, they evolve. And often, you want to start lean, because it will most probably stays that way, and otherwise would be so costly the project would never grow to a point you would need robustness.
Now in this particular case, this lib is not just checking type, but also arbitrary logic. And making sure data is consistent is necessary no matter how dynamic or static your language is. There is a limit to the constraints your type system can represent.
- thrwaway69 7y agoHave you ever checked dart? Type inference can get you so so far
- BiteCode_dev 7y agoYes. Flutter is a killer app. But then again, Django as well.
- dehrmann 7y agoI led a project for validating customer-provided data against a json schema. The structure of the data was moderately complex, and it was getting bound to pojos by a pretty lenient binder. This caused issues when making additions to the API since customers were sending arbitrary fields, and duck typing the data wasn't that reliable. The easiest thing to validate before binding with a json schema. What's different this and Python type hints is that this was a public-facing API, so you have far less control over what data you'll see, and the server code was Java, so getting things strongly typed and validated early on makes your life easier.
- BiteCode_dev 7y agoSchema validation is more than type checking, and has been very well solved by libraries like marshmallow and pydantic for a long time. I don't think the comparison holds.
- dehrmann 7y agoI've worked with Python professionally for 3-5 years, but only type-hinted Python for ~5 months. This is coming from someone who prefers Java, but I don't like Python type hints because they were bolted onto the language worse than classes, type hierarchies aren't mature, and if I wanted types, I'd be using Java. That said, you do need strong typing for a project once it hits a certain line count or developer count.
- BiteCode_dev 7y agoYes, the type system is clearly not well integrated. My point is, I'd rather have a not so perfect one when I need it, that None at all. And most of the time I don't need one.
- baq 7y agoJava's type system is weaker than what Python's typing provides. If you want to make fun of Python's type system, compare it to TypeScript, ocaml or haskell. Python's typing is actually useful even if it's bolted on and the typeshed is incomplete.
- zmmmmm 7y agoThere are languages though like eg: Groovy where gradual typing is more designed in. It's unfortunate to me that Python has cultivated this attititude because it's like local maxima that prevents the mainstream getting to soemthing better. Python is the new perl / PHP etc.
- BiteCode_dev 7y agoWhat attitude?
- zmmmmm 7y agoApologies ... I mean the attitude that Python is "good enough" because it has type hinting and therefore it is not worth bothering with exploring other languages, especially ones that have wholly more powerful type systems. I have found (personal experience only here) that it can be tremendously hard to get Python programmers to learn and embrace other languages and approaches to software.
- baq 7y agothat's true of any sufficiently popular tech stack. some people get either way too attached to their beloved language (...do carpenters fall in love with hammers?) or just are too lazy to learn something else - assuming that programming is their profession and not just a means to an end.