From 36946638a38b33dad19ce09456ca78531bade285 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 09:12:57 -0700 Subject: [PATCH 01/14] Collapse some stages --- .github/workflows/ci.yml | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2e4a62b..a446d29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,9 +23,6 @@ jobs: with: submodules: false coverage: codecov - libraries: | - apt: - - libopenjp2-7 envs: | - linux: py310 - linux: codestyle @@ -38,23 +35,11 @@ jobs: libraries: | apt: - libopenjp2-7 - brew: - - openjpeg envs: | - macos: py38 - windows: py39 - online: - needs: [test] - uses: OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@main - with: - submodules: false - coverage: codecov - libraries: | - apt: - - libopenjp2-7 - envs: | - - linux: py39-online - linux: build_docs + publish: # Build wheels when pushing to any branch except main # publish.yml will only publish if tagged ^v.* From 4b9b97eed76832eb1c2d39916cb49968caeeaba7 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 09:19:54 -0700 Subject: [PATCH 02/14] Update ci.yml --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a446d29..fa82cf4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,6 +35,7 @@ jobs: libraries: | apt: - libopenjp2-7 + - graphviz envs: | - macos: py38 - windows: py39 From e5cfd401de1bedf874af6016d69281d10124c52c Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 10:26:36 -0700 Subject: [PATCH 03/14] test mypy settings --- .github/workflows/ci.yml | 1 - .pre-commit-config.yaml | 50 ++++++++++++++++++++++------------------ pyproject.toml | 10 ++++++++ 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa82cf4..ea0e1c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,6 @@ jobs: - macos: py38 - windows: py39 - linux: build_docs - publish: # Build wheels when pushing to any branch except main # publish.yml will only publish if tagged ^v.* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 42b79ab..d163a52 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,26 +10,30 @@ repos: - id: autoflake args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable'] exclude: ".*(.fits|.fts|.fit|.txt|tca.*|extern.*|.rst|.md|__init__.py|docs/conf.py)$" - - repo: https://github.com/psf/black - rev: 22.3.0 - hooks: - - id: black - exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" - - repo: https://github.com/timothycrosley/isort - rev: 5.10.1 - hooks: - - id: isort - exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.2.0 - hooks: - - id: check-ast - - id: check-case-conflict - - id: trailing-whitespace - exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" - - id: mixed-line-ending - exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" - - id: end-of-file-fixer - exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" - - id: check-yaml - - id: debug-statements + - repo: https://github.com/psf/black + rev: 22.6.0 + hooks: + - id: black + exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" + - repo: https://github.com/timothycrosley/isort + rev: 5.10.1 + hooks: + - id: isort + exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: check-ast + - id: check-case-conflict + - id: trailing-whitespace + exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" + - id: mixed-line-ending + exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" + - id: end-of-file-fixer + exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" + - id: check-yaml + - id: debug-statements + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v0.961' + hooks: + - id: mypy diff --git a/pyproject.toml b/pyproject.toml index 56653b3..2241918 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,3 +26,13 @@ exclude = ''' )/ ) ''' + +[tool.mypy] +python_version = "3.9" +warn_return_any = true +warn_unused_configs = true +ignore_missing_imports = true +exclude = [ + 'setup.py', + 'docs/conf.py', +] From 74dda7ea97f321e0fa473fbf4380adb0c2062c5e Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 10:30:33 -0700 Subject: [PATCH 04/14] rtd libraries --- .readthedocs.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 0aeaeee..042ed4c 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -3,6 +3,9 @@ build: os: ubuntu-20.04 tools: python: "3.9" + apt_packages: + - libopenjp2-7 + - graphviz sphinx: builder: html From fb6a6b2e8abf1494842669cea244081494fc587f Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 10:31:15 -0700 Subject: [PATCH 05/14] skip diagrams by default --- docs/conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/conf.py b/docs/conf.py index 9dbc17b..a8d9e49 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,6 +25,7 @@ "sphinx_automodapi.automodapi", "sphinx_automodapi.smart_resolver", ] +automodapi_inheritance_diagram = False exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] source_suffix = ".rst" master_doc = "index" From 7a85e715c8d8ad91c775b5c658508a9acb174121 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 10:33:15 -0700 Subject: [PATCH 06/14] copied the api docs and tweaks to namespace --- docs/api.rst | 8 ++++++-- hvpy/io.py | 2 ++ hvpy/parameters.py | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 hvpy/parameters.py diff --git a/docs/api.rst b/docs/api.rst index 1b63e0a..8eeec3c 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -3,5 +3,9 @@ API Reference ************* .. automodapi:: hvpy - :inherited-members: - :include-all-objects: + +.. automodapi:: hvpy.core + +.. automodapi:: hvpy.io + +.. automodapi:: hvpy.parameters diff --git a/hvpy/io.py b/hvpy/io.py index 9362e4a..082f87f 100644 --- a/hvpy/io.py +++ b/hvpy/io.py @@ -2,6 +2,8 @@ from pydantic import BaseModel +__all__ = ["HvpyParameters", "OutputType"] + BASE_URL = "https://api.helioviewer.org/v2/" diff --git a/hvpy/parameters.py b/hvpy/parameters.py new file mode 100644 index 0000000..6c9bbdf --- /dev/null +++ b/hvpy/parameters.py @@ -0,0 +1,3 @@ +from hvpy.api_groups.jpeg2000.get_jp2_image import getJP2ImageInputParameters + +__all__ = ["getJP2ImageInputParameters"] From 6094fa5bd62733bdbb4e008cd096d29385d31e8d Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 10:54:12 -0700 Subject: [PATCH 07/14] some more typing and doc changes --- .pre-commit-config.yaml | 1 + hvpy/api_groups/jpeg2000/get_jp2_image.py | 13 ++++++----- hvpy/core.py | 21 +++++++++-------- hvpy/io.py | 28 ++++++++++++----------- pyproject.toml | 2 +- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d163a52..0a8342e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -37,3 +37,4 @@ repos: rev: 'v0.961' hooks: - id: mypy + additional_dependencies: [types-requests==2.28.0] diff --git a/hvpy/api_groups/jpeg2000/get_jp2_image.py b/hvpy/api_groups/jpeg2000/get_jp2_image.py index 23fe191..0cc2751 100644 --- a/hvpy/api_groups/jpeg2000/get_jp2_image.py +++ b/hvpy/api_groups/jpeg2000/get_jp2_image.py @@ -1,3 +1,4 @@ +from typing import Any, Literal from datetime import datetime from pydantic import Field, validator @@ -13,13 +14,13 @@ class getJP2ImageInputParameters(HvpyParameters): Attributes ---------- - date : datetime + date : `datetime.datetime` Desired date/time of the JP2 image. - sourceId : int + sourceId : `int` Unique image datasource identifier. - jpip : bool, optional + jpip : `bool`, optional Returns a JPIP URI instead of the binary data of the image if set to `True`, defaults to `False`. - json : bool, optional + json : `bool`, optional Returns the JSON if set to `True`, defaults to `False`. """ @@ -29,13 +30,13 @@ class getJP2ImageInputParameters(HvpyParameters): Json: bool = Field(False, alias="json") @validator("date") - def convert_date_to_isoformat(cls, v): + def convert_date_to_isoformat(cls, v) -> Any: """ Converts the date from a datetime object to a string in the ISO format. """ return v.isoformat() + "Z" - def get_output_type(self): + def get_output_type(self) -> Literal[OutputType.Raw, OutputType.String, OutputType.Json]: """ Returns the output type of the API call. """ diff --git a/hvpy/core.py b/hvpy/core.py index 6e9a840..6e04d0c 100644 --- a/hvpy/core.py +++ b/hvpy/core.py @@ -1,47 +1,50 @@ +from typing import Any, Union + import requests from hvpy.io import HvpyParameters, OutputType -def parse_response(response: requests.Response, output_parameters: OutputType): +def parse_response(response: requests.Response, output_parameters: OutputType) -> Union[bytes, str, Any]: """ Parses the response from the API call based on the output type. Parameters ---------- - response : requests.Response + response : `requests.Response` The response from the API call. - output_parameters : OutputType + output_parameters : `hvpy.io.OutputType` The output type. Returns ------- - binary | str | json + `bytes` | `str` | `dict` The parsed response. """ - if output_parameters == OutputType.Raw: return response.content elif output_parameters == OutputType.String: return response.content.decode("utf-8") elif output_parameters == OutputType.Json: return response.json() + else: + raise ValueError("Unknown output type") -def execute_api_call(input_parameters: HvpyParameters): +def execute_api_call(input_parameters: HvpyParameters) -> Union[bytes, str, dict]: """ Executes the API call and returns a parsed response. Parameters ---------- - input_parameters : HvpyParameters + input_parameters : `HvpyParameters` The input parameters. Returns ------- - Parsed response from the API. + `bytes` | `str` | `dict` + Parsed response from the API. """ - response = requests.get(input_parameters.url, params=input_parameters.dict()) response.raise_for_status() return parse_response(response, input_parameters.get_output_type()) diff --git a/hvpy/io.py b/hvpy/io.py index 082f87f..8713a1e 100644 --- a/hvpy/io.py +++ b/hvpy/io.py @@ -1,33 +1,35 @@ from enum import Enum, auto +from typing import Literal -from pydantic import BaseModel +from pydantic.main import BaseModel __all__ = ["HvpyParameters", "OutputType"] BASE_URL = "https://api.helioviewer.org/v2/" +class OutputType(Enum): + Raw = auto() + String = auto() + Json = auto() + + class HvpyParameters(BaseModel): + """ + Base model for all Helioviewer API parameters. + """ + def dict(self): - """ - Pydantic doesn't allow using lowercase 'json' as a field, so we - override it. - """ + # pydantic doesn't allow using lowercase 'json' as a field d = super().dict() if "Json" in d: d["json"] = d["Json"] del d["Json"] return d - def get_output_type(self): + def get_output_type(self) -> Literal[OutputType.Raw, OutputType.String, OutputType.Json]: return OutputType.Raw @property - def url(self): + def url(self) -> str: return BASE_URL + self.__class__.__name__[:-15] + "/" - - -class OutputType(Enum): - Raw = auto() - String = auto() - Json = auto() diff --git a/pyproject.toml b/pyproject.toml index 2241918..fdb5b22 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,7 @@ exclude = ''' ''' [tool.mypy] -python_version = "3.9" +python_version = "3.10" warn_return_any = true warn_unused_configs = true ignore_missing_imports = true From 6c39b8774e166ce0ba87c011122b5c4746ec4e37 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 11:06:21 -0700 Subject: [PATCH 08/14] test retun ttpe --- hvpy/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hvpy/core.py b/hvpy/core.py index 6e04d0c..ac7c945 100644 --- a/hvpy/core.py +++ b/hvpy/core.py @@ -18,7 +18,7 @@ def parse_response(response: requests.Response, output_parameters: OutputType) - Returns ------- - `bytes` | `str` | `dict` + {`bytes` | `str` | `dict`} The parsed response. """ if output_parameters == OutputType.Raw: @@ -42,7 +42,7 @@ def execute_api_call(input_parameters: HvpyParameters) -> Union[bytes, str, dict Returns ------- - `bytes` | `str` | `dict` + {`bytes` | `str` | `dict`} Parsed response from the API. """ response = requests.get(input_parameters.url, params=input_parameters.dict()) From c64e4d630e6e98a3c6cb5179768b083406910981 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 11:10:43 -0700 Subject: [PATCH 09/14] nitpick --- docs/conf.py | 11 +++++-- docs/nitpick-exceptions | 3 ++ hvpy/api_groups/jpeg2000/get_jp2_image.py | 19 +++++------ hvpy/core.py | 24 +++++++------- hvpy/io.py | 40 ++++++++++++++++++----- hvpy/tests/test_io.py | 2 +- pyproject.toml | 2 -- setup.cfg | 2 +- 8 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 docs/nitpick-exceptions diff --git a/docs/conf.py b/docs/conf.py index a8d9e49..939661a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -14,24 +14,31 @@ # -- General configuration --------------------------------------------------- extensions = [ "sphinx.ext.autodoc", + "sphinx.ext.napoleon", "sphinx.ext.intersphinx", "sphinx.ext.todo", "sphinx.ext.coverage", "sphinx.ext.inheritance_diagram", "sphinx.ext.viewcode", - "sphinx.ext.napoleon", "sphinx.ext.doctest", "sphinx.ext.mathjax", "sphinx_automodapi.automodapi", "sphinx_automodapi.smart_resolver", ] -automodapi_inheritance_diagram = False exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] source_suffix = ".rst" master_doc = "index" # Enable nitpicky mode, which forces links to be non-broken nitpicky = True +# This is not used. See docs/nitpick-exceptions file for the actual listing. +nitpick_ignore = [] +for line in open("nitpick-exceptions"): + if line.strip() == "" or line.startswith("#"): + continue + dtype, target = line.split(None, 1) + target = target.strip() + nitpick_ignore.append((dtype, target)) # -- Options for intersphinx extension ----------------------------------------- # Example configuration for intersphinx: refer to the Python standard library. diff --git a/docs/nitpick-exceptions b/docs/nitpick-exceptions new file mode 100644 index 0000000..69283a6 --- /dev/null +++ b/docs/nitpick-exceptions @@ -0,0 +1,3 @@ +py:class pydantic.main.BaseModel +py:class pydantic.utils.Representation +py:class rtype diff --git a/hvpy/api_groups/jpeg2000/get_jp2_image.py b/hvpy/api_groups/jpeg2000/get_jp2_image.py index 0cc2751..5c04c7b 100644 --- a/hvpy/api_groups/jpeg2000/get_jp2_image.py +++ b/hvpy/api_groups/jpeg2000/get_jp2_image.py @@ -1,4 +1,3 @@ -from typing import Any, Literal from datetime import datetime from pydantic import Field, validator @@ -14,13 +13,13 @@ class getJP2ImageInputParameters(HvpyParameters): Attributes ---------- - date : `datetime.datetime` + date : datetime.datetime Desired date/time of the JP2 image. - sourceId : `int` + sourceId : int Unique image datasource identifier. - jpip : `bool`, optional + jpip : bool, optional Returns a JPIP URI instead of the binary data of the image if set to `True`, defaults to `False`. - json : `bool`, optional + json : bool, optional Returns the JSON if set to `True`, defaults to `False`. """ @@ -30,19 +29,19 @@ class getJP2ImageInputParameters(HvpyParameters): Json: bool = Field(False, alias="json") @validator("date") - def convert_date_to_isoformat(cls, v) -> Any: + def convert_date_to_isoformat(cls, v) -> str: """ Converts the date from a datetime object to a string in the ISO format. """ return v.isoformat() + "Z" - def get_output_type(self) -> Literal[OutputType.Raw, OutputType.String, OutputType.Json]: + def get_output_type(self) -> int: """ Returns the output type of the API call. """ if self.Json and self.jpip: - return OutputType.Json + return OutputType.JSON elif not self.Json and self.jpip: - return OutputType.String + return OutputType.STRING else: - return OutputType.Raw + return OutputType.RAW diff --git a/hvpy/core.py b/hvpy/core.py index ac7c945..e127568 100644 --- a/hvpy/core.py +++ b/hvpy/core.py @@ -1,48 +1,48 @@ -from typing import Any, Union +from typing import Any, Dict, Union import requests from hvpy.io import HvpyParameters, OutputType -def parse_response(response: requests.Response, output_parameters: OutputType) -> Union[bytes, str, Any]: +def parse_response(response: requests.Response, output_type: int) -> Union[bytes, str, Dict[str, Any]]: """ Parses the response from the API call based on the output type. Parameters ---------- - response : `requests.Response` + response : requests.Response The response from the API call. - output_parameters : `hvpy.io.OutputType` + output_type : int The output type. Returns ------- - {`bytes` | `str` | `dict`} + Union[bytes, str, Dict[str, Any]] The parsed response. """ - if output_parameters == OutputType.Raw: + if output_type == OutputType.RAW: return response.content - elif output_parameters == OutputType.String: + elif output_type == OutputType.STRING: return response.content.decode("utf-8") - elif output_parameters == OutputType.Json: + elif output_type == OutputType.JSON: return response.json() else: - raise ValueError("Unknown output type") + raise ValueError(f"Unknown output type: {output_type}") -def execute_api_call(input_parameters: HvpyParameters) -> Union[bytes, str, dict]: +def execute_api_call(input_parameters: HvpyParameters) -> Union[bytes, str, Dict[str, Any]]: """ Executes the API call and returns a parsed response. Parameters ---------- - input_parameters : `HvpyParameters` + input_parameters : hvpy.io.HvpyParameters The input parameters. Returns ------- - {`bytes` | `str` | `dict`} + Union[bytes, str, Dict[str, Any]] Parsed response from the API. """ response = requests.get(input_parameters.url, params=input_parameters.dict()) diff --git a/hvpy/io.py b/hvpy/io.py index 8713a1e..8676556 100644 --- a/hvpy/io.py +++ b/hvpy/io.py @@ -1,5 +1,5 @@ -from enum import Enum, auto -from typing import Literal +from enum import IntEnum, auto +from typing import Any, Dict from pydantic.main import BaseModel @@ -8,10 +8,19 @@ BASE_URL = "https://api.helioviewer.org/v2/" -class OutputType(Enum): - Raw = auto() - String = auto() - Json = auto() +class OutputType(IntEnum): + RAW = auto() + """ + Defines the RAW output type. + """ + STRING = auto() + """ + Defines the STRING output type. + """ + JSON = auto() + """ + Defines the JSON output type. + """ class HvpyParameters(BaseModel): @@ -19,7 +28,7 @@ class HvpyParameters(BaseModel): Base model for all Helioviewer API parameters. """ - def dict(self): + def dict(self) -> Dict[str, Any]: # type: ignore # pydantic doesn't allow using lowercase 'json' as a field d = super().dict() if "Json" in d: @@ -27,9 +36,22 @@ def dict(self): del d["Json"] return d - def get_output_type(self) -> Literal[OutputType.Raw, OutputType.String, OutputType.Json]: - return OutputType.Raw + def get_output_type(self) -> int: + """ + Works out the return type of the API call. + + This by default is RAW (1), subclasses should redefine this. + + Returns + ------- + int + Output type based on the model. + """ + return OutputType.RAW @property def url(self) -> str: + """ + Final API endpoint URL. + """ return BASE_URL + self.__class__.__name__[:-15] + "/" diff --git a/hvpy/tests/test_io.py b/hvpy/tests/test_io.py index 2c295e6..e32f32d 100644 --- a/hvpy/tests/test_io.py +++ b/hvpy/tests/test_io.py @@ -3,7 +3,7 @@ def test_default_get_output_type_is_raw(): params = HvpyParameters() - assert params.get_output_type() == OutputType.Raw + assert params.get_output_type() == OutputType.RAW == 1 def test_url_property(): diff --git a/pyproject.toml b/pyproject.toml index fdb5b22..02d9d97 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,8 +29,6 @@ exclude = ''' [tool.mypy] python_version = "3.10" -warn_return_any = true -warn_unused_configs = true ignore_missing_imports = true exclude = [ 'setup.py', diff --git a/setup.cfg b/setup.cfg index 6fe75b7..3c615f7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ name = hvpy author = The Helioviewer Project author_email = contact@helioviewer.org license = BSD 2-Clause -license_file = licenses/LICENSE.rst +license_files = licenses/LICENSE.rst url = https://helioviewer.org/ description = Helioviewer Python API Wrapper long_description = file: README.rst From 3c83470798ec33839c0390a6c14321cad940e8e1 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 12:10:48 -0700 Subject: [PATCH 10/14] api doc working --- docs/api.rst | 1 + docs/nitpick-exceptions | 1 + hvpy/api_groups/jpeg2000/get_jp2_image.py | 8 +++++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 8eeec3c..1cb72c9 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -9,3 +9,4 @@ API Reference .. automodapi:: hvpy.io .. automodapi:: hvpy.parameters + :allowed-package-names: hvpy.api_groups diff --git a/docs/nitpick-exceptions b/docs/nitpick-exceptions index 69283a6..25cf937 100644 --- a/docs/nitpick-exceptions +++ b/docs/nitpick-exceptions @@ -1,3 +1,4 @@ +py:class optional py:class pydantic.main.BaseModel py:class pydantic.utils.Representation py:class rtype diff --git a/hvpy/api_groups/jpeg2000/get_jp2_image.py b/hvpy/api_groups/jpeg2000/get_jp2_image.py index 5c04c7b..b76a4d2 100644 --- a/hvpy/api_groups/jpeg2000/get_jp2_image.py +++ b/hvpy/api_groups/jpeg2000/get_jp2_image.py @@ -7,9 +7,7 @@ class getJP2ImageInputParameters(HvpyParameters): """ - Handles the input parameters of the `getJP2Image API. - - `__. + Handles the input parameters of the getJP2Image API. Attributes ---------- @@ -21,6 +19,10 @@ class getJP2ImageInputParameters(HvpyParameters): Returns a JPIP URI instead of the binary data of the image if set to `True`, defaults to `False`. json : bool, optional Returns the JSON if set to `True`, defaults to `False`. + + References + ---------- + * ``__ """ date: datetime From e7c814b3ff6297a15d6770cabea3ab66029121ef Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 12:43:49 -0700 Subject: [PATCH 11/14] fix type issue for OutputType --- hvpy/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hvpy/core.py b/hvpy/core.py index e127568..e4f26c6 100644 --- a/hvpy/core.py +++ b/hvpy/core.py @@ -13,7 +13,7 @@ def parse_response(response: requests.Response, output_type: int) -> Union[bytes ---------- response : requests.Response The response from the API call. - output_type : int + output_type : OutputType The output type. Returns From 3cca5b2ac0bb8d8e44f63735c6a984b51a40da2b Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 12:56:28 -0700 Subject: [PATCH 12/14] correct type again --- docs/nitpick-exceptions | 1 + hvpy/api_groups/jpeg2000/get_jp2_image.py | 3 ++- hvpy/core.py | 8 +++++--- hvpy/io.py | 8 ++++---- hvpy/tests/test_io.py | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/docs/nitpick-exceptions b/docs/nitpick-exceptions index 25cf937..b39e5e2 100644 --- a/docs/nitpick-exceptions +++ b/docs/nitpick-exceptions @@ -2,3 +2,4 @@ py:class optional py:class pydantic.main.BaseModel py:class pydantic.utils.Representation py:class rtype +py:obj typing.Literal[, , ] diff --git a/hvpy/api_groups/jpeg2000/get_jp2_image.py b/hvpy/api_groups/jpeg2000/get_jp2_image.py index b76a4d2..bdc2fe2 100644 --- a/hvpy/api_groups/jpeg2000/get_jp2_image.py +++ b/hvpy/api_groups/jpeg2000/get_jp2_image.py @@ -1,3 +1,4 @@ +from typing import Literal from datetime import datetime from pydantic import Field, validator @@ -37,7 +38,7 @@ def convert_date_to_isoformat(cls, v) -> str: """ return v.isoformat() + "Z" - def get_output_type(self) -> int: + def get_output_type(self) -> Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING]: """ Returns the output type of the API call. """ diff --git a/hvpy/core.py b/hvpy/core.py index e4f26c6..013c621 100644 --- a/hvpy/core.py +++ b/hvpy/core.py @@ -1,11 +1,13 @@ -from typing import Any, Dict, Union +from typing import Any, Dict, Union, Literal import requests from hvpy.io import HvpyParameters, OutputType -def parse_response(response: requests.Response, output_type: int) -> Union[bytes, str, Dict[str, Any]]: +def parse_response( + response: requests.Response, output_type: Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING] +) -> Union[bytes, str, Dict[str, Any]]: """ Parses the response from the API call based on the output type. @@ -13,7 +15,7 @@ def parse_response(response: requests.Response, output_type: int) -> Union[bytes ---------- response : requests.Response The response from the API call. - output_type : OutputType + output_type : Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING] The output type. Returns diff --git a/hvpy/io.py b/hvpy/io.py index 8676556..e4590db 100644 --- a/hvpy/io.py +++ b/hvpy/io.py @@ -1,5 +1,5 @@ -from enum import IntEnum, auto -from typing import Any, Dict +from enum import Enum, auto +from typing import Any, Dict, Literal from pydantic.main import BaseModel @@ -8,7 +8,7 @@ BASE_URL = "https://api.helioviewer.org/v2/" -class OutputType(IntEnum): +class OutputType(Enum): RAW = auto() """ Defines the RAW output type. @@ -36,7 +36,7 @@ def dict(self) -> Dict[str, Any]: # type: ignore del d["Json"] return d - def get_output_type(self) -> int: + def get_output_type(self) -> Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING]: """ Works out the return type of the API call. diff --git a/hvpy/tests/test_io.py b/hvpy/tests/test_io.py index e32f32d..deb0ec5 100644 --- a/hvpy/tests/test_io.py +++ b/hvpy/tests/test_io.py @@ -3,7 +3,7 @@ def test_default_get_output_type_is_raw(): params = HvpyParameters() - assert params.get_output_type() == OutputType.RAW == 1 + assert params.get_output_type() == OutputType.RAW def test_url_property(): From 17c8e02dca663282c2b627cc6fcf0c568d9c1ca4 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 13:09:05 -0700 Subject: [PATCH 13/14] added test --- hvpy/core.py | 2 ++ hvpy/tests/test_core.py | 8 ++++++++ 2 files changed, 10 insertions(+) create mode 100644 hvpy/tests/test_core.py diff --git a/hvpy/core.py b/hvpy/core.py index 013c621..6f3611c 100644 --- a/hvpy/core.py +++ b/hvpy/core.py @@ -4,6 +4,8 @@ from hvpy.io import HvpyParameters, OutputType +__all__ = ["execute_api_call", "parse_response"] + def parse_response( response: requests.Response, output_type: Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING] diff --git a/hvpy/tests/test_core.py b/hvpy/tests/test_core.py new file mode 100644 index 0000000..79cf6e4 --- /dev/null +++ b/hvpy/tests/test_core.py @@ -0,0 +1,8 @@ +import pytest + +from hvpy.core import parse_response + + +def test_wrong_input_type(): + with pytest.raises(ValueError, match="Unknown output type: 42"): + parse_response(None, 42) From 3a285fd1a99bc89251cee6d0041d3f524c1548c5 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Thu, 30 Jun 2022 13:15:17 -0700 Subject: [PATCH 14/14] change type --- hvpy/api_groups/jpeg2000/get_jp2_image.py | 3 +-- hvpy/core.py | 8 +++----- hvpy/io.py | 8 ++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/hvpy/api_groups/jpeg2000/get_jp2_image.py b/hvpy/api_groups/jpeg2000/get_jp2_image.py index bdc2fe2..da7e210 100644 --- a/hvpy/api_groups/jpeg2000/get_jp2_image.py +++ b/hvpy/api_groups/jpeg2000/get_jp2_image.py @@ -1,4 +1,3 @@ -from typing import Literal from datetime import datetime from pydantic import Field, validator @@ -38,7 +37,7 @@ def convert_date_to_isoformat(cls, v) -> str: """ return v.isoformat() + "Z" - def get_output_type(self) -> Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING]: + def get_output_type(self) -> OutputType: """ Returns the output type of the API call. """ diff --git a/hvpy/core.py b/hvpy/core.py index 6f3611c..770ff41 100644 --- a/hvpy/core.py +++ b/hvpy/core.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Union, Literal +from typing import Any, Dict, Union import requests @@ -7,9 +7,7 @@ __all__ = ["execute_api_call", "parse_response"] -def parse_response( - response: requests.Response, output_type: Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING] -) -> Union[bytes, str, Dict[str, Any]]: +def parse_response(response: requests.Response, output_type: OutputType) -> Union[bytes, str, Dict[str, Any]]: """ Parses the response from the API call based on the output type. @@ -17,7 +15,7 @@ def parse_response( ---------- response : requests.Response The response from the API call. - output_type : Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING] + output_type : hvpy.io.OutputType The output type. Returns diff --git a/hvpy/io.py b/hvpy/io.py index e4590db..e87af04 100644 --- a/hvpy/io.py +++ b/hvpy/io.py @@ -1,5 +1,5 @@ from enum import Enum, auto -from typing import Any, Dict, Literal +from typing import Any, Dict from pydantic.main import BaseModel @@ -36,15 +36,15 @@ def dict(self) -> Dict[str, Any]: # type: ignore del d["Json"] return d - def get_output_type(self) -> Literal[OutputType.RAW, OutputType.JSON, OutputType.STRING]: + def get_output_type(self) -> OutputType: """ Works out the return type of the API call. - This by default is RAW (1), subclasses should redefine this. + This by default is RAW, subclasses should redefine this. Returns ------- - int + hvpy.io.OutputType Output type based on the model. """ return OutputType.RAW