Skip to content
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

Decorate stateclass with @dataclass_transform() #1113

Open
kkom opened this issue Nov 23, 2024 · 1 comment
Open

Decorate stateclass with @dataclass_transform() #1113

kkom opened this issue Nov 23, 2024 · 1 comment

Comments

@kkom
Copy link

kkom commented Nov 23, 2024

Is your feature request related to a problem? Please describe.

I have pretty strict type checking enabled in my project. This results in hitting Pyright's reportUninitializedInstanceVariable rule when defining a Mesop stateclass:

@me.stateclass
class State:
    first_name: str

That's the error:

Instance variable "first_name" is not initialized in the class body or __init__ method reportUninitializedInstanceVariable

Describe the solution you'd like

I wonder if it's possible to annotate stateclass with dataclass_transform(), to make the type checker okay with things that normally look sketchy to it?

I.e. go from this:

mesop/mesop/__init__.py

Lines 258 to 262 in 85fac40

def stateclass(cls: type[_T] | None, **kw_args: Any) -> type[_T]:
"""
Similar as dataclass, but it also registers with Mesop runtime().
"""

To this:

if sys.version_info >= (3, 10):
    from typing import dataclass_transform
else:
    from typing_extensions import dataclass_transform


...


@dataclass_transform()
def stateclass(cls: type[_T] | None, **kw_args: Any) -> type[_T]:
  """
  Similar as dataclass, but it also registers with Mesop runtime().
  """

Describe alternatives you've considered

Can't think of any.

Additional context

Note – I'm vaguely aware of dataclass transforms. I am not 100% sure what other things need to be considered when adding this annotation, i.e. if the runtime code must then implement specific parts of the dataclass spec.

But I can say that making that change fixed the static errors for me.

@wwwillchen
Copy link
Collaborator

Looks like a reasonable change - feel free to open a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants