-
Notifications
You must be signed in to change notification settings - Fork 17
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
Type hinting #28
base: master
Are you sure you want to change the base?
Type hinting #28
Conversation
On Python<3.5, the typing module is an external dependency, so we can't import from jdcal in setup.py because the dependency hasn't been installed yet. So the version must be moved to a separate file and then both setup.py and jdcal.py both import from it.
This was causing build failures on ppc64le, see: https://travis-ci.community/t/permission-issue-while-building-wheels- for-various-python-packages/7822/14
In PEP 561, it is stated that we need to include the file py.typed in order for type checkers to be able to use the type hints: https://www.python.org/dev/peps/pep-0561/#packaging-type-information It also says that module-only distributions are not supported and so the code needs to be refactored into a package-based distribution. Therefore I moved jdcal.py into its own package directory. This then necessitated importing its functions in the __init__.py so that the API won't change (i.e. keep jdcal.is_leap rather than jdcal.jdcal.is_leap). But mypy --strict isn't happy with this unless you also explicitly re- export everything via __all__ or do from X import Y as Y on everything. I don't understand why this extra layer of redundancy is required but I can't find a way around it: https://mypy.readthedocs.io/en/latest/config_file.html#confval-implicit _reexport
Hello @ckp95 That is a lot of work you have put in! I appreciate that. Do we really need this? I am trying to keep the code base as simple as possible, to ensure So my opinion is that we don't merge this PR. I would like to hear your opinion before I close this. Thanks again for your contribution. |
There are no problems wrt Python versions -- I made sure to use the comment version of type hints so that it works on everything back to Python 2.7, you can see this in the CI tests. It introduces no further external dependencies for users of the package. All the typing stuff comes from the standard library. There is the addition of I think this is an improvement to the package, and will benefit projects that rely on |
FWIW, I'm in favor of seeing type hints added. Using the comment syntax ensures the code can remain compatible with legacy EOL Pythons. |
You have convinced me that this is a good thing!
|
|
If you want, you should be able to avoid the if False:
from typing import Tuple mypy understands this and will recognize the |
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.
Overall, really nice. Thanks for contributing this.
@@ -1,3 +1,4 @@ | |||
include README.rst | |||
include LICENSE.txt | |||
include *jdcal.py | |||
include src/jdcal/*.py |
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.
This should be included automatically by setuptools. We only need to mention the test files:
include src/jdcal/*.py | |
include tests/*.py |
@@ -35,5 +35,9 @@ | |||
'Programming Language :: Python :: Implementation :: CPython', | |||
'Programming Language :: Python :: Implementation :: PyPy', | |||
], | |||
py_modules=["jdcal"] | |||
# py_modules=["jdcal"], |
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.
Don't need to keep this.
# py_modules=["jdcal"], |
@@ -35,5 +35,9 @@ | |||
'Programming Language :: Python :: Implementation :: CPython', | |||
'Programming Language :: Python :: Implementation :: PyPy', | |||
], | |||
py_modules=["jdcal"] | |||
# py_modules=["jdcal"], | |||
packages=find_packages("src"), |
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 the package has no subpackages, find_packages
is overkill, IMO:
packages=find_packages("src"), | |
packages=["jdcal"], |
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.
Won't some of these suggestions change since we have decided to go with the latest recommendation
of using setup.cfg
to specify such information and keeping setup.py
to the minimum?
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.
When adjusting setup.cfg
there will be a choice of using find_packages
or listing the packages. IMO, listing them is better as the layout is relatively simple. So I think the comment still applies, but just to the new declarative syntax.
py_modules=["jdcal"] | ||
# py_modules=["jdcal"], | ||
packages=find_packages("src"), | ||
package_dir={"": "src"}, |
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.
You should also mention the package_data
here:
package_dir={"": "src"}, | |
package_data={"jdcal": ["py.typed"]}, | |
package_dir={"": "src"}, |
@@ -0,0 +1,14 @@ | |||
#!/usr/bin/env python |
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.
This file is a package and has no external side effects, it should not be executable as is.
#!/usr/bin/env python |
@@ -0,0 +1,4 @@ | |||
#!/usr/bin/env python |
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.
#!/usr/bin/env python |
|
||
[testenv:mypy] | ||
deps = mypy | ||
commands = mypy --strict src/jdcal/jdcal.py |
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.
We should type check the entire package, not just one file.
commands = mypy --strict src/jdcal/jdcal.py | |
commands = mypy --strict src/ |
|
||
[testenv:docstyle] | ||
deps = pydocstyle | ||
commands = pydocstyle jdcal.py test_jdcal.py | ||
commands = pydocstyle src/jdcal/jdcal.py |
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.
Same, run on full package.
@@ -13,8 +13,12 @@ deps = | |||
|
|||
[testenv:codestyle] | |||
deps = pycodestyle | |||
commands = pycodestyle --ignore=E722,E501 jdcal.py test_jdcal.py | |||
commands = pycodestyle --ignore=E722,E501 src/jdcal/jdcal.py tests/test_jdcal.py |
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.
run on entire directory.
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
# see https://mypy.readthedocs.io/en/latest/config_file.html#confval-implicit_reexport |
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.
IMO, it is more conventional to express exports using __all__
__all__ = ["__version__", "MJD_0", ...]
@ckp95 this would be a very interesting addition. |
I've added type hints to this package, so that static type checkers like mypy will work.
I thought that it would be a simple change, but it turned out to require reorganizing a lot of the package structure. I couldn't figure out a way to avoid doing this unfortunately.
I also altered the travis.yml to fix an unrelated build failure.