From 6d49dfc9b0f43118b43408290affa6080a28bc9b Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Thu, 17 Nov 2022 10:19:44 +0100 Subject: [PATCH 1/3] Use the proper version of SINTEF/ci-cd --- .github/workflows/cd_release.yml | 2 +- .github/workflows/ci_automerge_dependency_prs.yml | 2 +- .github/workflows/ci_cd_updated_main.yml | 2 +- .github/workflows/ci_check_dependencies.yml | 2 +- .github/workflows/ci_tests.yml | 2 +- .github/workflows/ci_update_dependencies.yml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cd_release.yml b/.github/workflows/cd_release.yml index 63ba0290..a0f754b5 100644 --- a/.github/workflows/cd_release.yml +++ b/.github/workflows/cd_release.yml @@ -8,7 +8,7 @@ on: jobs: publish-package-and-docs: name: External - uses: SINTEF/ci-cd/.github/workflows/cd_release.yml@v1.3.4 + uses: SINTEF/ci-cd/.github/workflows/cd_release.yml@v1 if: github.repository == 'EMMC-ASBL/tripper' && startsWith(github.ref, 'refs/tags/v') with: # General diff --git a/.github/workflows/ci_automerge_dependency_prs.yml b/.github/workflows/ci_automerge_dependency_prs.yml index 9a92dd89..a0e886b0 100644 --- a/.github/workflows/ci_automerge_dependency_prs.yml +++ b/.github/workflows/ci_automerge_dependency_prs.yml @@ -7,7 +7,7 @@ on: jobs: update-dependencies-branch: name: External - uses: SINTEF/ci-cd/.github/workflows/ci_automerge_prs.yml@v1.3.4 + uses: SINTEF/ci-cd/.github/workflows/ci_automerge_prs.yml@v1 if: github.repository_owner == 'EMMC-ASBL' && ( ( startsWith(github.event.pull_request.head.ref, 'dependabot/') && github.actor == 'dependabot[bot]' ) || ( github.event.pull_request.head.ref == 'ci/update-pyproject' && github.actor == 'TEAM4-0' ) ) secrets: PAT: ${{ secrets.RELEASE_PAT }} diff --git a/.github/workflows/ci_cd_updated_main.yml b/.github/workflows/ci_cd_updated_main.yml index a15fe32f..a0d0eb51 100644 --- a/.github/workflows/ci_cd_updated_main.yml +++ b/.github/workflows/ci_cd_updated_main.yml @@ -7,7 +7,7 @@ on: jobs: update-deps-branch-and-docs: name: External - uses: SINTEF/ci-cd/.github/workflows/ci_cd_updated_default_branch.yml@v1.3.4 + uses: SINTEF/ci-cd/.github/workflows/ci_cd_updated_default_branch.yml@v1 if: github.repository_owner == 'EMMC-ASBL' with: git_username: "TEAM 4.0[bot]" diff --git a/.github/workflows/ci_check_dependencies.yml b/.github/workflows/ci_check_dependencies.yml index 5a50e70e..7e61597a 100644 --- a/.github/workflows/ci_check_dependencies.yml +++ b/.github/workflows/ci_check_dependencies.yml @@ -9,7 +9,7 @@ on: jobs: check-dependencies: name: External - uses: SINTEF/ci-cd/.github/workflows/ci_check_pyproject_dependencies.yml@v1.3.4 + uses: SINTEF/ci-cd/.github/workflows/ci_check_pyproject_dependencies.yml@v1 if: github.repository_owner == 'EMMC-ASBL' with: git_username: "TEAM 4.0[bot]" diff --git a/.github/workflows/ci_tests.yml b/.github/workflows/ci_tests.yml index fc117848..3c2fa942 100644 --- a/.github/workflows/ci_tests.yml +++ b/.github/workflows/ci_tests.yml @@ -10,7 +10,7 @@ on: jobs: basic-tests: name: External - uses: SINTEF/ci-cd/.github/workflows/ci_tests.yml@v1.3.4 + uses: SINTEF/ci-cd/.github/workflows/ci_tests.yml@v1 with: # General settings: install_extras: "[dev]" diff --git a/.github/workflows/ci_update_dependencies.yml b/.github/workflows/ci_update_dependencies.yml index 0a676f8c..6047b001 100644 --- a/.github/workflows/ci_update_dependencies.yml +++ b/.github/workflows/ci_update_dependencies.yml @@ -9,7 +9,7 @@ on: jobs: create-collected-pr: name: External - uses: SINTEF/ci-cd/.github/workflows/ci_update_dependencies.yml@v1.3.4 + uses: SINTEF/ci-cd/.github/workflows/ci_update_dependencies.yml@v1 if: github.repository_owner == 'EMMC-ASBL' with: git_username: "TEAM 4.0[bot]" From 58bc00251006350fbf3b5435c21f37fce36ab7c0 Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Thu, 17 Nov 2022 12:09:12 +0100 Subject: [PATCH 2/3] Add type annotations to doc string Satisfy mypy, ensuring correct typing. --- tripper/literal.py | 28 ++++++++++++++++++---------- tripper/utils.py | 10 +++++----- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/tripper/literal.py b/tripper/literal.py index 2cff5881..04b67810 100644 --- a/tripper/literal.py +++ b/tripper/literal.py @@ -6,20 +6,22 @@ from tripper.namespace import OWL, RDF, RDFS, XSD if TYPE_CHECKING: # pragma: no cover - from typing import Any, Tuple, Union + from typing import Any, Optional, Union class Literal(str): """A literal RDF value. Arguments: - value: The literal value. See the `datatypes` class attribute for - valid supported data types. A localised string is provided as - a string with `lang` set to a language code. - lang: A standard language code, like "en", "no", etc. Implies that - the `value` is a localised string. - datatype: Explicit specification of the type of `value`. Should not - be combined with `lang`. + value (Union[datetime, bytes, bytearray, bool, int, float, str]): The literal + value. See the `datatypes` class attribute for valid supported data types. + A localised string is provided as a string with `lang` set to a language + code. + lang (Optional[str]): A standard language code, like "en", "no", etc. Implies + that the `value` is a localised string. + datatype (Any): Explicit specification of the type of `value`. Should not be + combined with `lang`. + """ lang: "Union[str, None]" @@ -37,7 +39,12 @@ class Literal(str): str: XSD.string, } - def __new__(cls, value, lang=None, datatype=None): + def __new__( + cls, + value: "Union[datetime, bytes, bytearray, bool, int, float, str]", + lang: "Optional[str]" = None, + datatype: "Optional[Any]" = None, + ): string = super().__new__(cls, value) if lang: if datatype: @@ -57,7 +64,8 @@ def __new__(cls, value, lang=None, datatype=None): elif isinstance(value, float): string.datatype = XSD.double elif isinstance(value, (bytes, bytearray)): - string = value.hex() + string = super().__new__(cls, value.hex()) + string.lang = None string.datatype = XSD.hexBinary elif isinstance(value, datetime): string.datatype = XSD.dateTime diff --git a/tripper/utils.py b/tripper/utils.py index 75c7a16f..dfcaae48 100644 --- a/tripper/utils.py +++ b/tripper/utils.py @@ -8,8 +8,7 @@ from tripper.namespace import XSD if TYPE_CHECKING: # pragma: no cover - from collections.abc import Mapping - from typing import Any, Callable, Dict, Generator, Tuple, Union + from typing import Any, Callable, Tuple def infer_iri(obj): @@ -89,9 +88,10 @@ def parse_literal(literal: "Any") -> "Literal": return literal if not isinstance(literal, str): - for type_, datatype in Literal.datatypes.items(): - if isinstance(literal, type_): - return Literal(literal, lang=lang, datatype=datatype) + if isinstance(literal, tuple(Literal.datatypes)): + return Literal( + literal, lang=lang, datatype=Literal.datatypes.get(type(literal)) + ) TypeError(f"unsupported literal type: {type(literal)}") match = re.match(r'^\s*("""(.*)"""|"(.*)")\s*$', literal, flags=re.DOTALL) From a9c13e4dd2b0fa44c7a575247f72f53971699c8f Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Tue, 22 Nov 2022 14:30:57 +0100 Subject: [PATCH 3/3] Add comment as per review comment --- tripper/literal.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tripper/literal.py b/tripper/literal.py index 04b67810..ca3d69b4 100644 --- a/tripper/literal.py +++ b/tripper/literal.py @@ -64,6 +64,8 @@ def __new__( elif isinstance(value, float): string.datatype = XSD.double elif isinstance(value, (bytes, bytearray)): + # Re-initialize the value anew, similarly to what is done in the first + # line of this method. string = super().__new__(cls, value.hex()) string.lang = None string.datatype = XSD.hexBinary