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

fix: Don't turn items annotated as InitVar into dataclass members #252

Merged
merged 3 commits into from
Mar 12, 2024

Conversation

has2k1
Copy link
Contributor

@has2k1 has2k1 commented Mar 12, 2024

This PR addresses the case of dataclass init-only variables also being treated as class members. i.e

from dataclasses import dataclass, InitVar

@dataclass
class Point:
    x: float
    y: float
    z: InitVar[float]

is equivalent (compatible) to

class Point:
    def __init__(self, x: float, y: float, z: float):
       self.x = x
       self.y = y

The class is not expected to have member z (instance.z).

Copy link
Member

@pawamoy pawamoy left a comment

Choose a reason for hiding this comment

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

I really know nothing about dataclasses haha! Thanks 🙂

src/griffe/extensions/dataclasses.py Outdated Show resolved Hide resolved
@has2k1
Copy link
Contributor Author

has2k1 commented Mar 12, 2024

I really know nothing about dataclasses haha! Thanks 🙂

I have just accepted them as a Whac-A-Mole situation.

tests/test_dataclasses.py Outdated Show resolved Hide resolved
Copy link
Member

@pawamoy pawamoy left a comment

Choose a reason for hiding this comment

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

OK, LGTM, lets squash and merge 🙂

@pawamoy
Copy link
Member

pawamoy commented Mar 12, 2024

Ah wait, there are some linting warnings, let me fix those.

@pawamoy pawamoy merged commit 6835ea3 into mkdocstrings:main Mar 12, 2024
17 checks passed
has2k1 added a commit to has2k1/griffe that referenced this pull request Jun 10, 2024
This change gives the Parameter class the following attributes /
propeties:

   - docstring
   - lineno
   - endlineno
   - parent
   - lines
   - linescollections
   - source

Currenlty these values are only populated for dataclasses, and they are
None for regular classes and functions. This makes it possible to accesss
this information for all dataclass parameters, especially those set as
`InitVar` where it was not possible to get it since they are not members
(attributes) of the class.

Issue mkdocstrings#286: mkdocstrings#286
Related to mkdocstrings#252: mkdocstrings#252
pawamoy added a commit that referenced this pull request Jun 12, 2024
This change gives the Parameter class the `docstring` and `function` attributes. 

The docstring value is only populated for dataclasses.
This makes it possible to access this information for all dataclass parameters,
especially those set as `InitVar` where it was not possible to get it
since they are not members (attributes) of the class.

This slot can also be used to support things like PEP 727 (with extensions).
In the future, we might add such a docstring slot to yielded, received and returned values too.

Issue-286: #286
Related-to-mkdocstrings/griffe#252: #252
PR-288: #288
Co-authored-by: Timothée Mazzucotelli <dev@pawamoy.fr>
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.

None yet

2 participants