Skip to content

Use @property to prevent direct modification of all_arrays #196

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

Merged
merged 9 commits into from
Dec 5, 2024

Conversation

bobleesj
Copy link
Contributor

@bobleesj bobleesj commented Dec 4, 2024

Closes #194

@@ -181,6 +181,17 @@ def __rtruediv__(self, other):
divided.on_q[1] = other.on_q[1] / self.on_q[1]
return divided

@property
def all_arrays(self):
return self._all_arrays
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A private property _all_arrays is used internally.

def all_arrays(self):
return self._all_arrays

@all_arrays.setter
Copy link
Contributor Author

Choose a reason for hiding this comment

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

when the user attempts to modify all_array, throw an exception. I've thought it might be better to use exception rather than warning since our _set_xarrays is doing some heavy lifting work and we want our users to use the way we want them to do?

compare_dicts(actual_dict, expected)


def test_all_array_setter():
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test for throwing an exception when the user attempts to modify all_arrays directly.

Copy link
Contributor Author

@bobleesj bobleesj left a comment

Choose a reason for hiding this comment

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

@sbillinge Ready for review!

@bobleesj bobleesj changed the title Use @property to prevent direction modification of all_arrays Use @property to prevent direct modification of all_arrays Dec 4, 2024
Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

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

pls see inline. I mostly just reviewed the tests.

actualdo = DiffractionObject(**inputs)
compare_dicts(actualdo.__dict__, expected)
actual_do = DiffractionObject(**inputs)
actual_dict = {
Copy link
Contributor

Choose a reason for hiding this comment

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

we normally do this work using actual_do.__dict__ so the test will fail if someone adds a new attribute to DiffractionObject

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, done.

actual_do = DiffractionObject()

# Attempt to directly modify the property
with pytest.raises(AttributeError, match="Direct modification of 'all_arrays' is not allowed."):
Copy link
Contributor

Choose a reason for hiding this comment

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

let's follow our favorite pattern. "what went wrong. How to fix it". The first part is done (though I would add the word "attribute" before 'all_arrays') but not the second? I think we want to point them towards using insert_diffraction_object or sthg, but give it some thought.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes done.

Copy link

codecov bot commented Dec 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.97%. Comparing base (ed9fb03) to head (d20d2bc).
Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #196      +/-   ##
==========================================
- Coverage   96.53%   93.97%   -2.56%     
==========================================
  Files           8        8              
  Lines         289      299      +10     
==========================================
+ Hits          279      281       +2     
- Misses         10       18       +8     
Files with missing lines Coverage Δ
tests/test_diffraction_objects.py 77.92% <100.00%> (-8.65%) ⬇️

@@ -341,5 +342,36 @@ def test_dump(tmp_path, mocker):

@pytest.mark.parametrize("inputs, expected", tc_params)
def test_constructor(inputs, expected):
actualdo = DiffractionObject(**inputs)
compare_dicts(actualdo.__dict__, expected)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we use a library DeepDiff to compare two dicts instead? We've manually defined compare_dicts and dicts_equal within test_diffraction_objects.py which can be replaced using DeepDiff across all .py files.

assert diff == {}


def test_all_array_getter():
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test getter for all_arrays

@bobleesj
Copy link
Contributor Author

bobleesj commented Dec 5, 2024

@sbillinge ready for review again

@sbillinge sbillinge merged commit 298e630 into diffpy:main Dec 5, 2024
3 checks passed
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.

overload the __setter__ in DiffractionObject to correctly update arrays
2 participants