Skip to content

Commit 758e48c

Browse files
committed
idea for dequantify_input decorator
from @mhvk comment at astropy#10156 (comment) Signed-off-by: Nathaniel Starkman <[email protected]>
1 parent 5c488dc commit 758e48c

1 file changed

Lines changed: 59 additions & 5 deletions

File tree

‎astropy/units/decorators.py‎

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
)
2222
from .physical import _unit_physical_mapping
2323
from .quantity import Quantity
24-
from .unitspec import UnitSpecBase, isAnnotated, NullUnitSpec
24+
from .unitspec import UnitSpecBase, UnitSpec, isAnnotated, NullUnitSpec
2525

2626

2727
__equivalencies_default = []
@@ -55,6 +55,7 @@ def __call__(self, wrapped_function):
5555
# Extract the function signature for the function we are wrapping.
5656
wrapped_signature = inspect.signature(wrapped_function)
5757

58+
# Make the UnitSpec Annotations
5859
annotations = wrapped_function.__annotations__ # TODO!
5960

6061
# Define a new function to return in place of the wrapped one
@@ -76,16 +77,16 @@ def wrapper(*func_args, **func_kwargs):
7677
with add_enabled_equivalencies(equivalencies):
7778

7879
# this updates with external changes to __annotations__ :)
79-
for name, annot in wrapper.__annotations__.items():
80+
for name, antn in wrapper.__annotations__.items():
8081

81-
if isAnnotated(annot):
82-
unitspec = annot.__metadata__[0]
82+
if isAnnotated(antn):
83+
unitspec = antn.__metadata__[0]
8384
if isinstance(unitspec, UnitSpecBase):
8485
if name == "return":
8586
return_spec = unitspec
8687
else:
8788
ba.arguments[name] = unitspec(
88-
ba.arguments[name], dtype=annot.__origin__
89+
ba.arguments[name], dtype=antn.__origin__
8990
)
9091
else:
9192
pass # not actually a unitspec
@@ -97,6 +98,7 @@ def wrapper(*func_args, **func_kwargs):
9798
return return_spec(return_, dtype=return_spec.dtype)
9899

99100
# /with
101+
100102
# /def
101103

102104
wrapper.__annotations__ = annotations
@@ -106,6 +108,58 @@ def wrapper(*func_args, **func_kwargs):
106108

107109
# /def
108110

111+
112+
# /class
113+
114+
115+
class DeQuantityInput(QuantityInput):
116+
"""
117+
118+
TODO, maybe rename attribute "dequantify" to "to_value"
119+
120+
"""
121+
def __init__(
122+
self,
123+
func=None,
124+
strict_dimensionless=__strict_dimensionless_default,
125+
**kwargs
126+
):
127+
if "dequantify" in kwargs:
128+
raise ValueError("cannot pass kwarg 'dequantify'")
129+
130+
super().__init__(
131+
func=func, strict_dimensionless=strict_dimensionless, **kwargs
132+
)
133+
134+
self.dequantify = True
135+
136+
# /def
137+
138+
139+
def __call__(self, wrapped_function):
140+
141+
# use all the machinery from superclass
142+
wrapper = super().__call__(wrapped_function)
143+
144+
# but now need to ensure that everything
145+
_antns_ = dict()
146+
for name, antn in wrapper.__annotations__.items():
147+
148+
if isAnnotated(antn):
149+
unitspec = antn.__metadata__[0]
150+
if isinstance(unitspec, UnitSpecBase):
151+
if name == "return":
152+
_antns_[name] = UnitSpec(unitspec, action="from_value")
153+
else:
154+
_antns_[name] = UnitSpec(unitspec, action="to_value")
155+
156+
wrapper.__annotations__.update(_antns_)
157+
158+
return wrapper
159+
160+
# /def
161+
162+
109163
# /class
110164

111165

0 commit comments

Comments
 (0)