-
Notifications
You must be signed in to change notification settings - Fork 25
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
Strong Typing #58
Comments
PICMI is very much relying on duck typing. A For any arguments that take a sequence, anything that has an indexing operation and a length method should work. Again, if a implementing code has special needs, it should take care to convert to an acceptable type. |
"duck typing" I think is the right concept to bring up. So essentially we would expect a check similar to this one to pass: def check_type_ducky(expected_type: type, checked_value: typing.Any) -> None:
"""
performs duck-typing based typecheck
Tries to cast checked_value to expected_type:
If ok passes silently, else raises.
**Does not modify checked_value.**
:param expected_type: type that checked_value must be castable to
:param checked_value: value to be checked
:raise ValueError: if checked_value is not castable to expected_type
"""
try:
expected_type(checked_value)
except ValueError:
raise ValueError(f"can't cast to {expected_type}: {checked_value}")
Which then leaves the question: Do we want such a check? |
I think that works with strong typing. We can request an arbitrary numeric type but disallow string. That will make implementations also way easier, e.g., they can do range checks on the numeric input without interpreting a string - and don't need to treat different type handling for most parameters. Some arguments we have will stay very generic, e.g., function generators, etc. That is the minority of options though and can start with Of course, individual codes can add more constrains and "reject" all but a subset of types, if they do not implement them. This should not often be the case, but would also be a valid way to handle a few very generic inputs, let's say, custom hook classes for custom initializers (again, not the majority and not numeric input). 👍 for adding type checks, as discussed last time. For examples, see
|
The reference implementation does not specify the used types, which makes portability really hard for implementing simulations.
Some potential conflicts (from the top of my head):
I'd like to suggest that the reference implementation specifies the minimum supported type, and potentially ensures that the given value can be converted to this type, e.g. when a tuple is requested, lists or numpy arrays of correct length are also accepted.
An implementation is free to support more types, but should make that clear by redefining the property, e.g.:
See PEP 484 for type annotations in python.
The text was updated successfully, but these errors were encountered: