-
-
Notifications
You must be signed in to change notification settings - Fork 544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ANN401: Replace Any #3526
base: main
Are you sure you want to change the base?
ANN401: Replace Any #3526
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
import sys | ||
import typing | ||
from collections import abc | ||
from enum import Enum | ||
from enum import Enum, EnumMeta | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
|
@@ -179,13 +179,13 @@ def create_concrete_type(self, evaled_type: type) -> type: | |
return evaled_type.__strawberry_definition__.resolve_generic(evaled_type) | ||
raise ValueError(f"Not supported {evaled_type}") | ||
|
||
def create_enum(self, evaled_type: Any) -> EnumDefinition: | ||
def create_enum(self, evaled_type: EnumMeta) -> EnumDefinition: | ||
patrick91 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try: | ||
return evaled_type._enum_definition | ||
except AttributeError: | ||
raise NotAStrawberryEnumError(evaled_type) | ||
|
||
def create_list(self, evaled_type: Any) -> StrawberryList: | ||
def create_list(self, evaled_type: TypeVar) -> StrawberryList: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Clarify the expected type for Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you sure these are typevars? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ops, I double checked and most seem to be |
||
item_type, *_ = get_args(evaled_type) | ||
of_type = StrawberryAnnotation( | ||
annotation=item_type, | ||
|
@@ -194,7 +194,7 @@ def create_list(self, evaled_type: Any) -> StrawberryList: | |
|
||
return StrawberryList(of_type) | ||
|
||
def create_optional(self, evaled_type: Any) -> StrawberryOptional: | ||
def create_optional(self, evaled_type: TypeVar) -> StrawberryOptional: | ||
types = get_args(evaled_type) | ||
non_optional_types = tuple( | ||
filter( | ||
|
@@ -263,15 +263,15 @@ def _is_async_type(cls, annotation: type) -> bool: | |
return origin in ASYNC_TYPES | ||
|
||
@classmethod | ||
def _is_enum(cls, annotation: Any) -> bool: | ||
def _is_enum(cls, annotation: TypeVar) -> bool: | ||
# Type aliases are not types so we need to make sure annotation can go into | ||
# issubclass | ||
if not isinstance(annotation, type): | ||
return False | ||
return issubclass(annotation, Enum) | ||
|
||
@classmethod | ||
def _is_graphql_generic(cls, annotation: Any) -> bool: | ||
def _is_graphql_generic(cls, annotation: type) -> bool: | ||
if hasattr(annotation, "__origin__"): | ||
if definition := get_object_definition(annotation.__origin__): | ||
return definition.is_graphql_generic | ||
|
@@ -281,11 +281,11 @@ def _is_graphql_generic(cls, annotation: Any) -> bool: | |
return False | ||
|
||
@classmethod | ||
def _is_lazy_type(cls, annotation: Any) -> bool: | ||
def _is_lazy_type(cls, annotation: TypeVar) -> bool: | ||
return isinstance(annotation, LazyType) | ||
|
||
@classmethod | ||
def _is_optional(cls, annotation: Any, args: List[Any]) -> bool: | ||
def _is_optional(cls, annotation: TypeVar, args: List[type]) -> bool: | ||
"""Returns True if the annotation is Optional[SomeType]""" | ||
|
||
# Optionals are represented as unions | ||
|
@@ -298,7 +298,7 @@ def _is_optional(cls, annotation: Any, args: List[Any]) -> bool: | |
return any(x is type(None) for x in types) | ||
|
||
@classmethod | ||
def _is_list(cls, annotation: Any) -> bool: | ||
def _is_list(cls, annotation: TypeVar) -> bool: | ||
"""Returns True if annotation is a List""" | ||
|
||
annotation_origin = get_origin(annotation) | ||
|
@@ -312,7 +312,7 @@ def _is_list(cls, annotation: Any) -> bool: | |
) | ||
|
||
@classmethod | ||
def _is_strawberry_type(cls, evaled_type: Any) -> bool: | ||
def _is_strawberry_type(cls, evaled_type: TypeVar) -> bool: | ||
# Prevent import cycles | ||
from strawberry.union import StrawberryUnion | ||
|
||
|
@@ -339,7 +339,7 @@ def _is_strawberry_type(cls, evaled_type: Any) -> bool: | |
return False | ||
|
||
@classmethod | ||
def _is_union(cls, annotation: Any, args: List[Any]) -> bool: | ||
def _is_union(cls, annotation: TypeVar, args: List[type]) -> bool: | ||
"""Returns True if annotation is a Union""" | ||
|
||
# this check is needed because unions declared with the new syntax `A | B` | ||
|
@@ -364,7 +364,7 @@ def _is_union(cls, annotation: Any, args: List[Any]) -> bool: | |
return any(isinstance(arg, StrawberryUnion) for arg in args) | ||
|
||
@classmethod | ||
def _strip_async_type(cls, annotation: Type[Any]) -> type: | ||
def _strip_async_type(cls, annotation: Type) -> type: | ||
return annotation.__args__[0] | ||
|
||
@classmethod | ||
|
@@ -377,7 +377,7 @@ def _strip_lazy_type(cls, annotation: LazyType[Any, Any]) -> type: | |
################################################################################ | ||
|
||
|
||
def _is_input_type(type_: Any) -> bool: | ||
def _is_input_type(type_: TypeVar) -> bool: | ||
if not has_object_definition(type_): | ||
return False | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Optional, Union, cast | ||
from typing import Any, Dict, Optional, Union, cast | ||
from typing_extensions import Annotated, get_args, get_origin | ||
|
||
from strawberry.type import StrawberryType | ||
|
@@ -24,11 +24,11 @@ class StrawberryAutoMeta(type): | |
|
||
""" | ||
|
||
def __init__(self, *args: str, **kwargs: Any) -> None: | ||
def __init__(self, *args: str, **kwargs: Dict[Any, Any]) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kwargs are implicitly marked as dicts 😊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm should we keep the Dict annotations? Ruff complained about the Any. |
||
self._instance: Optional[StrawberryAuto] = None | ||
super().__init__(*args, **kwargs) | ||
|
||
def __call__(cls, *args: str, **kwargs: Any) -> Any: | ||
def __call__(cls, *args: str, **kwargs: Dict[Any, Any]) -> StrawberryAuto: | ||
if cls._instance is None: | ||
cls._instance = super().__call__(*args, **kwargs) | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,8 @@ | |||||
TYPE_CHECKING, | ||||||
Any, | ||||||
Callable, | ||||||
Dict, | ||||||
List, | ||||||
Mapping, | ||||||
Optional, | ||||||
Union, | ||||||
|
@@ -139,7 +141,7 @@ def __init__( | |||||
graphql_ide: Optional[GraphQL_IDE] = "graphiql", | ||||||
allow_queries_via_get: bool = True, | ||||||
subscriptions_enabled: bool = False, | ||||||
**kwargs: Any, | ||||||
**kwargs: Dict[Any, Any], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider using Using
Suggested change
|
||||||
) -> None: | ||||||
self.schema = schema | ||||||
self.allow_queries_via_get = allow_queries_via_get | ||||||
|
@@ -198,15 +200,17 @@ class GraphQLView( | |||||
def get_root_value(self, request: HttpRequest) -> Optional[RootValue]: | ||||||
return None | ||||||
|
||||||
def get_context(self, request: HttpRequest, response: HttpResponse) -> Any: | ||||||
def get_context( | ||||||
self, request: HttpRequest, response: HttpResponse | ||||||
) -> StrawberryDjangoContext: | ||||||
return StrawberryDjangoContext(request=request, response=response) | ||||||
|
||||||
def get_sub_response(self, request: HttpRequest) -> TemporalHttpResponse: | ||||||
return TemporalHttpResponse() | ||||||
|
||||||
@method_decorator(csrf_exempt) | ||||||
def dispatch( | ||||||
self, request: HttpRequest, *args: Any, **kwargs: Any | ||||||
self, request: HttpRequest, *args: List[Any], **kwargs: Dict[Any, Any] | ||||||
) -> Union[HttpResponseNotAllowed, TemplateResponse, HttpResponse]: | ||||||
try: | ||||||
return self.run(request=request) | ||||||
|
@@ -245,7 +249,7 @@ class AsyncGraphQLView( | |||||
request_adapter_class = AsyncDjangoHTTPRequestAdapter | ||||||
|
||||||
@classonlymethod # pyright: ignore[reportIncompatibleMethodOverride] | ||||||
def as_view(cls, **initkwargs: Any) -> Callable[..., HttpResponse]: | ||||||
def as_view(cls, **initkwargs: Dict[Any, Any]) -> Callable[..., HttpResponse]: | ||||||
# This code tells django that this view is async, see docs here: | ||||||
# https://docs.djangoproject.com/en/3.1/topics/async/#async-views | ||||||
|
||||||
|
@@ -254,18 +258,20 @@ def as_view(cls, **initkwargs: Any) -> Callable[..., HttpResponse]: | |||||
|
||||||
return view | ||||||
|
||||||
async def get_root_value(self, request: HttpRequest) -> Any: | ||||||
async def get_root_value(self, request: HttpRequest) -> None: | ||||||
return None | ||||||
|
||||||
async def get_context(self, request: HttpRequest, response: HttpResponse) -> Any: | ||||||
async def get_context( | ||||||
self, request: HttpRequest, response: HttpResponse | ||||||
) -> StrawberryDjangoContext: | ||||||
return StrawberryDjangoContext(request=request, response=response) | ||||||
|
||||||
async def get_sub_response(self, request: HttpRequest) -> TemporalHttpResponse: | ||||||
return TemporalHttpResponse() | ||||||
|
||||||
@method_decorator(csrf_exempt) | ||||||
async def dispatch( # pyright: ignore | ||||||
self, request: HttpRequest, *args: Any, **kwargs: Any | ||||||
self, request: HttpRequest, *args: List[Any], **kwargs: Dict[Any, Any] | ||||||
) -> Union[HttpResponseNotAllowed, TemplateResponse, HttpResponse]: | ||||||
try: | ||||||
return await self.run(request=request) | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||
|
||||||
import itertools | ||||||
from functools import cached_property | ||||||
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Union | ||||||
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, Union | ||||||
|
||||||
if TYPE_CHECKING: | ||||||
from typing_extensions import TypeAlias | ||||||
|
@@ -20,14 +20,22 @@ def apply(self, field: StrawberryField) -> None: # pragma: no cover | |||||
pass | ||||||
|
||||||
def resolve( | ||||||
self, next_: SyncExtensionResolver, source: Any, info: Info, **kwargs: Any | ||||||
self, | ||||||
next_: SyncExtensionResolver, | ||||||
source: Any, | ||||||
info: Info, | ||||||
**kwargs: Dict[Any, Any], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider using Using
Suggested change
|
||||||
) -> Any: # pragma: no cover | ||||||
raise NotImplementedError( | ||||||
"Sync Resolve is not supported for this Field Extension" | ||||||
) | ||||||
|
||||||
async def resolve_async( | ||||||
self, next_: AsyncExtensionResolver, source: Any, info: Info, **kwargs: Any | ||||||
self, | ||||||
next_: AsyncExtensionResolver, | ||||||
source: Any, | ||||||
info: Info, | ||||||
**kwargs: Dict[Any, Any], | ||||||
) -> Any: # pragma: no cover | ||||||
raise NotImplementedError( | ||||||
"Async Resolve is not supported for this Field Extension" | ||||||
|
@@ -47,7 +55,11 @@ class SyncToAsyncExtension(FieldExtension): | |||||
Applied automatically""" | ||||||
|
||||||
async def resolve_async( | ||||||
self, next_: AsyncExtensionResolver, source: Any, info: Info, **kwargs: Any | ||||||
self, | ||||||
next_: AsyncExtensionResolver, | ||||||
source: Any, | ||||||
info: Info, | ||||||
**kwargs: Dict[Any, Any], | ||||||
) -> Any: | ||||||
return next_(source, info, **kwargs) | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,7 @@ def get_generic_alias(type_: Type) -> Type: | |
raise AssertionError(f"No GenericAlias available for {type_}") # pragma: no cover | ||
|
||
|
||
def is_generic_alias(type_: Any) -> TypeGuard[_GenericAlias]: | ||
def is_generic_alias(type_: TypeVar) -> TypeGuard[_GenericAlias]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Clarify the expected type for Using |
||
"""Returns True if the type is a generic alias.""" | ||
# _GenericAlias overrides all the methods that we can use to know if | ||
# this is a subclass of it. But if it has an "_inst" attribute | ||
|
@@ -323,7 +323,7 @@ def _get_namespace_from_ast( | |
|
||
|
||
def eval_type( | ||
type_: Any, | ||
type_: TypeVar, | ||
globalns: Optional[Dict] = None, | ||
localns: Optional[Dict] = None, | ||
) -> Type: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmh, I think we don't need Optional here 🤔 (same below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added Optional because of this implementation, is it wrong?
strawberry/strawberry/channels/handlers/ws_handler.py
Lines 118 to 119 in 2f02b49