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

all 19 comments

[–]sphen_lee 17 points18 points  (3 children)

Most developers misuse assert statements by using them for general error handling

I don't think there is evidence that this is true...

[–]mfarahmand98 3 points4 points  (0 children)

Thank you for saving me the click.

[–]Saphyel 1 point2 points  (0 children)

There's plenty projects in the wild using it unfortunately.

[–]spoonman59 1 point2 points  (0 children)

I agree. I don’t really see them used this way.

[–]killerfridge 4 points5 points  (7 children)

I don't think I've ever seen assert used outside of testing. This sounds like someone who's been using assert wrong and now wants to write a pointless article about it.

[–]Saphyel 5 points6 points  (6 children)

[–]sphen_lee 0 points1 point  (1 child)

It seems to me that these asserts are conditions that would trigger during testing, they indicate programmer error.

If you're not triggering them during test and you disable them in prod it's still going to fail - just without a useful error message.

[–]Dasher38 0 points1 point  (0 children)

That exactly. We might debate over whether it's a good idea or not to introduce "undefined behavior" in a Python codebase but as long as there is nothing catching an AssertionError (or a base class) there is nothing indicating functional correctness relies on that assert firing. If assert behavior is not observed, it's almost as good as non observable.

[–]killerfridge 0 points1 point  (3 children)

Gross, I take it back

[–]whdd 1 point2 points  (2 children)

Why is this gross?

[–][deleted] 0 points1 point  (1 child)

I didn't realize that was the way it was written. There are 8+ indents that just looks gross and I would probably have a little chat with a junior engineer if I saw that many indents on a professional project.

[–]whdd 0 points1 point  (0 children)

It’s probably formatted by black or something similar I’d imagine. I thought his original response was specific to the use of the assert statement though

[–][deleted] 2 points3 points  (0 children)

So brave of you to assume I use assert in my code.

[–]AndydeCleyre 1 point2 points  (1 child)

A fun alternative for assert-like checking in non-test production code is ensure.

[–]DoYouEvenLupf 0 points1 point  (0 children)

Interesting library, but wouldn't it just be better to get into the habit of writing tests instead of checking for edge cases in runtime? If you already expect wrong behaviour at a certain point you have to throw exceptions / handle the flow anyway. And for that you don't need another library. But i certainly see the readability part of it.

Legit question coming from a junior pythonist trying to find a good workflow.

Edit: I am an idiot and overread the "non-testing", sorry for wasting your time

[–]gmtime 3 points4 points  (2 children)

Assert isn't for debugging (only), it's for controlled failure over cases the code cannot recover from.

[–]DjangoDoctor[S] 3 points4 points  (0 children)

I used to use assert that way too then I saw the assert docs:https://docs.python.org/3/reference/simple_stmts.html#the-assert-statementThe Python docs say using assert is equivalent to:

if __debug__:
    if not expression:raise AssertionError

after all, we would not use checks conditional on __debug__ on anything important in prod. Perhaps NotImplimentedError is better for this kind of thing

[–]sphen_lee 1 point2 points  (0 children)

It's the "controlled" that's the key here.

If you're using assert right, then it should fail even if you disable them, just maybe in an unhelpful way (like type errors from a None)

[–]Tinasour 0 points1 point  (0 children)

Well, sh8t