-
Hi folks, here I am with another question 😊 Excuse me for the title, not sure it makes sense :) In Strawberry we have a class called import strawberry
@strawberry.type
class User:
name: str
age: strawberry.Private[int]
patrick = User(name="Patrick", age=1)
reveal_type(patrick.age) unfortunately this breaks the type checkers since For MyPy we made a plugin to prevent this problem, is there a way to make this class a pass through to the passed type? The only alternative I'm thinking of is to convert the class to be a function that returns Here's our implementation: class Private:
__slots__ = ("type",)
def __init__(self, type):
self.type = type
def __repr__(self):
if isinstance(self.type, type):
type_name = self.type.__name__
else:
# typing objects, e.g. List[int]
type_name = repr(self.type)
return f"strawberry.Private[{type_name}]"
def __class_getitem__(cls, type):
return Private(type) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Another option is to adopt something similar to the dataclass It would look something like this: @strawberry.type
class User:
name: str
age: int = strawberry.field(private=True) (One potential problem with This approach would also work with dataclass transform. |
Beta Was this translation helpful? Give feedback.
-
I solved with https://github.com/strawberry-graphql/strawberry/pull/1437/files |
Beta Was this translation helpful? Give feedback.
I solved with
Private = Annotated[T, StrawberryPrivate()]
^^https://github.com/strawberry-graphql/strawberry/pull/1437/files