-
Notifications
You must be signed in to change notification settings - Fork 112
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
LookupTransformer TypeError when default value is not exactly the type from the mapping #403
Comments
This type check is intended to block situations, where the mapped values and the default values are of completely different data type. For example, the former are Mis-matching data types can cause some hard-to-debug issues, because they render differently when printed to text documents. Very rare (affects only a small number of values), but when it does happen, it's very tricky to debug. Howver, in the this issue, I see that you're suggesting that the type check should be relaxed, so that in addition to strict type equality check, also type-is-a-subtype-of checks should be enabled. The case in point is that import numpy
print(type(1.0)) # <class 'float'>
print(type(numpy.float64(1.0))) # <class 'numpy.float64'>
print(type(1.0) == type(numpy.float64(1.0))) # False Makes sense. Will fix in the next release, but need to study Python's builti-ns vs Numpy type compatibility first. |
I've been thinking about this issue, and I've decided to keep the current behaviour unchanged (ie. "won't fix"). It still makes sense to require absolute type identity both on the key side and on the value side. Once we start mixing types, it becomes possible that the dict lookup-behaviour breaks down (ie. a If there's a chance of mixed type objects - most notably a mix of Python and Numpy scalars - then they should be normalized to either representation. I'd personally unpack Numpy scalars using the following helper: def _normalize(x):
if hasattr(x, "item"):
return x.item()
return x
raw_mapping = ...
raw_default_value = ...
transformer = LookupTransformer(mapping = {k : _normalize(v) for k, v in raw_mapping.items()}, default_value = _normalize(raw_default_value)) |
Hi,
Not sure if this is intended behavior, but the following initialization fails with TypeError:
This fails with:
I think this can be fixed by relaxing the inequality in this line with
not isinstance(default_value, v_type)
Thank you
The text was updated successfully, but these errors were encountered: