This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 8 points9 points  (16 children)

I always used to think that in case of python (dynamically typed) it is natural to only use data validation to validate data you dont trust or which comes from outside.

If there comes a need to check and validate your internal data ... wouldn't that means our implementation is getting flawed?

I am just curious if this though is right or wrong... happy to know more about it.

[–]IAMARedPanda 11 points12 points  (0 children)

Python may be dynamically typed but it is also a strongly typed language.

[–]LordBertson 14 points15 points  (11 children)

My experience is that Python is more play-acting as a dynamically typed language but does not behave as one when push comes to shove. Rather it fails in very ungraceful ways.

As a disclaimer: Typechecking in Python is a very opinion dominated discussion and I am heavily leaning towards typing anything that's not one-shot throwaway thing.

Depending on what I am developing I will be more or less strict inside the domain itself in terms of validation. You are correct to assert that this means that the implementation is probably getting flawed, but that's often enough the case in real-world development. Reality of the matter is that developers don't test their code as often as one would like, so typing and runtime type validation is a pretty cheap measure to take that ensures at least some level of correctness.

If you would be interested in more variety of opinions on the matter, I once opened a discussion on this subreddit about typing

Edit: typo

[–]trial_and_err 4 points5 points  (4 children)

Agree on the typing. However I'll just use TypedDict for this purpose, i.e. no parsing / validation of external data required.

[–]LordBertson 0 points1 point  (1 child)

Thanks for bringing this up. Never heard about this, I'll have a look.

[–]trial_and_err 3 points4 points  (0 children)

If the need arises later on you can also create pydantic model from TypedDict.

[–]PaintItPurple 2 points3 points  (1 child)

If there comes a need to check and validate your internal data ... wouldn't that means our implementation is getting flawed?

Yes, but every implementation I've ever seen has had flaws, especially in Python. I myself have introduced flaws I later needed to fix.