Motivation
In PyGMT, most parameters default to False (for boolean-only parameters) or None (for non-boolean parameters), which means the parameters are not given and should not be used in building the CLI option string. We usually need to check if a parameter is given, like below
if value is not False: # For boolean-only parameters
...
if value is not None: # For non-boolean parameters
...
if value is not None and value is not False: # A more general check
...
This repetitive pattern:
- Reduces code readability
- Makes maintenance harder [Sometimes we may incorrectly use
if value is not None when value is a boolean type]
- Makes the intent less clear to new contributors
Solution
Introduced a new is_given() helper function to simplify parameter validation across PyGMT and refactored existing code to use it consistently.
def is_given(value: Any) -> bool:
return value is not None and value is not False
Then we should
- replace
x is not None/x is not False/x is not None and x is not False with is_given(x).
- replace
x is None or x is False to not is_given(x)
This is just an internal change and doesn't break any existing behavior.
Comments?
Motivation
In PyGMT, most parameters default to
False(for boolean-only parameters) orNone(for non-boolean parameters), which means the parameters are not given and should not be used in building the CLI option string. We usually need to check if a parameter is given, like belowThis repetitive pattern:
if value is not Nonewhenvalueis a boolean type]Solution
Introduced a new
is_given()helper function to simplify parameter validation across PyGMT and refactored existing code to use it consistently.Then we should
x is not None/x is not False/x is not None and x is not Falsewithis_given(x).x is None or x is Falsetonot is_given(x)This is just an internal change and doesn't break any existing behavior.
Comments?