From 2cd48f1bc63032513c35b3139c1f9e72014dde9c Mon Sep 17 00:00:00 2001 From: liamhuber Date: Tue, 10 Sep 2024 12:11:12 -0700 Subject: [PATCH 1/2] [patch] Let pint-hinted input values pass through type hinting By checking the actual datatype associated with their "magnitude". I'm also boldly going to try setting the lower bound to the last version and hope it works. --- .ci_support/environment.yml | 1 + .ci_support/lower_bound.yml | 1 + pyiron_workflow/type_hinting.py | 3 +++ pyproject.toml | 1 + tests/unit/test_type_hinting.py | 4 ++++ 5 files changed, 10 insertions(+) diff --git a/.ci_support/environment.yml b/.ci_support/environment.yml index 12cce71bd..807e69612 100644 --- a/.ci_support/environment.yml +++ b/.ci_support/environment.yml @@ -8,6 +8,7 @@ dependencies: - executorlib =0.0.2 - graphviz =9.0.0 - pandas =2.2.2 +- pint =0.24.3 - pyiron_snippets =0.1.4 - python-graphviz =0.20.3 - toposort =1.10 diff --git a/.ci_support/lower_bound.yml b/.ci_support/lower_bound.yml index ceaa7c58a..1a362590b 100644 --- a/.ci_support/lower_bound.yml +++ b/.ci_support/lower_bound.yml @@ -8,6 +8,7 @@ dependencies: - executorlib =0.0.1 - graphviz =9.0.0 - pandas =2.2.0 +- pint =0.23.0 - pyiron_snippets =0.1.4 - python-graphviz =0.20.0 - toposort =1.10 diff --git a/pyiron_workflow/type_hinting.py b/pyiron_workflow/type_hinting.py index 82359e6e9..8796d79df 100644 --- a/pyiron_workflow/type_hinting.py +++ b/pyiron_workflow/type_hinting.py @@ -7,10 +7,13 @@ import typing from collections.abc import Callable +from pint import Quantity from typeguard import check_type, TypeCheckError def valid_value(value, type_hint) -> bool: + value = value.magnitude if isinstance(value, Quantity) else value # De-unit it + try: return isinstance(value, type_hint) except TypeError: diff --git a/pyproject.toml b/pyproject.toml index 788b0e32e..ae76b780a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ dependencies = [ "executorlib==0.0.2", "graphviz==0.20.3", "pandas==2.2.2", + "pint==0.24.3", "pyiron_snippets==0.1.4", "toposort==1.10", "typeguard==4.3.0", diff --git a/tests/unit/test_type_hinting.py b/tests/unit/test_type_hinting.py index 27fd4cfaa..189d8a1e9 100644 --- a/tests/unit/test_type_hinting.py +++ b/tests/unit/test_type_hinting.py @@ -1,6 +1,7 @@ import typing import unittest +from pint import UnitRegistry from pyiron_workflow.type_hinting import ( type_hint_is_as_or_more_specific_than, valid_value @@ -16,6 +17,8 @@ class Bar: def __call__(self): return None + ureg = UnitRegistry() + for hint, good, bad in ( (int | float, 1, "foo"), (typing.Union[int, float], 2.0, "bar"), @@ -29,6 +32,7 @@ def __call__(self): (typing.Callable, Bar(), Foo()), (tuple[int, float], (1, 1.1), ("fo", 0)), (dict[str, int], {'a': 1}, {'a': 'b'}), + (int, 1 * ureg.seconds, 1.0 * ureg.seconds) # Disregard unit, look@type ): with self.subTest(msg=f"Good {good} vs hint {hint}"): self.assertTrue(valid_value(good, hint)) From d229720f9dac89140814a96dd1e82c45dd046b3b Mon Sep 17 00:00:00 2001 From: pyiron-runner Date: Tue, 10 Sep 2024 19:12:36 +0000 Subject: [PATCH 2/2] [dependabot skip] Update env file --- .binder/environment.yml | 1 + docs/environment.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.binder/environment.yml b/.binder/environment.yml index 12cce71bd..807e69612 100644 --- a/.binder/environment.yml +++ b/.binder/environment.yml @@ -8,6 +8,7 @@ dependencies: - executorlib =0.0.2 - graphviz =9.0.0 - pandas =2.2.2 +- pint =0.24.3 - pyiron_snippets =0.1.4 - python-graphviz =0.20.3 - toposort =1.10 diff --git a/docs/environment.yml b/docs/environment.yml index b64d88c16..720306b4f 100644 --- a/docs/environment.yml +++ b/docs/environment.yml @@ -14,6 +14,7 @@ dependencies: - executorlib =0.0.2 - graphviz =9.0.0 - pandas =2.2.2 +- pint =0.24.3 - pyiron_snippets =0.1.4 - python-graphviz =0.20.3 - toposort =1.10