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

Add types hints for ingredients and experiment #543

Merged
merged 4 commits into from
Jul 31, 2019

Conversation

gabrieldemarmiesse
Copy link
Collaborator

@gabrieldemarmiesse gabrieldemarmiesse commented Jul 31, 2019

Adding type hints on the API surface allows IDEs and static code analysis tools to do a better job at detecting user errors.

It's also quite informative when using IDEs as you can get hints in the auto-completion.

@coveralls
Copy link

coveralls commented Jul 31, 2019

Coverage Status

Coverage increased (+0.04%) to 85.502% when pulling 96eb5f5 on gabrieldemarmiesse:add_type_hints into 6df989e on IDSIA:master.

@@ -36,6 +39,8 @@

PYTHON_IDENTIFIER = re.compile("^[a-zA-Z_][_a-zA-Z0-9]*$")

PathType = Union[str, bytes, Path]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we use os.PathLike instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can't actually find a clear answer to this question. I though about looking into the python stub file for os.path but it's not actually informative. https://github.com/python/typeshed/blob/edee7cfb3cc5af16afa557f1279c22ee6663a61f/stdlib/3/os/path.pyi#L14 . I'm open to any idea.

From simple experiments, it seems that
isinstance(pathlib.Path('.'), os.PathLike) returns true but
isinstance('dodo', os.PathLike) returns False.

Any guidance is welcome.

Copy link
Collaborator

@JarnoRFB JarnoRFB Jul 31, 2019

Choose a reason for hiding this comment

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

True. Also this discussion python/typing#402 seems to state that there is the idea to have a protocol class for this at some time, but currently it is something different. I would then probably use
PathType = Union[str, bytes, os.PathLike]
That's also what this section in the original PEP states https://www.python.org/dev/peps/pep-0519/#provide-specific-type-hinting-support. The alternative would be to use only PathLike and force the user to wrap their pathlib.Path though I do not really see so much benefit in it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should we slowly migrate the internals of sacred to pathlib? That would make the code easier to read. We can do so while staying backward compatible of course.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

os.pathlike is not available for python 3.5. That solves the issue. Going back to pathlib.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright, migrating sacred internals to pathlib sounds like a very good idea!

@JarnoRFB
Copy link
Collaborator

Great idea!

@JarnoRFB JarnoRFB changed the title Added types hints for ingredients and experiment. Add types hints for ingredients and experiment. Jul 31, 2019
@JarnoRFB JarnoRFB changed the title Add types hints for ingredients and experiment. Add types hints for ingredients and experiment Jul 31, 2019
gabrieldemarmiesse added 2 commits July 31, 2019 16:35
@JarnoRFB JarnoRFB merged commit b9df680 into IDSIA:master Jul 31, 2019
@gabrieldemarmiesse gabrieldemarmiesse deleted the add_type_hints branch July 31, 2019 15:25
@Qwlouse
Copy link
Collaborator

Qwlouse commented Aug 1, 2019

Awesome, thanks! Adding type-annotations and eventually mypy checking to the tests has been on my list too. I'm very happy that you got started on that already!

And +1 for pathlib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants