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

Dynamic import broken for class and module attributes #307

Closed
has2k1 opened this issue Nov 24, 2023 · 4 comments
Closed

Dynamic import broken for class and module attributes #307

has2k1 opened this issue Nov 24, 2023 · 4 comments

Comments

@has2k1
Copy link
Contributor

has2k1 commented Nov 24, 2023

With dynamic=True

  1. class attributes get the docstrings of their parent object
  2. module attributes cannot be imported

This is a regression introduced in cd7fd5a.

Here is how to reproduce it.

from pathlib import Path
from quartodoc import get_object

text = '''
class AwesomeClass:
    """
    This is an awesome class
    """
    
    a: int
    b: dict
    """b is great"""

c: int = 1
d: dict = {}
"""d is great"""
'''

filepath = Path("tmp_test_file.py")
filepath.write_text(text)

obj_a = get_object("tmp_test_file:AwesomeClass.a", dynamic=True)
obj_b = get_object("tmp_test_file:AwesomeClass.b", dynamic=True)
# obj_c = get_object("tmp_test_file:c", dynamic=True)  # AttributeError
# obj_d = get_object("tmp_test_file:d", dynamic=True)  # AttributeError

print(obj_a.docstring and obj_a.docstring.value)
print(obj_b.docstring.value)
# print(obj_c.docstring and obj_a.docstring.value)
# print(obj_d.docstring.value)
This is an awesome class
This is an awesome class

The output should be the same when dynamic=False

obj_a = get_object("tmp_test_file:AwesomeClass.a", dynamic=False)
obj_b = get_object("tmp_test_file:AwesomeClass.b", dynamic=False)
obj_c = get_object("tmp_test_file:c", dynamic=False)
obj_d = get_object("tmp_test_file:d", dynamic=False)

print(obj_a.docstring and obj_a.docstring.value)
print(obj_b.docstring.value)
print(obj_c.docstring and obj_a.docstring.value)
print(obj_d.docstring.value)
None
b is great
None
d is great
@machow
Copy link
Owner

machow commented Nov 27, 2023

Shoot, thanks for the example--I'm working on this now, and will add these to the test suite...

@machow
Copy link
Owner

machow commented Nov 27, 2023

From testing with quarto v0.6.6, things seem like this:

  • regression: module attributes cannot be imported (or docstring comes from containing class; e.g. a member gets the class constructor docstring).
    • (note also encountered an error importing value-less class attributes, so this is a big regression).
  • same behavior: class attributes get the docstrings of their parent object.
    • note that this only applies to class attributes with values (i.e. not purely an annotation)
    • the reason is that, when loading dynamically, quartodoc can't tell whether you meant to get the value's docstring or not.
class AwesomeClass:
    """
    This is an awesome class
    """
    
    # REGRESSION: couldn't dynamically import a or b ----
    a: int
    b: dict
    """b is great"""

# UNCHANGED BEHAVIOR: docstring for `1` or `{}` being used ----
# quartodoc can't tell whether you mean to use the dynamically imported
# objects docstring or not (e.g. if it were a function instead of `1`, people
# often want to use the function's docstring).
#
# For cases like d, we could also try to *statically* load the docstring ("d is great"),
# and if that works, then use that one?
c: int = 1
d: dict = {}
"""d is great"""

(I tested pretty quickly, so might have messed something up, but am digging it more!)

@machow
Copy link
Owner

machow commented Nov 27, 2023

I've added a fix and tests for the regression in #309 -- do you mind checking to see if it works for you?!

@machow
Copy link
Owner

machow commented Dec 12, 2023

Should be resolved by #309 , definitely let me know if you run into issues -- happy to relook

@machow machow closed this as completed Dec 12, 2023
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

No branches or pull requests

2 participants