diff --git a/docs/README.md b/docs/README.md index a78bb414fe..f660081be7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -27,8 +27,6 @@ ## Editor integration - [Visual Studio Code](./editors/vscode.md) -- [PyCharm](./editors/vscode.md) -- [MyPy](./editors/mypy.md) ## Guides diff --git a/docs/editors/pylance.png b/docs/editors/pylance.png new file mode 100644 index 0000000000..25997be387 Binary files /dev/null and b/docs/editors/pylance.png differ diff --git a/docs/editors/vscode.md b/docs/editors/vscode.md index b75b954219..f9eda041c8 100644 --- a/docs/editors/vscode.md +++ b/docs/editors/vscode.md @@ -17,4 +17,35 @@ The first thing we need to do is to install this is the extension that enables type checking and intellisense for Visual Studio Code. -... +Once the extension is installed, we need to configure it to enable type +checking. To do so we need to change or add the following two settings: + +```json +{ + "python.languageServer": "Pylance", + "python.analysis.typeCheckingMode": "basic" +} +``` + +The first settings tells the editor to use Pylance as the language server. The +second setting tells the editor to enable type checking by using the basic type +checking mode. At the moment strict mode is not supported. + +Once you have configured the settings, you can restart VS Code and you should be +getting type checking errors in vscode. + +![Pylance showing a type error](./pylance.png) + +## Notes + +Unfortunately Pylance doesn't fully support `strawberry.field`, so for example +using this code will result in an error from Pylance: + +```python +@strawberry.type +class User: + id: int + name: str = strawberry.field(resolver=a) +``` + +We will update Strawberry to support this when and if Pylance allows it. diff --git a/strawberry/object_type.py b/strawberry/object_type.py index 7366deb597..8b919d8e37 100644 --- a/strawberry/object_type.py +++ b/strawberry/object_type.py @@ -1,9 +1,8 @@ import dataclasses -from functools import partial from typing import List, Optional, Type, cast from .exceptions import MissingFieldAnnotationError, MissingReturnAnnotationError -from .field import StrawberryField +from .field import StrawberryField, field from .types.type_resolver import _get_fields from .types.types import FederationTypeParams, TypeDefinition from .utils.str_converters import to_camel_case @@ -123,7 +122,7 @@ def _process_type( return cls -@__dataclass_transform__(order_default=True) +@__dataclass_transform__(order_default=True, field_descriptors=(field, StrawberryField)) def type( cls: Type = None, *, @@ -160,7 +159,7 @@ def wrap(cls): return wrap(cls) -@__dataclass_transform__(order_default=True) +@__dataclass_transform__(order_default=True, field_descriptors=(field, StrawberryField)) def input( cls: Type = None, *, @@ -180,7 +179,7 @@ def input( ) -@__dataclass_transform__(order_default=True) +@__dataclass_transform__(order_default=True, field_descriptors=(field, StrawberryField)) def interface( cls: Type = None, *, diff --git a/strawberry/utils/typing.py b/strawberry/utils/typing.py index d9f109fecc..9d3047e689 100644 --- a/strawberry/utils/typing.py +++ b/strawberry/utils/typing.py @@ -1,5 +1,6 @@ +from collections import Callable import typing -from collections.abc import AsyncGenerator, Callable +from collections.abc import AsyncGenerator from typing import Any, Tuple, Type, TypeVar, Union @@ -102,6 +103,6 @@ def __dataclass_transform__( eq_default: bool = True, order_default: bool = False, kw_only_default: bool = False, - field_descriptors: Tuple[Union[type, typing.Callable[..., Any]], ...] = (()), -) -> typing.Callable[[_T], _T]: - return lambda a: a + field_descriptors: Tuple[Union[type, Callable[..., Any]], ...] = (()), +) -> Callable[[_T], _T]: + return lambda a: a \ No newline at end of file