On dataclasses
and typing
#913
Replies: 5 comments 7 replies
-
I've been using the backend filtering refactor PR as a test bed for dataclasses and typing and I'm a fan. These are my thoughts as summarized there:
Data classesMy plan with this post was to paste the above summary and then write two data class implementations for comparison: one using Partial typingI find that partial typing, it being optional, is a situation without losers and thus is a no-brainer. For more specific thoughts, see above quote. |
Beta Was this translation helpful? Give feedback.
-
I started writing this comment not caring much about type hints, and became less of a fan the more I thought about them. I think if we want static typing, we should use a proper statically typed language (e.g., golang) that has the features you'd expect: real function overloading, interfaces, etc. I find working with static types in python a bit tedious, since you don't get any of the real goodies of a static typing system. What you get instead is nagging. Moreover, I like duck typing and I think it forces the programmer to focus on behavior rather than getting bogged down in type. You often (for example) don't care whether something is a tuple or a list in python, you just care that you can iterate through its members. Encouraging the programmer to specify this in the function signature encourages them to lazily specify whatever the first structure they think of is. If they, for example, specify a tuple of integers in their function signature, it makes it more irritating down the road for a programmer who realizes they can save some memory (and flops in many cases) by using a generator expression instead. While it's possible to specify As far as dataclasses, I have a general suspicion of "magic" annotations that generate a bunch of hidden code in large projects. Maybe too much trauma from diagnosing bugs involving Lombok and SpringBoot in the Java world. The point is, if you can't see the actual For example, how is All that said, while I have strong reservations about type hints and data classes, I'm also comfortable using them if the project goes that way. |
Beta Was this translation helpful? Give feedback.
-
Exactly what I wanted to say. Moreover frameworks, tools, design patterns for statically typed language are actually built with that in mind, so trying to achieve 100% consistency with typing in python is going to be futile, which would mean that it should be used only when needed with proper reasons. Since a 100% consistency is not possible, it will be hard to enforce or trust it using a static type checker and this would require using tricks like But I do love using it as an alternate to Static typings are great for large and well defined codebases, so the tradeoff for flexibility is worth it. But we would benefit from flexibility rather than a rigid structure as of now. I really loved static typing when I had to switch to a typed language from a large Django codebase, but later realised that I was not writing pythonic code. Python codebases from what I have seen are very modular(script like), easy to look into, flexible and rely on test cases as they are really cheap compared to other languages. I just find it a worthy compromise As for The biggest advantage and disadvantage of python is its flexibility, so it is hard to give a binary answer, so I am leaning towards easy to understand, straightforward vocabulary solution. Right now I see them as a magic tools that python provides which should be used when the situation demands it |
Beta Was this translation helpful? Give feedback.
-
General thoughts:
In the interest of reaching a decision quickly, I think we should avoid using dataclasses and type annotations for now. If there is a specific use case that anyone thinks that these features would help solve better than other solutions, then let's talk about that particular use case when it comes up. |
Beta Was this translation helpful? Give feedback.
-
Our perspectives are different. Well, to be fair, there seem to be only two camps, and I'm alone in mine, haha. So I guess my perspective is different. In my very personal experience, technological solutions are where I can find the lowest hanging fruit. The tech (a.k.a. tool) might not be trivial to use or to switch to, but if it can improve the consistency with which a problem is solved, it's usually worth the effort in my book. In a situation like that, I'm exchanging the effort of dealing with inconsistency with the effort of upgrading my toolkit. I guess it's a question of when the exchange makes sense. I have a low tolerance for inconsistency and a high tolerance for novelty. But, I guess I should appreciate that others might look for setups that favor other things. At the same time, I feel like we're sometimes prioritizing technological conservacy. I acknowledge the point about that being good for the future community around Mathesar, but I'm just not sure if that's true.
See, in my experience it's the opposite. It's the ambiguity that requires ongoing maintenance, and annotations address that. We could use the same argument for code comments and docstrings. They put constraints on your thinking and interpretation; they can be done wrong; when you change a seemingly small thing you might have to go through the comments making changes; writing up code might make you think about nuances that you wouldn't have to otherwise think about (again, slowing you down and constraining you). What I was saying is that we shouldn't approach this tool as something that we (or the community) wouldn't have the skill to use.
I don't expect type annotation related discussions to come up. Each moment it might be useful is such a small nuisance on its own that the effort to renew a discussion will not feel warranted. I'll see how data classes work out in the filtering refactor. We might be able to experiment with alternatives there. |
Beta Was this translation helpful? Give feedback.
-
Let us discuss the following, somewhat recent, Python features:
dataclasses
andtyping
.I'm not structuring this discussion with a proposal, since I think that right now we could use a general discussion on the subject. When we want to raise and consider a proposal, I recommend opening a dedicated discussion for that.
Beta Was this translation helpful? Give feedback.
All reactions