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

Support dataclass transform #2049

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

Morikko
Copy link

@Morikko Morikko commented Feb 15, 2025

Goal

Support dataclass transform as specified: https://typing.readthedocs.io/en/latest/spec/dataclasses.html#dataclass-transform

Motivation

To have signature completions when using libraries like Pydantic or SQLalchemy.

Features

  • dataclass init=False
  • Final attributes (already supported, just tested)
  • Support ClassVar (excluded from init)
  • dataclass_transform base class
  • dataclass_transform decorator
  • dataclass_transform metaclass
  • (dataclasses.Field, dataclasses.field) init=False
  • dataclass_transform field_specifiers init=False
  • dataclass_transform init=False. The decorator or base model are always assumed to default to init=True however if the transformed class is having init=False then the parameters are ignored.

Technical details

  1. Python Support
# 3.11+
from typing import dataclass_transform
# <3.11
from typing_extensions import dataclass_transform
  1. The decorator usage is very similar to the dataclasses.dataclass

  2. With a difference though:

If dataclass_transform is applied to a class, dataclass-like semantics will be assumed for any class that directly or indirectly derives from the decorated class or uses the decorated class as a metaclass. Attributes on the decorated class and its base classes are not considered to be fields.

@Morikko

This comment was marked as outdated.

@Morikko

This comment was marked as resolved.

filter_ = cls.as_context().get_global_filter()
# .values ordering is not guaranteed, at least not in
# Python < 3.6, when dicts where not ordered, which is an
# implementation detail anyway.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python < 3.6 is not supported anymore.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to remove the sort or just the comment ?

@davidhalter
Copy link
Owner

Thanks a lot for taking this on!

If there is any chance to merge it.

100%. In general my acceptance of changes mostly depends on quality and test coverage. This looks very good so far. Feel free to "finish" it as far as you can and then I'll review it. I think there might be a few minor changes or discussions, but they shouldn't cost you a lot of time. Also don't waste too much time in making Mypy happy. You can also skip some things with # type: ignore, if it's really not easy to type it. In general the Jedi code base is only typed in very little parts. Typing things is definitely a good thing, but some old code is very hard to type, because it works a lot with kind of obscure duck typing.

the implementation is more complex and I had to tweak klass.py. I would like to know if the design/implementation is going towards the right direction.

The complexity is unfortunately expected, with pretty much all typing changes. This change is on the very light side with only minor changes. My Mypy in Rust rewrite had to do way more to implement dataclass_transform (for doing proper type checking). Also note that this is a very low impact change, because it doesn't affect type inference and therefore won't affect performance.

Happy to review it thoroughly once you think this is ready. I would be happy to see at least some of the CI green. But I'm happy to help out if there are issues.

@Morikko
Copy link
Author

Morikko commented Mar 15, 2025

About attrs.

It uses dataclass_transform since 23.2.0 (2023-12-31) (ref). By dropping the stdlib hook to attrs, it would use this new dataclass_transform hook and simplify the code.

But users with an older attrs would lose completions if they would use a newer version of jedi. Sticking to an old jedi version would avoid that.

Should I drop attrs hooks or keep it ?

@Morikko

This comment was marked as resolved.

@Morikko Morikko marked this pull request as draft March 15, 2025 17:53
@davidhalter
Copy link
Owner

But users with an older attrs would lose completions if they would use a newer version of jedi. Sticking to an old jedi version would avoid that.
Should I drop attrs hooks or keep it ?

Good question. I think that the tests should probably still work. I don't care if an older attr version doesn't work anymore, as long as the most recent versions work.

@Morikko
Copy link
Author

Morikko commented Mar 24, 2025

Read the Docs build failed!
Missing Sphinx configuration key. The sphinx.configuration key is missing. This key is now required

Not sure what to do.

@PeterJCLaw
Copy link
Collaborator

Read the Docs build failed!
Missing Sphinx configuration key. The sphinx.configuration key is missing. This key is now required

Not sure what to do.

If you pull in the changes from master (either rebase or merge) this should be fixed.

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

Successfully merging this pull request may close these issues.

3 participants