Skip to content

Commit

Permalink
monkey patch pydantic_core.Url for str concatentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sblack-usu committed Mar 4, 2024
1 parent 33c9b38 commit 6bd788b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
16 changes: 16 additions & 0 deletions hsmodels/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pydantic_core import Url


def __add__(self, other):
return str(self) + other


def __radd__(self, other):
return other + str(self)


# monkey patch string concatentation until pydantic implements Url as an extension of str again
# https://github.com/pydantic/pydantic-core/pull/1126

Url.__add__ = __add__
Url.__radd__ = __radd__
2 changes: 1 addition & 1 deletion hsmodels/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from enum import Enum

from pydantic import BaseModel
from pydantic_core import Url
from rdflib import Graph, Literal, URIRef

from hsmodels import Url
from hsmodels.namespaces import DC, HSTERMS, ORE, RDF, RDFS1
from hsmodels.schemas.aggregations import (
FileSetMetadata,
Expand Down
3 changes: 1 addition & 2 deletions hsmodels/schemas/rdf/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pydantic_core import Url

from hsmodels import Url
from hsmodels.schemas.enums import CoverageType, DateType
from hsmodels.schemas.languages_iso import languages

Expand Down
2 changes: 1 addition & 1 deletion hsmodels/schemas/root_validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic_core import Url
from rdflib import URIRef

from hsmodels import Url
from hsmodels.schemas.enums import CoverageType, DateType, ModelProgramFileType, RelationType, UserIdentifierType
from hsmodels.utils import to_coverage_dict

Expand Down
6 changes: 6 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,9 @@ def test_resource_metadata(res_md):
res_md.publisher.name == "Consortium of Universities for the Advancement of Hydrologic Science, Inc. (CUAHSI)"
)
assert str(res_md.publisher.url) == "https://www.cuahsi.org/"


def test_resource_metadata_url_str_concat(res_md):
assert (
"concat test " + res_md.url == "concat test http://www.hydroshare.org/resource/84805fd615a04d63b4eada65644a1e20"
)

0 comments on commit 6bd788b

Please sign in to comment.