8000 Support dataclass transform by Morikko · Pull Request #2049 · davidhalter/jedi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support dataclass transform #2049

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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

Morikko
Copy link
@Morikko Morikko commented Feb 15, 2025
8000

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

  • Final attributes (already supported, just tested)
  • Support ClassVar (excluded from init)
  • dataclass_transform base class
  • dataclass_transform decorator
  • dataclass_transform metaclass
  • init=False. The dataclass_transform decorator, metaclass or base class are always assumed to default to init=True. However if it is setting init=False, the parameters are by default ignored. If the defined dataclass-semantics is overriding the init, it takes precedence.

Not covered:

  • (dataclasses.Field, dataclasses.field) init=False / dataclass_transform field_specifiers init=False. Those fields are always present. It may also show the wrong type/default value but as before this MR.
  • Base Model with custom default is not feasible as __init_subclass__ signature is hardcoded in the jedi third_party typeshed. So init can not be introspected.

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.

Morikko added 3 commits May 5, 2025 00:37
* origin:
  Don't remove `sys.path[0]`.
  perf: improve performance by replacing list to set
  Explicit sphinx config path
@Morikko
Copy link
Author
Morikko commented May 5, 2025

Cancelled job reason:

This is a scheduled Ubuntu 20.04 retirement. Ubuntu 20.04 LTS runner will be removed on 2025-04-15. For more details, see https://github.com/actions/runner-images/issues/11101

@Morikko Morikko marked this pull request as ready for review May 5, 2025 00:21
@Morikko
Copy link
Author
Morikko commented May 5, 2025

On my side, the code is ready and I do not plan to add any functionality as part of this MR. I will also do some integration tests in the coming days.

This was referenced May 5, 2025
@glyph
Copy link
glyph commented May 5, 2025

Is there a simple guide for setting up a cutout from my Emacs jedi installation to point at this branch?

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.

4 participants
0