strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Add support for `__slots__` in python 3.10

Open patrick91 opened this issue 3 years ago • 5 comments

In python 3.10 you can use slots=True when applying the dataclasses decorator

see https://docs.python.org/3.10/whatsnew/3.10.html#slots

from dataclasses import dataclass

@dataclass(slots=True)
class Birthday:
    name: str
    birthday: datetime.date

we should think about whether we expose this to end users, like so:

import strawberry

@strawberry.type(slots=True)
class Birthday:
    name: str
    birthday: datetime.date

or if we enable it automatically (but we should still allow to disable it if that's the case)

patrick91 avatar May 09 '22 15:05 patrick91

hey @patrick91 I was trying my luck with adding __slots__ to strawberry.type and I have a question. Does strawberry requires all of the queries / mutations / subscription to be under one huge child that is inheriting from all other? or they are composed and does not require inheritance. If the first is the case I'm afraid it would be impossible unless you have all unique field names and do some hacks, see this SO for more detail

nrbnlulu avatar Jul 10 '22 08:07 nrbnlulu

@nrbnlulu we could still allow to users to enable slots manually maybe :)

field names have to be unique, so maybe we are fine? also maybe we could find a way to prevent slots from being applied to root level types (maybe types that extend other types?)

patrick91 avatar Jul 10 '22 11:07 patrick91

we could still allow to users to enable slots manually maybe :)

Sure, that is not the problem though hehe... but if you mentioned this, I thought to do somthing like this:

def _gte_310() -> bool:
    return sys.version_info >= (3, 10)

# inside strawberry.type we expose the slots argument
def _wrap_dataclass(cls: Type, slots: bool = _gte_310()):
    """Wrap a strawberry.type class with a dataclass and check for any issues
    before doing so"""

    # Ensure all Fields have been properly type-annotated
    _check_field_annotations(cls)

    return dataclasses.dataclass(slots=slots)(cls)

This would prevent to do pytest.mark.parameterize all over the place. What are your thoughts?


field names have to be unique, so maybe we are fine?

Unique where? at all the schema? does fields get renamed implicitly by strawberry?

nrbnlulu avatar Jul 10 '22 12:07 nrbnlulu

we could still allow to users to enable slots manually maybe :)

Sure, that is not the problem though hehe... but if you mentioned this, I thought to do somthing like this:

def _gte_310() -> bool:
    return sys.version_info >= (3, 10)

# inside strawberry.type we expose the slots argument
def _wrap_dataclass(cls: Type, slots: bool = _gte_310()):
    """Wrap a strawberry.type class with a dataclass and check for any issues
    before doing so"""

    # Ensure all Fields have been properly type-annotated
    _check_field_annotations(cls)

    return dataclasses.dataclass(slots=slots)(cls)

This would prevent to do pytest.mark.parameterize all over the place. What are your thoughts?

I think we can backport this feature to older versions of python :)

field names have to be unique, so maybe we are fine?

Unique where? at all the schema? does fields get renamed implicitly by strawberry?

Unique per type :) we don't do any renaming when extending, I can't remember if we error out, if that's not the case we should :)

patrick91 avatar Jul 10 '22 12:07 patrick91

lets continue the discussion on the PR

nrbnlulu avatar Jul 10 '22 13:07 nrbnlulu