Skip to content

Exception using dataclasses with v1.9 #637

Closed

Description

@EthanAtLL

Running uv run doctest.py on

# /// script
# dependencies = [
#   "numpydoc==1.8.0",
# ]
# ///
from dataclasses import dataclass
from numpydoc.validate import validate, get_doc_object

@dataclass
class MyClass:
    """Docs.
    """
    x: int

if __name__=='__main__':
    doc_obj = get_doc_object(MyClass)
    x = get_doc_object(MyClass.__init__)
    print(validate(x))

works yielding {'type': 'function', 'docstring': '', 'deprecated': False, 'file': '<string>', 'file_line': None, 'errors': [('GL08', 'The object does not have a docstring')], 'examples_errors': ''} but changing to numpydoc==1.90 gives

Traceback (most recent call last):
  File ".../doctest.py", line 18, in <module>
    print(validate(x))
  File ".../uv/environments-v2/doctest-76a1dd0b6c7db47d/lib/python3.9/site-packages/numpydoc/validate.py", line 648, in validate
    cls = Validator._load_obj(f"{doc.code_obj.__module__}.{cls_name}")
  File ".../uv/environments-v2/doctest-76a1dd0b6c7db47d/lib/python3.9/site-packages/numpydoc/validate.py", line 268, in _load_obj
    obj = getattr(obj, part)
AttributeError: module '__main__' has no attribute '__create_fn__'

The error only arises with the @dataclass annotation.

Activity

  1. mattgebert commented on Jul 22, 2025

    @mattgebert
    Contributor

    @DerWeh it seems your bug is the same as #638, but distinct from this one.

    @EthanAtLL I've gone through the code the this issue arises from the use of the code_obj to resolve the parent class (via __qualname__ attribute).

    __qualname__ was introduced in Python 3.3. In the dataclasses.py module, the processing path for adding the __qualname__ for __init__ is

    dataclasses.py > _process_class > _set_new_attribute(cls, '__init__', init_fn( ... ) )
    

    Here the _set_new_attribute is responsible for binding the __init__ function to the new class.

    It looks like in Python 3.9, there is no support in the dataclasses [Py3.9] library for setting __qualname__ for the created instances/functions by _set_new_attribute - the only reference to __qualname__ is in creating a __repr__ function.

    In Python 3.10, many __qualname__ modificaitons are introduced to dataclasses [Py3.10], including all uses of the _set_new_attribute method.

    @rossbar I'm not familiar enough with this sort of bug to know if we ought to introduce a python version specific fix. Python 3.9 end of life is Oct 2025 as well, if relevant? This issue is very much localized to dataclasses.

  2. larsoner commented on Jul 22, 2025

    @larsoner
    Collaborator

    My vote would be to require Python 3.10+ for numpydoc. It's maybe a few months earlier than we would have preferred to make the jump but seems reasonable. People on 3.9 and older can use older versions if needed

  3. EthanAtLL commented on Jul 22, 2025

    @EthanAtLL
    Author

    @mattgebert Thank you for looking into this! @larsoner With 3.9 EOLing soon, requiring 3.10+ makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions