-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Deep magic underscore error messages rebase #2843
Deep magic underscore error messages rebase #2843
Conversation
- plotly.graph_objs.Area | ||
- plotly.graph_objs.Histogram | ||
- etc. | ||
plotly.graph_objs.Data is deprecated. |
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.
please revert these whitespace-only changes ... I think your editor must be doing this?
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.
This is weird because I never opened this file, so maybe black
did it. I'll just check out the master version.
packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_layout_subplots.py
Outdated
Show resolved
Hide resolved
# Check indexing errors can be detected when path used as key to go.Figure | ||
try: | ||
x0 = some_fig["layout.shapes[2].x0"] | ||
except IndexError as e: |
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.
Ah, so every test here needs to be rewritten to use with pytest.raises(...)
because otherwise these tests can pass if no error is raised! We need to guarantee that the error is raised here, and check its contents.
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 tried using pytest.raisesRegex because I wanted to check the error message, but had trouble making a regex matching multiple lines. I can show you the error when using the multiline flag for re
, it kind of looked like a bug with re
. Also I don't need a full-blown regex, just a string comparison would suffice.
packages/python/plotly/plotly/tests/test_core/test_errors/test_dict_path_errors.py
Outdated
Show resolved
Hide resolved
So the error message for this call has changed for the worse IMO: import plotly.graph_objects as go
go.Figure(go.Scatter()).update_traces(text_yo="hey") It used to say basically |
Also needs a changelog entry |
Do you mean this error message is not so great?: import plotly.graph_objects as go
go.Figure(go.Scatter()).update_traces(text="hey") Could it be because |
Shows the property that doesn't support it. Need to still update the tests though.
This is an improvement, but the error message is still a little opaque.
I wonder if it shouldn't be similar to the error we get if we try
Perhaps the
|
For example, `fig.update_layout(width_x=1)` gives the following error message: TypeError: 'NoneType' object is not subscriptable Invalid value received for the 'width' property of layout The 'width' property is a number and may be specified as: - An int or float in the interval [10, inf] Property does not support subscripting: width_x ~~~~~ because `fig.layout['width']` can't be subscripted (you can't do `fig.layout['width']['x'] = 1`)
@jonmmease mind taking a quick look here to see if everything makes sense? |
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.
Looks really nice! I left a couple of comments on the exception class hierarchy. I still want to check out the branch and play around with it a little. I'll do that today.
@@ -83,3 +83,14 @@ def __init__(self, obj, path, notes=()): | |||
super(PlotlyDataTypeError, self).__init__( | |||
message=message, path=path, notes=notes | |||
) | |||
|
|||
|
|||
class PlotlyKeyError(LookupError): |
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.
This is technically a breaking change unfortunately, since this will behave differently:
try:
fig.update(...)
except KeyError:
pass
Instead, I think you should be able to subclass KeyError and override the __str__
magic method.
packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_layout_subplots.py
Outdated
Show resolved
Hide resolved
Right now it looks like trailing underscores are ignored, e.g. this is valid: fig.update_layout(width_=300) I'm torn on whether this should raise an error (looks like it did before). |
I agree with @nicolaskruchten that the This one is also a little off IMO: fig.update_layout(title_text_asdf=300)
|
Do you have the latest commit? For me I get this, which was desired:
|
But I'm also open to that being problematic : ) |
But overrode the __str__ method.
Thanks @jonmmease and @nicolaskruchten for the reviews! How important is it to maintain Now we could change You may ask: "But how did the more verbose error get thrown before this PR?"
show the possible values for
basically did this, because it knew
and so that's why the error was reported relative to |
@nicholas-esterer I think I agree with Jon that changing the |
Seeing what happens if we change |
Yes, that's the case I meant. OK, then how about we leave that one case to return a KeyError (albeit with a nicer error message), and catch that KeyError and rewrap it as a ValueError in the non-[] cases? Either way, I think Jon and I are aligned that we should not change the error types in this PR. I agree that the current situation is not ideal, with KeyError sometimes and ValueError sometimes, but it's too late to change this basically :) |
I agree too that changing the error types is a bad idea. I guess I got to start digging 🕳️ 🙂 |
Because lookup in subclasses of BasePlotlyType and BaseFigure should throw KeyError.
I believe the new behaviour is consistent with the previous exception behaviour, but we will run the CI tests to be sure.
OK! So I think I've managed to preserve the previous exception behaviour as I have not changed any of the already existing tests (the only test "changed" is |
Before the subscripting error was only thrown for NoneType objects, but because stuff like `"hello"["hi"]` and `1["hey"]` also are invalid, and these also throw TypeError, then these throw the subscripting error message too. HOWEVER, this error is casted to ValueError on property assignment and PlotlyKeyError on property read, to keep consistency among the exception types.
Now subscripting errors are thrown for any type throwing TypeError when subscripting them. This is enough for Plotly.py purposes because other than |
This looks like it's getting close to mergeable! Can we switch to using the full-width |
and underneath the whole offending part of the path e.g., the_badpart_of_the_path ^^^^^^^ for both property and subscripting errors.
@@ -19,6 +19,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). | |||
- The `add_trace`, `add_shape`, `add_annotation`, `add_layout_image`, `add_hline`, `add_vline`, `add_hrect`, `add_vrect` functions accept an argument `exclude_empty_subplots` which if `True`, only adds the object to subplots already containing traces or layout objects. This is useful in conjunction with the `row="all"` and `col="all"` arguments. ([#2840](https://github.com/plotly/plotly.py/pull/2840)) | |||
- For all `go.Figure` functions accepting a selector argument (e.g., `select_traces`), this argument can now also be a function which is passed each relevant graph object (in the case of `select_traces`, it is passed every trace in the figure). For graph objects where this function returns true, the graph object is included in the selection. ([#2844](https://github.com/plotly/plotly.py/pull/2844)) | |||
|
|||
### Added |
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.
please move this to the 4.13 block
then if that fails, try and print the int, then if that fails, truely fail. This allows taking the length of unicode objects in Python2.
The only outstanding item here seems to be the "trailing underscore" question... Right now this fails on |
Ahh actually it's a bit worse... this also works: |
need to add tests
Cool, just need some tests on the leading/trailing/multiple underscores :) |
Tests in tests/test_core/test_errors/test_dict_path_errors.py not yet updated though.
Also tripped when no underscore paths are used, e.g., fig.data[0].line["colr"] = "blue"
@jonmmease this is ready for another review I think. Check out the "did you mean...?" 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.
Looks great @nicholas-esterer. I'm happy with the code, so if @nicolaskruchten is happy with the behavior (I didn't check out and play with the latest version) then lgtm.
See #2072
Rebase of
#2824