Skip to content
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

[patch] Let pint-hinted input values pass through type hinting #451

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .binder/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .ci_support/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .ci_support/lower_bound.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pyiron_workflow/type_hinting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_type_hinting.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"),
Expand All @@ -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))
Expand Down
Loading