-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change how Private is implemented to better support type checkers (#1437
- Loading branch information
Showing
6 changed files
with
86 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Release type: minor | ||
|
||
This release changes how `strawberry.Private` is implemented to | ||
improve support for type checkers. |
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 |
---|---|---|
@@ -1,27 +1,31 @@ | ||
class Private: | ||
"""Represent a private field that won't be converted into a GraphQL field | ||
from typing import TypeVar | ||
|
||
Example: | ||
from typing_extensions import Annotated, get_args, get_origin | ||
|
||
>>> import strawberry | ||
>>> @strawberry.type | ||
... class User: | ||
... name: str | ||
... age: strawberry.Private[int] | ||
""" | ||
|
||
__slots__ = ("type",) | ||
class StrawberryPrivate: | ||
... | ||
|
||
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}]" | ||
T = TypeVar("T") | ||
|
||
def __class_getitem__(cls, type): | ||
return Private(type) | ||
Private = Annotated[T, StrawberryPrivate()] | ||
Private.__doc__ = """Represent a private field that won't be converted into a GraphQL field | ||
Example: | ||
>>> import strawberry | ||
>>> @strawberry.type | ||
... class User: | ||
... name: str | ||
... age: strawberry.Private[int] | ||
""" | ||
|
||
|
||
def is_private(type_: object) -> bool: | ||
if get_origin(type_) is Annotated: | ||
return any( | ||
isinstance(argument, StrawberryPrivate) for argument in get_args(type_) | ||
) | ||
|
||
return False |
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,50 @@ | ||
from .utils import Result, requires_pyright, run_pyright, skip_on_windows | ||
|
||
|
||
pytestmark = [skip_on_windows, requires_pyright] | ||
|
||
|
||
CODE = """ | ||
import strawberry | ||
@strawberry.type | ||
class User: | ||
name: str | ||
age: strawberry.Private[int] | ||
patrick = User(name="Patrick", age=1) | ||
User(n="Patrick") | ||
reveal_type(patrick.name) | ||
reveal_type(patrick.age) | ||
""" | ||
|
||
|
||
def test_pyright(): | ||
results = run_pyright(CODE) | ||
|
||
assert results == [ | ||
Result( | ||
type="error", | ||
message='No parameter named "n" (reportGeneralTypeIssues)', | ||
line=12, | ||
column=6, | ||
), | ||
Result( | ||
type="error", | ||
message=( | ||
"Arguments missing for parameters " | ||
'"name", "age" (reportGeneralTypeIssues)' | ||
), | ||
line=12, | ||
column=1, | ||
), | ||
Result( | ||
type="info", message='Type of "patrick.name" is "str"', line=14, column=13 | ||
), | ||
Result( | ||
type="info", message='Type of "patrick.age" is "int"', line=15, column=13 | ||
), | ||
] |