Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #188 +/- ##
===========================================
+ Coverage 81.15% 81.24% +0.08%
===========================================
Files 52 52
Lines 4267 4287 +20
Branches 740 745 +5
===========================================
+ Hits 3463 3483 +20
Misses 624 624
Partials 180 180
Flags with carried forward coverage won't be shown. Click here to find out more.
|
| try: | ||
| self._convert_unit(self._desired_unit) | ||
| except Exception as e: | ||
| raise UnitError(f'Failed to convert unit from {temporary_parameter.unit} to {self._desired_unit}: {e}') |
There was a problem hiding this comment.
Should we completely bail out on bad unit conversion? Maybe return the original value with the original unit and warn users of the failure of the unit conversion.
There was a problem hiding this comment.
Hmm, good question. I tend to think yes, since something must have gone wrong if the user expects a unit that is incompatible with the calculated unit.
There was a problem hiding this comment.
I would move this check down to the make_dependent_on method and do a _revert_dependency if it fails.
Like with all our other checks. The update method should have no checks.
The dependency_result can be converted to try it out.
There was a problem hiding this comment.
I moved the check. Update only checks if self._desired_unit is not None
damskii9992
left a comment
There was a problem hiding this comment.
I wouldn't simply call this argument "unit", it is too vague and can be understood as it can be anything. Maybe "unit_format"? Or something similar.
|
|
||
| if self._independent: | ||
| raise AttributeError('This is an independent parameter, desired unit can only be set for dependent parameters.') | ||
| self._desired_unit = unit_str |
There was a problem hiding this comment.
This is missing a check on the type of unit_str
| try: | ||
| self._convert_unit(self._desired_unit) | ||
| except Exception as e: | ||
| raise UnitError(f'Failed to convert unit from {temporary_parameter.unit} to {self._desired_unit}: {e}') |
There was a problem hiding this comment.
I would move this check down to the make_dependent_on method and do a _revert_dependency if it fails.
Like with all our other checks. The update method should have no checks.
The dependency_result can be converted to try it out.
The argument 'unit' is already used at least in tests, e.g. the following, so I'm hesitant to change it. Thoughts? |
Why does it matter that it is already used in tests? O.o That would literally be the case for any name changes we do XD |
Well it might not matter, it's just that that implies that people might rely on this behavior. But maybe we shouldn't allow a unit in Edit: the point is that |
Ahh, in this case it is simply the test that is wrong. The "unit" is a kwarg passed to the Parameter constructor, but it gets overwritten so it is a redundant argument. |
|
|
||
| if self._desired_unit is not None: | ||
| try: | ||
| dependency_result._convert_unit(self._desired_unit) | ||
| except Exception as e: | ||
| raise UnitError(f'Failed to convert unit from {dependency_result.unit} to {self._desired_unit}: {e}') | ||
|
|
There was a problem hiding this comment.
When the unit conversion fails, the code raises a UnitError but does not call _revert_dependency() first, leaving the parameter in an inconsistent state if unit conversion fails.
This is inconsistent with all other validation checks in make_dependent_on() that properly revert the dependency state before raising.
There was a problem hiding this comment.
I didn't realise dependency_result changed self. Adding _revert_dependency() before raising the error.
| self, dependency_expression: str, dependency_map: Optional[dict] = None, unit: str | sc.Unit | None = None | ||
| ) -> None: | ||
| """ | ||
| Make this parameter dependent on another parameter. This will overwrite the current value, unit, variance, min and max. |
There was a problem hiding this comment.
There's no validation on unit type. A user can pass unit=1234 and this will get accepted.
There was a problem hiding this comment.
Added validation.
| '_dependency_string': self._dependency_string, | ||
| '_dependency_map': self._dependency_map, | ||
| '_dependency_interpreter': self._dependency_interpreter, | ||
| '_clean_dependency_string': self._clean_dependency_string, |
There was a problem hiding this comment.
You forgot to add _desired_unit to previous dependency
| self._convert_unit(self._desired_unit) | ||
|
|
There was a problem hiding this comment.
_convert_unit doesn't catch any exceptions and we aren't checking anything here. Maybe add some exception handling so it doesn't bubble up potentially leaving the parameter in a weird state?
There was a problem hiding this comment.
This is on purpose. To keep the _update method as fast as possible, all the checks are made in the make_dependent_on method. If this method succeeds, then the _update will also always succeed.
This is also how we did it for the other updates :)
| normal_parameter.value == 4 | ||
| self.compare_parameters(normal_parameter, 2*independent_parameter) | ||
|
|
||
|
|
There was a problem hiding this comment.
There's no test verifying that _desired_unit is properly handled when calling make_dependent_on on an already-dependent parameter (i.e., overwriting an existing dependency).
There was a problem hiding this comment.
Good point! Expanded the test_dependent_parameter_make_dependent_on_with_desired_unit test
Because all other If you/the the team prefer a different name ( I'm also curious to hear what @rozyczko and @AndrewSazonov think about this :) |
The problem is that the We pass kwargs to the And this is why I want this "desired_unit" or "preffered_unit" to be named differently, because it is different from the regular "unit". |
Added desired units for the dependent parameter constructors, and a method to change it later:
set_desired_unit