Skip to content
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

Merged
merged 2 commits into from
Jan 10, 2024
Merged

Complete mypy type coverage for entire codebase #403

merged 2 commits into from
Jan 10, 2024

Conversation

janbjorge
Copy link
Contributor

@janbjorge janbjorge commented Jan 8, 2024

Merge after: #405

subfolder: str = ""
tagname: str = ""
timedata: Optional[List[list]] = None
timedata: List[List] | None = None
Copy link
Contributor Author

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.

@janbjorge janbjorge changed the title Fully typed and pyupgrade --py3-plus Fully typed Jan 8, 2024
@janbjorge janbjorge self-assigned this Jan 9, 2024
@janbjorge janbjorge mentioned this pull request Jan 9, 2024
@janbjorge janbjorge added the enhancement New feature or request label Jan 9, 2024
@janbjorge janbjorge marked this pull request as ready for review January 9, 2024 12:09
Copy link
Collaborator

@mferrera mferrera left a 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.

.github/workflows/mypy.yml Outdated Show resolved Hide resolved
.github/workflows/mypy.yml Outdated Show resolved Hide resolved
src/fmu/dataio/_design_kw.py Outdated Show resolved Hide resolved
src/fmu/dataio/_filedata_provider.py Outdated Show resolved Hide resolved
src/fmu/dataio/_filedata_provider.py Show resolved Hide resolved
src/fmu/dataio/_objectdata_provider.py Outdated Show resolved Hide resolved
src/fmu/dataio/_objectdata_provider.py Outdated Show resolved Hide resolved
src/fmu/dataio/_utils.py Outdated Show resolved Hide resolved
src/fmu/dataio/dataio.py Outdated Show resolved Hide resolved
Comment on lines +910 to +915
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.
Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Member

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
Copy link
Contributor Author

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.

Copy link
Collaborator

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 ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

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?

Copy link
Contributor Author

@janbjorge janbjorge Jan 10, 2024

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.

Copy link
Member

@perolavsvendsen perolavsvendsen left a 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: 🚀

Copy link
Collaborator

@mferrera mferrera left a 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
Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Collaborator

@jcrivenaes jcrivenaes left a 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
Copy link
Collaborator

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)

Copy link
Contributor Author

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:
Copy link
Collaborator

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

Copy link
Contributor Author

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 ?
Copy link
Collaborator

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?

.DS_Store Outdated Show resolved Hide resolved
@janbjorge janbjorge changed the title Fully typed Complete mypy type coverage for entire codebase Jan 10, 2024
@janbjorge janbjorge merged commit cafb98f into equinor:main Jan 10, 2024
11 checks passed
@janbjorge janbjorge deleted the add-mypy branch January 10, 2024 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants