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

feature: Warn about attributes sections found in __init__ method docstrings #144

Open
CunliangGeng opened this issue Mar 27, 2024 · 3 comments
Assignees
Labels
extractor: griffe Related to griffe feature New feature or request griffe extension Can be solved with a Griffe extension

Comments

@CunliangGeng
Copy link

Description of the bug

If attributes are documented in the __init__ method (see class below), their types are missing after rendering (see the screenshot).

class MolecularFamily:
    def __init__(self, family_id: str):
        """Class to model molecular family.

        Args:
            family_id: Unique id for the molecular family.

        Attributes:
            family_id: Unique id for the molecular family.
            spectra_ids: Set of spectrum ids in the molecular family.
        """
        self.family_id: str = family_id
        self.spectra_ids: set[str] = set()
        self._spectra: set[Spectrum] = set()
        self._strains: StrainCollection = StrainCollection()

screenshot
image

To Reproduce

WRITE MRE / INSTRUCTIONS HERE

Full traceback

Full traceback
PASTE TRACEBACK HERE

Expected behavior

Not matter where attributes are documented (module level, class level or __init__ method), their types should be extracted and rendered properly.

Environment information

python -m mkdocstrings_handlers.python.debug  # | xclip -selection clipboard
  • System: macOS-14.4.1-arm64-arm-64bit
  • Python: cpython 3.9.18
  • Environment variables:
  • Installed packages:
    • mkdocs v1.5.3
    • mkdocstrings v0.24.1
    • mkdocstrings-python v1.9.0
    • griffe v0.42.0

Additional context

mkdocs.yml

plugins
- mkdocstrings:
    handlers:
      python:
        options:
          members_order: source
          filters: ["!^_"]
          merge_init_into_class: true
          show_if_no_docstring: true
          show_root_heading: true
          show_root_full_path: false
          show_signature_annotations: true
          signature_crossrefs: true
          separate_signature: true
          show_symbol_type_heading: true
          show_symbol_type_toc: true
@pawamoy
Copy link
Member

pawamoy commented Mar 28, 2024

Hello, thanks for the report!

Attributes section only make sense in module and class docstrings. An __init__ method doesn't have any attributes, and Griffe doesn't look into the parent class to find their types. We could consider supporting that, but IMO we already have one obvious solution. Napoleon docs do not show examples of an Attributes section in an __init__ method docstring either, so I'm inclined to stick to the current behavior 🙂

class MolecularFamily:
    """Class to model molecular family.

    Attributes:
        family_id: Unique id for the molecular family.
        spectra_ids: Set of spectrum ids in the molecular family.
    """

    def __init__(self, family_id: str):
        """Initialize the molecular family.

        Args:
            family_id: Unique id for the molecular family.
        """
        self.family_id: str = family_id
        self.spectra_ids: set[str] = set()
        self._spectra: set[Spectrum] = set()
        self._strains: StrainCollection = StrainCollection()

I'll close this but feel free to comment further! Maybe I'm missing something here.

@pawamoy pawamoy closed this as completed Mar 28, 2024
@pawamoy
Copy link
Member

pawamoy commented Mar 28, 2024

Actually I'll reopen, as we could maybe emit a warning when we find an attributes section in an __init__ method docstring 🙂

@pawamoy pawamoy reopened this Mar 28, 2024
@pawamoy pawamoy changed the title bug: missing types for attributes documented in __init__ method feature: Warn about attributes sections found in __init__ method docstrings Mar 28, 2024
@CunliangGeng
Copy link
Author

Thanks for the explanation! It's nice to emit a warning.

@pawamoy pawamoy added feature New feature or request extractor: griffe Related to griffe griffe extension Can be solved with a Griffe extension labels Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extractor: griffe Related to griffe feature New feature or request griffe extension Can be solved with a Griffe extension
Projects
None yet
Development

No branches or pull requests

2 participants