-
Notifications
You must be signed in to change notification settings - Fork 15
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
Complete mypy type coverage for entire codebase #403
Conversation
src/fmu/dataio/dataio.py
Outdated
subfolder: str = "" | ||
tagname: str = "" | ||
timedata: Optional[List[list]] = None | ||
timedata: List[List] | None = None |
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.
These must be typing.List
due to how _validate_variable(...)
is built. Can be converted to list
when that functions is cleaned up.
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.
Super nice to see the full code base get fully typed like, right away. Wow. I tried to speedrun this review to give some feedback before end of day so I mean no offense with any low quality comments 😄
I'm going to defer approving more substantive PRs since I think @perolavsvendsen and @jcrivenaes should probably be doing that until we're more formally in it.
toutfile, md5 = export_file_compute_checksum_md5( | ||
obj, | ||
outfile, | ||
outfile.suffix, | ||
flag=useflag, # type: ignore | ||
# BUG(?): Looks buggy, if flag is bool export_file will blow up. |
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.
Might be worth making an issue over this checksum function, perhaps a few things that could be improved based upon your comments in here
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.
Yeah, i have a few other potential bugs that need tickets as well.
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.
@@ -1,4 +1,5 @@ | |||
# flake8: noqa | |||
# type: ignore |
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.
I think this file can be simplified a bit, looks like there is some relics of py27 in here.
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.
True, is was borrowed from another library in the python 3.4 days
@@ -761,8 +766,8 @@ def _establish_pwd_rootpath(self): | |||
if self._inside_rms or INSIDE_RMS or "RUN_DATAIO_EXAMPLES" in os.environ: | |||
self._rootpath = (self._pwd / "../../.").absolute().resolve() | |||
logger.info("Run from inside RMS (or pretend)") | |||
self._inside_rms = True | |||
# make some extra keys in settings: | |||
# BUG(?): Should be ExportData._inside_rms ? |
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.
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.
Yes a class variable initially; but I think the instance variable will just behave like (i.e. borrow from) the class variable?
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.
Im not sure, i have todo some research. Will followup on this in another PR/issue. My gut tells me this works due what Jan wrote above. Its just a bit counterintuitive due to it being a ClassVar.
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.
As far as I can see, no (expected) impact on runtime. Nice clean up. See comments, also would be nice to have opinions from @jcrivenaes before merging. But from my perspective: 🚀
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.
LGTM, very nice. You might consider leaving notes about potential bugs as issues rather than comments
"""Export and compute checksum, with possibility to use a tmp file.""" | ||
|
||
usefile = filename | ||
usefile: Path | None = filename |
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.
if tmp:
tmpdir = tempfile.TemporaryDirectory()
filename = Path(tmpdir.name) / "tmpfile"
export_file(obj, filename, extension, flag=flag)
checksum = md5sum(filename)
if tmp:
tmpdir.cleanup()
return filename if not tmp else None, checksum
Just an idea, helps to keep the typing less optional
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.
Good input, but i wonder this functions should take in buffer or something. Creating a file here seems a bit odd to me.
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.
Nice work; seems that you also have uncovered some bugs (which is probably one achievement from doing type annotations)
if res is not None: | ||
return res | ||
with contextlib.suppress(ValueError): | ||
return float(value) | ||
|
||
return value |
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.
Will we ever reach this line? (unclear for me)
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.
Int from string conversion will fail if the value contains a . or is "-inf/inf/nan".
Ex.
>>> float("nan")
nan
>>> int("nan")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'nan'
>>>
@@ -492,7 +497,7 @@ def parse_timedata(datablock: dict, isoformat=True): | |||
if isinstance(datablock["time"], list): | |||
date0 = datablock["time"][0]["value"] | |||
|
|||
if len(datablock["time"] == 2): | |||
if len(datablock["time"]) == 2: |
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.
OBS thanks for fixing this bug
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.
Sneaky, but all thanks to mypy.
@@ -761,8 +766,8 @@ def _establish_pwd_rootpath(self): | |||
if self._inside_rms or INSIDE_RMS or "RUN_DATAIO_EXAMPLES" in os.environ: | |||
self._rootpath = (self._pwd / "../../.").absolute().resolve() | |||
logger.info("Run from inside RMS (or pretend)") | |||
self._inside_rms = True | |||
# make some extra keys in settings: | |||
# BUG(?): Should be ExportData._inside_rms ? |
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.
Yes a class variable initially; but I think the instance variable will just behave like (i.e. borrow from) the class variable?
Merge after: #405