Skip to content
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

prevent the creation of new px.default properties at run time #2183

Merged
merged 7 commits into from
Feb 12, 2020

Conversation

jdamiba
Copy link
Contributor

@jdamiba jdamiba commented Feb 11, 2020

closes #2168

The purpose of this PR is to prevent the creation of new px.default properties at run time.

Testing Plan:

Prior to this PR, the following code does not throw an exception:

import plotly.express as px

px.defaults.template = "ggplot2"
px.defaults.color_continuous_scale = px.colors.sequential.Blackbody
px.defaults.width = 600
px.defaults.height = 400
px.defaults.should_not_work = "test"

df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="sepal_length", width=400)
fig.show()

When running this PR, the above code throws:

Traceback (most recent call last):
  File "app.py", line 6, in <module>
    px.defaults.should_not_work = "test"
AttributeError: 'PxDefaults' object has no attribute 'should_not_work'

@emmanuelle
Copy link
Contributor

Very elegant solution! I didn't know __slots__ before 🎉. Could you also please add a test in https://github.com/plotly/plotly.py/blob/master/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py which would check that an AttributeError is raised? See for example https://github.com/plotly/plotly.py/blob/master/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py#L159 for a typical pattern.

@jdamiba
Copy link
Contributor Author

jdamiba commented Feb 12, 2020

@emmanuelle

I have added a test- thanks for the reminder. I will try to proactively add them where relevant going forward :)

@@ -232,3 +233,9 @@ def assert_orderings(days_order, days_check, times_order, times_check):
for days in permutations(df["day"].unique()):
for times in permutations(df["time"].unique()):
assert_orderings(days, days, times, times)


def check_permissive_defaults():
Copy link
Contributor

Choose a reason for hiding this comment

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

I think your function should be called test_something so that pytest catches it in the test suite (otherwise it will consider it a helper function). You can verify this by executing locally pytest in your machine (for example in the same directory, pytest -v test_px.py and check whether the new function appears). Maybe I'm wrong but please check :-).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right about tests needing to start with test in order to be picked up by pytest.

After modifying to above code, the output of running pytest -v test_px.py is:

test_px.py::test_scatter PASSED
test_px.py::test_custom_data_scatter PASSED
test_px.py::test_labels PASSED
test_px.py::test_px_templates PASSED
test_px.py::test_orthogonal_orderings PASSED
test_px.py::test_permissive_defaults PASSED

Copy link
Contributor

Choose a reason for hiding this comment

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

awesome!

@emmanuelle
Copy link
Contributor

Thanks @jdamiba ! Just wait for the CI to finish (everything should be green except for one of the orca builds which failure is not related to this PR), and then 💃 !

@jdamiba jdamiba merged commit f4a005d into master Feb 12, 2020
@jdamiba jdamiba deleted the px-permissive-defaults branch February 12, 2020 17:17
@nicolaskruchten nicolaskruchten added this to the v4.5.1 milestone Feb 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

px.defaults too permissive
3 participants