Skip to content

Commit

Permalink
Use the proper version of SINTEF/ci-cd (#36)
Browse files Browse the repository at this point in the history
Add type annotations to doc string.
Satisfy mypy, ensuring correct typing.
  • Loading branch information
CasperWA authored Nov 22, 2022
1 parent 95f1d4c commit d94771f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_automerge_dependency_prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
2 changes: 1 addition & 1 deletion .github/workflows/ci_cd_updated_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_check_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_update_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down
30 changes: 20 additions & 10 deletions tripper/literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand All @@ -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:
Expand All @@ -57,7 +64,10 @@ def __new__(cls, value, lang=None, datatype=None):
elif isinstance(value, float):
string.datatype = XSD.double
elif isinstance(value, (bytes, bytearray)):
string = value.hex()
# 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
elif isinstance(value, datetime):
string.datatype = XSD.dateTime
Expand Down
10 changes: 5 additions & 5 deletions tripper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d94771f

Please sign in to comment.