Skip to content

Commit a7663f4

Browse files
authored
Use Self for __new__ implementations (#259)
1 parent 81ef8aa commit a7663f4

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/graphql/pyutils/undefined.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
from __future__ import annotations
44

55
import warnings
6+
from typing import TYPE_CHECKING
7+
8+
if TYPE_CHECKING:
9+
try:
10+
from typing import Self
11+
except ImportError: # Python < 3.11
12+
from typing_extensions import Self
613

714
__all__ = ["Undefined", "UndefinedType"]
815

916

1017
class UndefinedType:
1118
"""Auxiliary class for creating the Undefined singleton."""
1219

13-
_instance: UndefinedType | None = None
20+
_instance: Self | None = None
1421

15-
def __new__(cls) -> UndefinedType:
22+
def __new__(cls) -> Self:
1623
"""Create the Undefined singleton."""
1724
if cls._instance is None:
1825
cls._instance = super().__new__(cls)

src/graphql/type/definition.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959
if TYPE_CHECKING:
6060
from .schema import GraphQLSchema
6161

62+
try:
63+
from typing import Self
64+
except ImportError: # Python < 3.11
65+
from typing_extensions import Self
66+
6267

6368
__all__ = [
6469
"GraphQLAbstractType",
@@ -232,7 +237,7 @@ class GraphQLNamedType(GraphQLType):
232237

233238
reserved_types: Mapping[str, GraphQLNamedType] = {}
234239

235-
def __new__(cls, name: str, *_args: Any, **_kwargs: Any) -> GraphQLNamedType:
240+
def __new__(cls, name: str, *_args: Any, **_kwargs: Any) -> Self:
236241
"""Create a GraphQL named type."""
237242
if name in cls.reserved_types:
238243
msg = f"Redefinition of reserved type {name!r}"

0 commit comments

Comments
 (0)