From a8f8fc3200d59e21e78e69c8ca275f7fcc8dea52 Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 20:30:04 +0000 Subject: [PATCH 01/11] add type hints --- jdcal.py | 15 ++++++++------- tox.ini | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/jdcal.py b/jdcal.py index cb1661c..a80caa9 100644 --- a/jdcal.py +++ b/jdcal.py @@ -41,6 +41,7 @@ from __future__ import division from __future__ import print_function import math +from typing import Tuple __version__ = "1.4.1" @@ -48,7 +49,7 @@ MJD_JD2000 = 51544.5 -def is_leap(year): +def is_leap(year): # type: (int) -> int """Leap year or not in the Gregorian calendar.""" x = year % 4 y = year % 100 @@ -59,7 +60,7 @@ def is_leap(year): return not x and (y or not z) -def gcal2jd(year, month, day): +def gcal2jd(year, month, day): # type: (int, int, int) -> Tuple[float, float] """Gregorian calendar date to Julian date. The input and output are for the proleptic Gregorian calendar, @@ -179,7 +180,7 @@ def gcal2jd(year, month, day): day = int(day) a = int((month - 14) / 12.0) - jd = int((1461 * (year + 4800 + a)) / 4.0) + jd = int((1461 * (year + 4800 + a)) / 4.0) # type: float jd += int((367 * (month - 2 - 12 * a)) / 12.0) x = int((year + 4900 + a) / 100.0) jd -= int((3 * x) / 4.0) @@ -190,7 +191,7 @@ def gcal2jd(year, month, day): return MJD_0, jd -def jd2gcal(jd1, jd2): +def jd2gcal(jd1, jd2): # type: (float, float) -> Tuple[int, int, int, float] """Julian date to Gregorian calendar date and time of day. The input and output are for the proleptic Gregorian calendar, @@ -289,7 +290,7 @@ def jd2gcal(jd1, jd2): return int(year), int(month), int(day), f -def jcal2jd(year, month, day): +def jcal2jd(year, month, day): # type: (int, int, int) -> Tuple[float, float] """Julian calendar date to Julian date. The input and output are for the proleptic Julian calendar, @@ -344,7 +345,7 @@ def jcal2jd(year, month, day): month = int(month) day = int(day) - jd = 367 * year + jd = 367 * year # type: float x = int((month - 9) / 7.0) jd -= int((7 * (year + 5001 + x)) / 4.0) jd += int((275 * month) / 9.0) @@ -356,7 +357,7 @@ def jcal2jd(year, month, day): return MJD_0, jd -def jd2jcal(jd1, jd2): +def jd2jcal(jd1, jd2): # type: (float, float) -> Tuple[int, int, int, float] """Julian calendar date for the given Julian date. The input and output are for the proleptic Julian calendar, diff --git a/tox.ini b/tox.ini index 9e4d1cf..71240e2 100644 --- a/tox.ini +++ b/tox.ini @@ -1,16 +1,27 @@ [tox] project = jdcal -envlist = py{27,34,35,36,37,38,py,py3}, codestyle, docstyle +envlist = py{27,34,35,36,37,38,py,py3}, codestyle, docstyle, mypy [testenv] deps = pytest commands = pytest +[testenv:py{27,34}] +deps = + typing + pytest + astropy + [testenv:py{27,34,35,36,37,38}] deps = pytest astropy +[testenv:pypy] +deps = + typing + pytest + [testenv:codestyle] deps = pycodestyle commands = pycodestyle --ignore=E722,E501 jdcal.py test_jdcal.py @@ -18,3 +29,7 @@ commands = pycodestyle --ignore=E722,E501 jdcal.py test_jdcal.py [testenv:docstyle] deps = pydocstyle commands = pydocstyle jdcal.py test_jdcal.py + +[testenv:mypy] +deps = mypy +commands = mypy --strict jdcal.py From 3df42fe5a98fc4e5de35a4d6c45620a40733fa8d Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 21:08:48 +0000 Subject: [PATCH 02/11] install typing in ppc64le --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4c34343..34cb2a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -63,6 +63,6 @@ before_install: install: - pip install tox - - if [[ "$TRAVIS_CPU_ARCH" == "ppc64le" ]]; then pip install pytest; pip install astropy; fi + - if [[ "$TRAVIS_CPU_ARCH" == "ppc64le" ]]; then pip install pytest; pip install astropy; pip install typing; fi script: tox From 72e00b9a78ed37e22352b09e6e5a8dba452233c9 Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 21:57:54 +0000 Subject: [PATCH 03/11] move version number to __init__.py --- __init__.py | 4 ++++ jdcal.py | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 __init__.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..a1bab83 --- /dev/null +++ b/__init__.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +version = "1.4.1" diff --git a/jdcal.py b/jdcal.py index a80caa9..b0e7d47 100644 --- a/jdcal.py +++ b/jdcal.py @@ -43,8 +43,6 @@ import math from typing import Tuple -__version__ = "1.4.1" - MJD_0 = 2400000.5 MJD_JD2000 = 51544.5 From 95d6ab21293b048ca25d96e75922a2d2404adf5b Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 21:58:18 +0000 Subject: [PATCH 04/11] add __init__.py to MANIFEST.in --- MANIFEST.in | 1 + 1 file changed, 1 insertion(+) diff --git a/MANIFEST.in b/MANIFEST.in index c30aef0..8d46c00 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,4 @@ include README.rst include LICENSE.txt include *jdcal.py +include __init__.py From c78e5f752f63f2fc67ab08d1a954c946eebf2aa8 Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 21:58:50 +0000 Subject: [PATCH 05/11] add typing to setup.py, change version import --- setup.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index eade07b..2c5511c 100644 --- a/setup.py +++ b/setup.py @@ -2,9 +2,7 @@ from setuptools import setup -import jdcal - -version = jdcal.__version__ +from __init__ import version long_description = open("README.rst").read() @@ -35,5 +33,6 @@ 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', ], - py_modules=["jdcal"] + py_modules=["jdcal"], + install_requires=["typing;python_version<'3.5'"] ) From 7d586ade3801edd96fd172ff4afb4b47045b8b6c Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 21:59:25 +0000 Subject: [PATCH 06/11] tidy up tox.ini --- tox.ini | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tox.ini b/tox.ini index 71240e2..edca961 100644 --- a/tox.ini +++ b/tox.ini @@ -6,22 +6,11 @@ envlist = py{27,34,35,36,37,38,py,py3}, codestyle, docstyle, mypy deps = pytest commands = pytest -[testenv:py{27,34}] -deps = - typing - pytest - astropy - [testenv:py{27,34,35,36,37,38}] deps = pytest astropy -[testenv:pypy] -deps = - typing - pytest - [testenv:codestyle] deps = pycodestyle commands = pycodestyle --ignore=E722,E501 jdcal.py test_jdcal.py From 5728cd3ab180ba24366e1a34cab4d7ce7d4f6de7 Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 22:01:25 +0000 Subject: [PATCH 07/11] revert change to travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 34cb2a4..4c34343 100644 --- a/.travis.yml +++ b/.travis.yml @@ -63,6 +63,6 @@ before_install: install: - pip install tox - - if [[ "$TRAVIS_CPU_ARCH" == "ppc64le" ]]; then pip install pytest; pip install astropy; pip install typing; fi + - if [[ "$TRAVIS_CPU_ARCH" == "ppc64le" ]]; then pip install pytest; pip install astropy; fi script: tox From 7fbc401e9eff60beb0c0d6373d2e0f5754061a54 Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 22:51:27 +0000 Subject: [PATCH 08/11] move version to separate module 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. --- MANIFEST.in | 2 +- __init__.py => __version__.py | 2 +- jdcal.py | 2 ++ setup.py | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) rename __init__.py => __version__.py (68%) diff --git a/MANIFEST.in b/MANIFEST.in index 8d46c00..fa34c97 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ include README.rst include LICENSE.txt include *jdcal.py -include __init__.py +include __version__.py diff --git a/__init__.py b/__version__.py similarity index 68% rename from __init__.py rename to __version__.py index a1bab83..afbd696 100644 --- a/__init__.py +++ b/__version__.py @@ -1,4 +1,4 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -version = "1.4.1" +__version__ = "1.4.1" diff --git a/jdcal.py b/jdcal.py index b0e7d47..a25099d 100644 --- a/jdcal.py +++ b/jdcal.py @@ -43,6 +43,8 @@ import math from typing import Tuple +from __version__ import __version__ + MJD_0 = 2400000.5 MJD_JD2000 = 51544.5 diff --git a/setup.py b/setup.py index 2c5511c..f5b352e 100644 --- a/setup.py +++ b/setup.py @@ -2,13 +2,13 @@ from setuptools import setup -from __init__ import version +from __version__ import __version__ long_description = open("README.rst").read() setup( name="jdcal", - version=version, + version=__version__, description="Julian dates from proleptic Gregorian and Julian calendars.", long_description=long_description, license='BSD', From d562dd3f2872d378d7a9e0e13024f365f860eaf9 Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sat, 5 Dec 2020 23:46:41 +0000 Subject: [PATCH 09/11] remove before_install section of travis.yml This was causing build failures on ppc64le, see: https://travis-ci.community/t/permission-issue-while-building-wheels- for-various-python-packages/7822/14 --- .travis.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4c34343..7f77f69 100644 --- a/.travis.yml +++ b/.travis.yml @@ -55,12 +55,6 @@ matrix: - arch: ppc64le python: pypy3 -before_install: - - | - if [[ "$TRAVIS_CPU_ARCH" == "ppc64le" ]]; then - sudo chown -Rv $USER:$GROUP ~/.cache/pip/wheels - fi - install: - pip install tox - if [[ "$TRAVIS_CPU_ARCH" == "ppc64le" ]]; then pip install pytest; pip install astropy; fi From 1009b9c78732653f56b25e9ef1db354f51507378 Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sun, 6 Dec 2020 00:19:39 +0000 Subject: [PATCH 10/11] add mypy to travis build process --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 7f77f69..9e48e67 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,8 @@ matrix: env: TOXENV=codestyle - python: 3.8 env: TOXENV=docstyle + - python: 3.8 + env: TOXENV=mypy - python: 3.8 env: TOXENV=py38 - python: 3.7 @@ -34,6 +36,9 @@ matrix: - python: 3.8 arch: ppc64le env: TOXENV=docstyle + - python: 3.8 + arch: ppc64le + env: TOXENV=mypy - python: 3.8 arch: ppc64le env: TOXENV=py38 From 81c0fa7e435272e9e7442ba90a49fcd7839cfb86 Mon Sep 17 00:00:00 2001 From: Cameron Pinnegar Date: Sun, 6 Dec 2020 13:16:46 +0000 Subject: [PATCH 11/11] yak shave: include py.typed and move to src layout 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 --- MANIFEST.in | 4 ++-- setup.py | 13 +++++++++---- src/jdcal/__init__.py | 14 ++++++++++++++ jdcal.py => src/jdcal/jdcal.py | 1 - src/jdcal/py.typed | 0 __version__.py => src/jdcal/version.py | 0 test_jdcal.py => tests/test_jdcal.py | 0 tox.ini | 6 +++--- 8 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 src/jdcal/__init__.py rename jdcal.py => src/jdcal/jdcal.py (99%) create mode 100644 src/jdcal/py.typed rename __version__.py => src/jdcal/version.py (100%) rename test_jdcal.py => tests/test_jdcal.py (100%) diff --git a/MANIFEST.in b/MANIFEST.in index fa34c97..94204e2 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ include README.rst include LICENSE.txt -include *jdcal.py -include __version__.py +include src/jdcal/*.py +include src/jdcal/py.typed diff --git a/setup.py b/setup.py index f5b352e..2520dd5 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,13 @@ #!/usr/bin/env python -from setuptools import setup +from setuptools import setup, find_packages -from __version__ import __version__ long_description = open("README.rst").read() +with open("src/jdcal/version.py") as f: + exec(f.read()) + setup( name="jdcal", version=__version__, @@ -33,6 +35,9 @@ 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', ], - py_modules=["jdcal"], - install_requires=["typing;python_version<'3.5'"] + # py_modules=["jdcal"], + packages=find_packages("src"), + package_dir={"": "src"}, + install_requires=["typing;python_version<'3.5'"], + include_package_data=True, ) diff --git a/src/jdcal/__init__.py b/src/jdcal/__init__.py new file mode 100644 index 0000000..2667a23 --- /dev/null +++ b/src/jdcal/__init__.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# see https://mypy.readthedocs.io/en/latest/config_file.html#confval-implicit_reexport +from .version import __version__ as __version__ +from .jdcal import ( + MJD_0 as MJD_0, + MJD_JD2000 as MJD_JD2000, + is_leap as is_leap, + gcal2jd as gcal2jd, + jd2gcal as jd2gcal, + jcal2jd as jcal2jd, + jd2jcal as jd2jcal, +) diff --git a/jdcal.py b/src/jdcal/jdcal.py similarity index 99% rename from jdcal.py rename to src/jdcal/jdcal.py index a25099d..4d3ac37 100644 --- a/jdcal.py +++ b/src/jdcal/jdcal.py @@ -43,7 +43,6 @@ import math from typing import Tuple -from __version__ import __version__ MJD_0 = 2400000.5 MJD_JD2000 = 51544.5 diff --git a/src/jdcal/py.typed b/src/jdcal/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/__version__.py b/src/jdcal/version.py similarity index 100% rename from __version__.py rename to src/jdcal/version.py diff --git a/test_jdcal.py b/tests/test_jdcal.py similarity index 100% rename from test_jdcal.py rename to tests/test_jdcal.py diff --git a/tox.ini b/tox.ini index edca961..ab3ab32 100644 --- a/tox.ini +++ b/tox.ini @@ -13,12 +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 [testenv:docstyle] deps = pydocstyle -commands = pydocstyle jdcal.py test_jdcal.py +commands = pydocstyle src/jdcal/jdcal.py [testenv:mypy] deps = mypy -commands = mypy --strict jdcal.py +commands = mypy --strict src/jdcal/jdcal.py