-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #556 from bioimage-io/better_axis_id_repr
Make AxisId inherite from str to be usable with xarray.DataArray
- Loading branch information
Showing
13 changed files
with
121 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import sys | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, Type | ||
|
||
import annotated_types | ||
from pydantic import GetCoreSchemaHandler, functional_validators | ||
from pydantic_core import CoreSchema | ||
from pydantic_core.core_schema import no_info_after_validator_function | ||
|
||
if sys.version_info < (3, 10): | ||
SLOTS: Dict[str, bool] = {} | ||
KW_ONLY: Dict[str, bool] = {} | ||
else: | ||
SLOTS = {"slots": True} | ||
KW_ONLY = {"kw_only": True} | ||
|
||
|
||
# TODO: make sure we use this one everywhere and not the vanilla pydantic one | ||
@dataclass(frozen=True, **SLOTS) | ||
class AfterValidator(functional_validators.AfterValidator): | ||
def __str__(self): | ||
return f"AfterValidator({self.func.__name__})" | ||
|
||
|
||
# TODO: make sure we use this one everywhere and not the vanilla pydantic one | ||
@dataclass(frozen=True, **SLOTS) | ||
class BeforeValidator(functional_validators.BeforeValidator): | ||
def __str__(self): | ||
return f"BeforeValidator({self.func.__name__})" | ||
|
||
|
||
# TODO: make sure we use this one everywhere and not the vanilla pydantic one | ||
@dataclass(frozen=True, **SLOTS) | ||
class Predicate(annotated_types.Predicate): | ||
def __str__(self): | ||
return f"Predicate({self.func.__name__})" | ||
|
||
|
||
@dataclass(frozen=True, **SLOTS) | ||
class RestrictCharacters: | ||
alphabet: str | ||
|
||
def __get_pydantic_core_schema__( | ||
self, source: Type[Any], handler: GetCoreSchemaHandler | ||
) -> CoreSchema: | ||
if not self.alphabet: | ||
raise ValueError("Alphabet may not be empty") | ||
schema = handler(source) # get the CoreSchema from the type / inner constraints | ||
if schema["type"] != "str": | ||
raise TypeError("RestrictCharacters can only be applied to strings") | ||
return no_info_after_validator_function( | ||
self.validate, | ||
schema, | ||
) | ||
|
||
def validate(self, value: str) -> str: | ||
if any(c not in self.alphabet for c in value): | ||
raise ValueError(f"{value!r} is not restricted to {self.alphabet!r}") | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.