Skip to content

Assertion helper added #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Conversation

Kacppian
Copy link

  • Helper to replace assert added.

Copy link
Member

@jkimbo jkimbo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks @Kacppian

@jkimbo jkimbo requested a review from syrusakbary August 25, 2018 10:33
@syrusakbary
Copy link
Member

First, thanks a lot for your work @Kacppian.

After reviewing this PR, I sincerely don't prefer this new syntax, since with the assert the code was much more legible/readable.
This was my fault since I embraced the change proposed by @danpalmer without questioning if the readability was going to be better or not (#784), maybe this PR is approached differently to what Dan suggested:

Instead of:

assert something is not None, "Something was unexpectedly None"

We should do this:

if something is not None:
    raise AssertionError("Something was unexpectedly None")

What we have in the PR:

raise_assertion_if_not(
   something is not None,
   "Something was unexpectedly None"
)

I would love to get more input into this, so comments are more than welcome!

@danpalmer
Copy link
Contributor

Hey sorry for not looking at this sooner. I agree with Syrus, the helper feels unnecessary. It basically just abstracts out an if statement, which I think makes the code less readable.

@Kacppian
Copy link
Author

Thank you for the comments. Should I fall back to not having the helper method?

@jkimbo
Copy link
Member

jkimbo commented Aug 29, 2018

In JavaScript the invariant function is quite commonly used (https://github.com/zertosh/invariant) and I've found that I makes my code more readable. Would changing the method name to be more concise help?

@danpalmer
Copy link
Contributor

@Kacppian in my opinion, we should use the form:

if condition:
    raise AssertionError(message)

And I think we should do this at the call sites, not in a function.

@jkimbo I think that makes more sense in the JS ecosystem where everything is libraries and functions all the way down, but in Python it's much expected that you do the obvious thing rather than wrapping up potentially unexpected effects.

In this case I don't think there's any benefit introduced by wrapping up AssertionErrors - it really does just feel like turning an if statement into a function - that requires an extra import, possibly a test, documentation, knowledge transfer, and understanding by new developers. An if/raise is just standard usage of two core parts of the language.

@Kacppian
Copy link
Author

I used the suggested construction in #794 and closed it later because you'd suggested I use a helper method. Never mind, I'll revert back to it.

@danpalmer
Copy link
Contributor

@Kacppian apologies for the back and forth. I disagree with @dan98765's suggestion on that PR, and it appears that @syrusakbary does too. I'm afraid we're not a cohesive team, just people who happen to have an interest in this library!

@Kacppian
Copy link
Author

I'm extremely sorry. I didn't mean it to come out like that. Just mentioning the reason I've done it this way.

@danpalmer
Copy link
Contributor

danpalmer commented Aug 29, 2018 via email

@Kacppian
Copy link
Author

Cool then, I'll remove the helper method and make the necessary changes.

@jkimbo
Copy link
Member

jkimbo commented Aug 29, 2018

I agree that the unwrapped form is the one to go for because it's unambiguous and requires no extra
domain knowledge. However I do think it hurts readability because it draws attention to what are just sanity checks. IMO the best form is the assert one, it's just a shame that python doesn't respect it in production environments!

@morenoh149
Copy link
Contributor

needs rebase

@morenoh149
Copy link
Contributor

needs love ;)

@stale
Copy link

stale bot commented Jul 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 29, 2019
@stale stale bot closed this Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants