Skip to content

Commit cd1622c

Browse files
committed
feat: adding in the IDCoercible type for turms generated functions
1 parent 1746140 commit cd1622c

File tree

3 files changed

+15
-85
lines changed

3 files changed

+15
-85
lines changed

rath/qt/__init__.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

rath/qt/helpers.py

Lines changed: 0 additions & 64 deletions
This file was deleted.

rath/scalars.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pydantic import BaseModel
2-
from typing import Any, Type
2+
from typing import Any, Type, Protocol
33
from pydantic import GetCoreSchemaHandler
44
from pydantic_core import CoreSchema, core_schema
55

@@ -8,9 +8,9 @@ class ID(str):
88
"""A custom scalar for IDs. If passed a pydantic model it an id property
99
it will automatically validate to that models id"""
1010

11-
def __init__(self, value: str) -> None:
12-
"""Initialize the ID"""
13-
self.value = value
11+
def __get__(self, instance, owner) -> "ID": ...
12+
13+
def __set__(self, instance, value: "IDCoercible") -> None: ...
1414

1515
@classmethod
1616
def __get_pydantic_core_schema__(
@@ -22,7 +22,7 @@ def __get_pydantic_core_schema__(
2222
return core_schema.no_info_before_validator_function(cls.validate, handler(str))
2323

2424
@classmethod
25-
def validate(cls: Type["ID"], v: Any) -> "ID":
25+
def validate(cls: Type["ID"], v: "IDCoercible") -> "ID":
2626
"""Validate the ID"""
2727
if isinstance(v, BaseModel):
2828
if hasattr(v, "id"):
@@ -36,4 +36,13 @@ def validate(cls: Type["ID"], v: Any) -> "ID":
3636
if isinstance(v, int):
3737
return cls(str(v))
3838

39-
raise TypeError(f"Needs to be either a instance of BaseModel (with an id) or a string got {type(v)}")
39+
raise TypeError(
40+
f"Needs to be either a instance of BaseModel (with an id) or a string got {type(v)}"
41+
)
42+
43+
44+
class WithId(Protocol):
45+
id: ID
46+
47+
48+
IDCoercible = str | ID | WithId

0 commit comments

Comments
 (0)