-
-
Notifications
You must be signed in to change notification settings - Fork 24
Runtime assertions with binding.assert
#341
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
base: main
Are you sure you want to change the base?
Conversation
lib/literal.rb
Outdated
| def assert(**kwargs) | ||
| kwargs.each do |name, type| | ||
| value = local_variable_get(name) | ||
| Literal.check(value, type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add more context to the error message since we know what the key was.
|
Things to consider adding:
PR also needs some minimal docs explaining how the method works (what error it raises if an assertion fails, what kinds of types you can pass, etc.) But, this feels like a perfect next step for Literal. I love it |
4c5ee73 to
8edcea7
Compare
8edcea7 to
379a078
Compare
binding.assert
| end | ||
| end | ||
|
|
||
| ::Binding.include(Literal::BindingAssert) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make a literal/pure entry point that people can use if they want to opt out of patches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally would rather opt-in than opt-out, eg literal/binding_assert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elaborate? It has no perf cost, no overhead. And this DX is superior to Literal.assert(binding, **constraints).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is mixing something in Binding, which I would find somewhat unexpected unless I explicitly wanted that feature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the main use case will be people opting in. I was only thinking of adding /pure for gem authors who might not want a transient dependency to do patching.
fractaledmind
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a great feature that needs one more pass to be ready.
| value = INSTANCE_VARIABLE_GET_METHOD.bind_call(bind.receiver, name) | ||
| end | ||
| elsif first_byte == 36 # $foo | ||
| raise if string_name.match?(/[\s\.]/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to raise a named exception here with a clear message.
| end | ||
|
|
||
| unless type === value | ||
| raise ::TypeError.new(<<~MESSAGE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Joel and I discussed that Literal::TypeError needs a refactor to be more generic to allow for simpler errors like this. Personally, I think make TypeError simple like this and move the property stuff into a PropertyError class that inherits from TypeError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea
Co-authored-by: Stephen Margheim <[email protected]>
This PR introduces a
Bindingpatch that allows you to make assertions about local variables at any point.You would ideally include
Literal::TypesintoObjectif you wanted to make the best use of this, since otherwise the_types are not available in the context of an instance.It would be nice if there was an equally clean way to make assertions about instance variables.