Skip to content

Commit

Permalink
feat: Allow none as default parameter (#10)
Browse files Browse the repository at this point in the history
* feat: allow None as default parameter

* chore: push version
  • Loading branch information
jfaldanam authored Nov 8, 2023
1 parent 730edbc commit efebd1e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "eidos"
version = "0.99.2"
version = "0.99.3"
authors = [
{ name="José F. Aldana Martín", email="jfaldanam@uma.es" },
]
Expand Down
18 changes: 12 additions & 6 deletions src/eidos/validation/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,19 @@ def validate_input_schema(
if required is None or required: # If required is None, it is defined as True
if schema_name not in arguments:
raise ValueError(f"Argument {schema_name} not found in arguments")

if not validate_type(arguments[schema_name], type_, allow_none=False):
raise TypeError(
f"Argument {schema_name} is not of type {type_}. "
f"Got {type(arguments[schema_name])} instead."
)
else: # If required is False, add the default value
if schema_name not in arguments:
arguments[schema_name] = parameter["default"]
if not validate_type(arguments[schema_name], type_):
raise TypeError(
f"Argument {schema_name} is not of type {type_}. Either change the "
"default or provide an alternative value"
)

# None must be a valid type if it's marked as default.
if not validate_type(arguments[schema_name], type_, allow_none=True):
raise TypeError(
f"Argument {schema_name} is not of type {type_}. "
f"Got {type(arguments[schema_name])} instead."
)
return arguments
6 changes: 5 additions & 1 deletion src/eidos/validation/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ def split_type_from_generic(type_: str) -> tuple[str, str | None]:
return main_type, generic_type


def validate_type(value: Any, type_: str) -> bool:
def validate_type(value: Any, type_: str, allow_none: bool = False) -> bool:
"""Validate the type of a value.
Args:
value (Any): Value to validate.
type (str): Type to validate the value against.
allow_none (bool): Whether to allow None as a valid value.
Returns:
bool: True if the value is of the specified type, False otherwise.
"""
if allow_none:
if value is None:
return True

main_type, generic_type = split_type_from_generic(type_)
if generic_type and main_type == "list":
Expand Down

0 comments on commit efebd1e

Please sign in to comment.