-
Notifications
You must be signed in to change notification settings - Fork 2
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
Supporting different file handle types is too difficult #140
Comments
Maybe the type check in I have also seen trouble with Python 3.12 type aliases in relation to that, but I need to try that again as I do not remember the details. |
I think checking if the type if a subclass of the desired type should be enough. NewInt = NewType("NewInt", int)
a: int = NewInt(1) # pass the static type check
b: NewInt = 1 # does not pass the static type check Hmm... but then the |
The IO objects are a bit strange. |
Hmm.... Maybe we can define our own protocol/meta class in this particular case.... |
Some reading material: python/typing#829 |
The |
I would appreciate some advice on how to implement the affected functions. Should we support |
Can't we define that as the domain type?
|
This is not allowed: from typing import NewType
from pathlib import Path
Filename = NewType("Filename", str | Path) gives this error with mypy:
See https://mypy.readthedocs.io/en/stable/error_code_list.html#check-the-target-of-newtype-valid-newtype Using a type alias does not work either: import sciline as sl
from typing import TypeAlias
from pathlib import Path
Filename: TypeAlias = str | Path
pl = sl.Pipeline([], params={Filename: "test.txt"}) mypy complains with (for the params of
|
Does it work with Python 3.12 type aliases? |
Not yet:
|
But in any case, waiting for that would mean that all our projects fail type checks for the next 2 years until 3.12 becomes our minimum Python version. |
In the context of scipp/essreduce#8 we considered supporting opening files once and passing the file handle to the various loaders. Supporting this is tricky because there are many different file handle types:
So supporting these with the current system requires
where the param key needs to be adjusted for the concrete type of file handle. This is bad UX and Python has a workaround. For normal functions, we would annotate them as
but that is not possible because
Pipeline
requires an exact type match for parameters, i.e.fails.
This gets exasperated when
load
needs to also support a file path and maybe an openh5py.File
orscippnexus.File
.I don't know a good solution at this point. But automatic type conversion in parameters might help (as proposed as part of #139).
The text was updated successfully, but these errors were encountered: