From 3e4fedd0308f36e1e734fdbbf3d1ab2459e18f8c Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Fri, 2 Feb 2024 11:58:21 -0600 Subject: [PATCH 1/8] ~@validator~ => @field_validator --- src/curies/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/curies/api.py b/src/curies/api.py index 24ed1945..4ad95116 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -33,7 +33,7 @@ ) import requests -from pydantic import BaseModel, Field, validator +from pydantic import BaseModel, Field, field_validator from pytrie import StringTrie if TYPE_CHECKING: # pragma: no cover @@ -267,7 +267,7 @@ class Records(BaseModel): "Warning: this is an experimental feature.", ) - @validator("prefix_synonyms") # type:ignore + @field_validator("prefix_synonyms") # type:ignore def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # noqa:N805 """Check that the canonical prefix does not apper in the prefix synonym list.""" prefix = values["prefix"] @@ -275,7 +275,7 @@ def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # no raise ValueError(f"Duplicate of canonical prefix `{prefix}` in prefix synonyms") return v - @validator("uri_prefix_synonyms") # type:ignore + @field_validator("uri_prefix_synonyms") # type:ignore def uri_prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # noqa:N805 """Check that the canonical URI prefix does not apper in the URI prefix synonym list.""" uri_prefix = values["uri_prefix"] From 488e4e0b95834439d10347f36dd08fcdfc9b5906 Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Fri, 2 Feb 2024 13:24:25 -0600 Subject: [PATCH 2/8] values.data['foo'] instead of values['foo] --- src/curies/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/curies/api.py b/src/curies/api.py index 4ad95116..4fcc9d0f 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -270,7 +270,7 @@ class Records(BaseModel): @field_validator("prefix_synonyms") # type:ignore def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # noqa:N805 """Check that the canonical prefix does not apper in the prefix synonym list.""" - prefix = values["prefix"] + prefix = values.data["prefix"] if prefix in v: raise ValueError(f"Duplicate of canonical prefix `{prefix}` in prefix synonyms") return v @@ -278,7 +278,7 @@ def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # no @field_validator("uri_prefix_synonyms") # type:ignore def uri_prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # noqa:N805 """Check that the canonical URI prefix does not apper in the URI prefix synonym list.""" - uri_prefix = values["uri_prefix"] + uri_prefix = values.data["uri_prefix"] if uri_prefix in v: raise ValueError( f"Duplicate of canonical URI prefix `{uri_prefix}` in URI prefix synonyms" From e4e43ba0015b149d235a9d62e47a687e3febdb12 Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Fri, 2 Feb 2024 13:33:43 -0600 Subject: [PATCH 3/8] backwards compatibility --- src/curies/api.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/curies/api.py b/src/curies/api.py index 4fcc9d0f..e5d6e975 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -33,7 +33,15 @@ ) import requests -from pydantic import BaseModel, Field, field_validator +from pydantic import BaseModel, Field +from pydantic import __version__ as pydantic_version + +# Check if the major version of Pydantic is 1 or 2 +if pydantic_version.startswith("1."): + from pydantic import validator as field_validator +else: + from pydantic import field_validator + from pytrie import StringTrie if TYPE_CHECKING: # pragma: no cover From 2858c9e558332f78747d9847484a87d8b9100e95 Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Fri, 2 Feb 2024 14:18:12 -0600 Subject: [PATCH 4/8] needed version conditions --- src/curies/api.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/curies/api.py b/src/curies/api.py index e5d6e975..40a6a0aa 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -278,7 +278,10 @@ class Records(BaseModel): @field_validator("prefix_synonyms") # type:ignore def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # noqa:N805 """Check that the canonical prefix does not apper in the prefix synonym list.""" - prefix = values.data["prefix"] + if pydantic_version.startswith("1."): + prefix = values["prefix"] + else: + prefix = values.data["prefix"] if prefix in v: raise ValueError(f"Duplicate of canonical prefix `{prefix}` in prefix synonyms") return v @@ -286,7 +289,10 @@ def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # no @field_validator("uri_prefix_synonyms") # type:ignore def uri_prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # noqa:N805 """Check that the canonical URI prefix does not apper in the URI prefix synonym list.""" - uri_prefix = values.data["uri_prefix"] + if pydantic_version.startswith("1."): + uri_prefix = values["uri_prefix"] + else: + uri_prefix = values.data["uri_prefix"] if uri_prefix in v: raise ValueError( f"Duplicate of canonical URI prefix `{uri_prefix}` in URI prefix synonyms" From 177d02a19abc5cafc930c84017520b45fa88ccb7 Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Fri, 2 Feb 2024 14:20:30 -0600 Subject: [PATCH 5/8] # type: ignore --- src/curies/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/curies/api.py b/src/curies/api.py index 40a6a0aa..34298f87 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -281,7 +281,7 @@ def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # no if pydantic_version.startswith("1."): prefix = values["prefix"] else: - prefix = values.data["prefix"] + prefix = values.data["prefix"] # type: ignore if prefix in v: raise ValueError(f"Duplicate of canonical prefix `{prefix}` in prefix synonyms") return v @@ -292,7 +292,7 @@ def uri_prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: if pydantic_version.startswith("1."): uri_prefix = values["uri_prefix"] else: - uri_prefix = values.data["uri_prefix"] + uri_prefix = values.data["uri_prefix"] # type: ignore if uri_prefix in v: raise ValueError( f"Duplicate of canonical URI prefix `{uri_prefix}` in URI prefix synonyms" From 960215f311fff6ed0378246f2d975ef0d0c97abf Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Fri, 2 Feb 2024 14:27:48 -0600 Subject: [PATCH 6/8] unnecessary space --- src/curies/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/curies/api.py b/src/curies/api.py index 34298f87..0ebf2062 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -281,7 +281,7 @@ def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # no if pydantic_version.startswith("1."): prefix = values["prefix"] else: - prefix = values.data["prefix"] # type: ignore + prefix = values.data["prefix"] # type:ignore if prefix in v: raise ValueError(f"Duplicate of canonical prefix `{prefix}` in prefix synonyms") return v @@ -292,7 +292,7 @@ def uri_prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: if pydantic_version.startswith("1."): uri_prefix = values["uri_prefix"] else: - uri_prefix = values.data["uri_prefix"] # type: ignore + uri_prefix = values.data["uri_prefix"] # type:ignore if uri_prefix in v: raise ValueError( f"Duplicate of canonical URI prefix `{uri_prefix}` in URI prefix synonyms" From 927f2ec109b5b407680807391c9136f3ada4efb2 Mon Sep 17 00:00:00 2001 From: Charles Tapley Hoyt Date: Sun, 4 Feb 2024 11:26:50 +0100 Subject: [PATCH 7/8] Add pydantic compatibility submodule --- src/curies/_pydantic_compat.py | 24 ++++++++++ src/curies/api.py | 87 ++++++++++++---------------------- 2 files changed, 53 insertions(+), 58 deletions(-) create mode 100644 src/curies/_pydantic_compat.py diff --git a/src/curies/_pydantic_compat.py b/src/curies/_pydantic_compat.py new file mode 100644 index 00000000..620d1191 --- /dev/null +++ b/src/curies/_pydantic_compat.py @@ -0,0 +1,24 @@ +"""A compatibility layer for pydantic 1 and 2.""" + +from pydantic import __version__ as pydantic_version + +__all__ = [ + "PYDANTIC_V1", + "field_validator", + "get_field_validator_values", +] + +PYDANTIC_V1 = pydantic_version.startswith("1.") + +if PYDANTIC_V1: + from pydantic import validator as field_validator +else: + from pydantic import field_validator + + +def get_field_validator_values(values, key: str): + """Get the value for the key from a field validator object, cross-compatible with Pydantic 1 and 2.""" + if PYDANTIC_V1: + return values[key] + else: + return values.data[key] # type:ignore diff --git a/src/curies/api.py b/src/curies/api.py index 0ebf2062..c319018c 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -34,16 +34,10 @@ import requests from pydantic import BaseModel, Field -from pydantic import __version__ as pydantic_version - -# Check if the major version of Pydantic is 1 or 2 -if pydantic_version.startswith("1."): - from pydantic import validator as field_validator -else: - from pydantic import field_validator - from pytrie import StringTrie +from ._pydantic_compat import PYDANTIC_V1, field_validator, get_field_validator_values + if TYPE_CHECKING: # pragma: no cover import pandas import rdflib @@ -278,10 +272,7 @@ class Records(BaseModel): @field_validator("prefix_synonyms") # type:ignore def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # noqa:N805 """Check that the canonical prefix does not apper in the prefix synonym list.""" - if pydantic_version.startswith("1."): - prefix = values["prefix"] - else: - prefix = values.data["prefix"] # type:ignore + prefix = get_field_validator_values(values, "prefix") if prefix in v: raise ValueError(f"Duplicate of canonical prefix `{prefix}` in prefix synonyms") return v @@ -289,10 +280,7 @@ def prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # no @field_validator("uri_prefix_synonyms") # type:ignore def uri_prefix_not_in_synonyms(cls, v: str, values: Mapping[str, Any]) -> str: # noqa:N805 """Check that the canonical URI prefix does not apper in the URI prefix synonym list.""" - if pydantic_version.startswith("1."): - uri_prefix = values["uri_prefix"] - else: - uri_prefix = values.data["uri_prefix"] # type:ignore + uri_prefix = get_field_validator_values(values, "uri_prefix") if uri_prefix in v: raise ValueError( f"Duplicate of canonical URI prefix `{uri_prefix}` in URI prefix synonyms" @@ -1048,8 +1036,7 @@ def is_uri(self, s: str) -> bool: @overload def compress_or_standardize( self, uri_or_curie: str, *, strict: Literal[True] = True, passthrough: bool = False - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload @@ -1059,8 +1046,7 @@ def compress_or_standardize( *, strict: Literal[False] = False, passthrough: Literal[True] = True, - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload @@ -1070,8 +1056,7 @@ def compress_or_standardize( *, strict: Literal[False] = False, passthrough: Literal[False] = False, - ) -> Optional[str]: - ... + ) -> Optional[str]: ... def compress_or_standardize( self, uri_or_curie: str, *, strict: bool = False, passthrough: bool = False @@ -1128,22 +1113,21 @@ def compress_strict(self, uri: str) -> str: # docstr-coverage:excused `overload` @overload - def compress(self, uri: str, *, strict: Literal[True] = True, passthrough: bool = False) -> str: - ... + def compress( + self, uri: str, *, strict: Literal[True] = True, passthrough: bool = False + ) -> str: ... # docstr-coverage:excused `overload` @overload def compress( self, uri: str, *, strict: Literal[False] = False, passthrough: Literal[True] = True - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload def compress( self, uri: str, *, strict: Literal[False] = False, passthrough: Literal[False] = False - ) -> Optional[str]: - ... + ) -> Optional[str]: ... def compress( self, uri: str, *, strict: bool = False, passthrough: bool = False @@ -1238,8 +1222,7 @@ def is_curie(self, s: str) -> bool: @overload def expand_or_standardize( self, curie_or_uri: str, *, strict: Literal[True] = True, passthrough: bool = False - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload @@ -1249,8 +1232,7 @@ def expand_or_standardize( *, strict: Literal[False] = False, passthrough: Literal[True] = True, - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload @@ -1260,8 +1242,7 @@ def expand_or_standardize( *, strict: Literal[False] = False, passthrough: Literal[False] = False, - ) -> Optional[str]: - ... + ) -> Optional[str]: ... def expand_or_standardize( self, curie_or_uri: str, *, strict: bool = False, passthrough: bool = False @@ -1318,22 +1299,21 @@ def expand_strict(self, curie: str) -> str: # docstr-coverage:excused `overload` @overload - def expand(self, curie: str, *, strict: Literal[True] = True, passthrough: bool = False) -> str: - ... + def expand( + self, curie: str, *, strict: Literal[True] = True, passthrough: bool = False + ) -> str: ... # docstr-coverage:excused `overload` @overload def expand( self, curie: str, *, strict: Literal[False] = False, passthrough: Literal[True] = True - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload def expand( self, curie: str, *, strict: Literal[False] = False, passthrough: Literal[False] = False - ) -> Optional[str]: - ... + ) -> Optional[str]: ... def expand( self, curie: str, *, strict: bool = False, passthrough: bool = False @@ -1473,22 +1453,19 @@ def expand_pair_all(self, prefix: str, identifier: str) -> Optional[Collection[s @overload def standardize_prefix( self, prefix: str, *, strict: Literal[True] = True, passthrough: bool = False - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload def standardize_prefix( self, prefix: str, *, strict: Literal[False] = False, passthrough: Literal[True] = True - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload def standardize_prefix( self, prefix: str, *, strict: Literal[False] = False, passthrough: Literal[False] = False - ) -> Optional[str]: - ... + ) -> Optional[str]: ... def standardize_prefix( self, prefix: str, *, strict: bool = False, passthrough: bool = False @@ -1532,22 +1509,19 @@ def standardize_prefix( @overload def standardize_curie( self, curie: str, *, strict: Literal[True] = True, passthrough: bool = False - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload def standardize_curie( self, curie: str, *, strict: Literal[False] = False, passthrough: Literal[True] = True - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload def standardize_curie( self, curie: str, *, strict: Literal[False] = False, passthrough: Literal[False] = False - ) -> Optional[str]: - ... + ) -> Optional[str]: ... def standardize_curie( self, curie: str, *, strict: bool = False, passthrough: bool = False @@ -1594,22 +1568,19 @@ def standardize_curie( @overload def standardize_uri( self, uri: str, *, strict: Literal[True] = True, passthrough: bool = False - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload def standardize_uri( self, uri: str, *, strict: Literal[False] = False, passthrough: Literal[True] = True - ) -> str: - ... + ) -> str: ... # docstr-coverage:excused `overload` @overload def standardize_uri( self, uri: str, *, strict: Literal[False] = False, passthrough: Literal[False] = False - ) -> Optional[str]: - ... + ) -> Optional[str]: ... def standardize_uri( self, uri: str, *, strict: bool = False, passthrough: bool = False From 7e18dd510e7e3ce178c47dfefc5f656f2039de5b Mon Sep 17 00:00:00 2001 From: Charles Tapley Hoyt Date: Sun, 4 Feb 2024 11:33:12 +0100 Subject: [PATCH 8/8] Fix --- src/curies/_pydantic_compat.py | 4 ++-- src/curies/api.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/curies/_pydantic_compat.py b/src/curies/_pydantic_compat.py index 620d1191..71471ea2 100644 --- a/src/curies/_pydantic_compat.py +++ b/src/curies/_pydantic_compat.py @@ -16,9 +16,9 @@ from pydantic import field_validator -def get_field_validator_values(values, key: str): +def get_field_validator_values(values, key: str): # type:ignore """Get the value for the key from a field validator object, cross-compatible with Pydantic 1 and 2.""" if PYDANTIC_V1: return values[key] else: - return values.data[key] # type:ignore + return values.data[key] diff --git a/src/curies/api.py b/src/curies/api.py index c319018c..730d4e6a 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -36,7 +36,7 @@ from pydantic import BaseModel, Field from pytrie import StringTrie -from ._pydantic_compat import PYDANTIC_V1, field_validator, get_field_validator_values +from ._pydantic_compat import field_validator, get_field_validator_values if TYPE_CHECKING: # pragma: no cover import pandas