-
Notifications
You must be signed in to change notification settings - Fork 8
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
clarify string types in pyi #118
Conversation
It amazes me what can be done by type annotations alone! I think I like the "intermediate" approach. Would I be right that this would trigger type check errors if you pass the wrong kind of string that was previously returned by a |
yeah, the intermediate approach (which I now changed this to) will basically never result in static type errors, unless the user (such as In other words, as this PR currently stands, the following will still be just fine from a typing perspective. core.loadDevice('WhateverYouWant') but, if you want to, you could do this to indicate that a parameter is only valid if it was returned from the core: import pymmcore
core = pymmcore.CMMCore()
def strict_init(device_label: pymmcore.ValidDeviceLabel) -> None:
core.initializeDevice(device_label)
# Argument 1 to "strict_init" has incompatible type "str"; expected "ValidDeviceLabel"
strict_init("Camera")
# OK
strict_init(core.getLoadedDevices()[0]) |
I see, but I think that is not too big a downside, especially if it's just one more click. Much more helpful to have the string category clarified, as you say. But (now that I've seen how this looks) how would this compare to doing the following?
The main advantage being simplicity (validity can change over time so cannot truly be enforced by typing anyway). But it also happens to solve the beginner problem of not being clear that a |
brilliant :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good!
There are lots of strings used throughout the CMMCore API, but using the wrong type of string in the wrong place can raise exceptions. This PR clarifies those string names using type hints. At the moment, I'm actually using
NewType
, but may relax that before this merges. (NewType actually triggers a static typing error if you use anything other than the actual newtype, which is rather strict, but ensures safety). A safer intermediate would be to use NewType for string types in return values, but useTypeAlias
strings for parameters used as inputs