def __new__( cls, v: Union[str, URIRef, URIorCURIE, "NodeIdentifier", BNode] ) -> "NodeIdentifier": if hasattr(v, 'id'): # Allows passing instances of identified objects v = v.id if isinstance(v, NodeIdentifier): if is_strict() and not issubclass(type(v), cls): raise ValueError( f"Invalid identifier type for {cls.__name__}: {v} ({type(v).__name__})" ) else: return super().__new__(cls, v) elif type(v) in (str, URIRef, URIorCURIE, BNode) or not is_strict(): return super().__new__(cls, str(v)) else: raise ValueError(f"Unknown identifier for {cls.__name__}: {v}")
def __init__( self, v: Union[str, URIRef, BNode, "URIorCURIE", "Identifier"]) -> None: v = str(v) if not isinstance(v, str) else v if is_strict() and not self.is_valid(v): raise ValueError(f"{v}: is not a valid Identifier") TypedNode.__init__(self, v)
def __new__(cls, value: Union[str, datetime.date, Literal]) -> str: if is_strict() and not cls.is_valid(value): raise ValueError(f"{value} is not a valid date") if isinstance(value, Literal): value = value.value try: if not isinstance(value, datetime.date): value = datetime.date.fromisoformat(str(value)) return value.isoformat() except TypeError as e: pass except ValueError as e: pass if not is_strict(): return str(value) raise e
def __new__(cls, v: Union[str, bool, "Bool"]) -> bool: if isinstance(v, bool): return v if cls.bool_true.match(str(v)): return True if cls.bool_false.match(str(v)): return False if is_strict(): raise ValueError(f"{v}: Must be a boolean value") return bool(v)
def __new__( cls, v: Union[str, URIRef, URIorCURIE, NodeIdentifier, "ElementIdentifier"] ) -> "ElementIdentifier": if hasattr(v, 'id'): # Allows passing instances of identified objects v = v.id if is_strict() and (type(v) is BNode or str(v).startswith('_:')): raise ValueError( f"Invalid identifier type for {cls.__name__}: {v} ({type(v).__name__})" ) return super().__new__(cls, v)
def __init__(self, v: str) -> None: if is_strict() and not self.is_valid(v): raise ValueError(f"{TypedNode.yaml_loc(v)}{v}: Not a valid NCName") self.nsm: Optional[Namespaces] = None super().__init__()
def __init__(self, v: str, *args, **kwargs) -> None: if is_strict() and not self.is_valid(v): raise ValueError(f"{v} is not a valid CURIE") super().__init__(v, *args, **kwargs)
def __init__(self, v: Union[str, URIRef, "Curie", "URIorCURIE"], *args, **kwargs) -> None: if is_strict() and not URIorCURIE.is_valid(v): raise ValueError(f"{v} is not a valid URI or CURIE") super().__init__(v, *args, **kwargs)
def __init__(self, v: str) -> None: if is_strict() and not self.is_valid(v): raise ValueError(f"{v}: invalid prefix") self.nsm: Optional[Namespaces] = None super().__init__()