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

Make it possible to ignore class members based on wildcard strings #449

Open
ml-kalju opened this issue Aug 1, 2024 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@ml-kalju
Copy link

ml-kalju commented Aug 1, 2024

I have a module that defines big nested data structure that is based on Pydantic. For the examples below, I use pydantic as an example even though this happens with any base-class. Minimal version is like this.

from pydantic import BaseModel

class ConfA(BaseModel):
    a_value: int

class ConfB(BaseModel):
    b_value: str

class Configuration(BaseModel):
    a: ConfA
    b: ConfB

I would want to use __pdoc__ variable to ignore the BaseModel's methods from the documentation, as they appear under each of these classes.

However, I don't want to disable inheritance altogether with the --config flag, as this is the only case where inherited members are not desired.

Expected Behavior

Something like the following removes the documentation for these inherited members from all the classes in the module where this is defined.

__pdoc__ = {
    '*.model_fields': False,
    '*.model_computed_fields': False,
    '*.model_config': False,
}

Actual Behavior

__pdoc__ = {
    '*.model_fields': False,
    '*.model_computed_fields': False,
    '*.model_config': False,
}

Produces the following:

UserWarning: __pdoc__-overriden key '*.model_fields' does not exist in module 'MY_MODULE'

And there is no change in documentation.

Steps to Reproduce

  1. Install pydantic pip install pydantic
  2. Create module as described in the example with the __pdoc__ parameter as defined in the expected behaviour
  3. pdoc3 --http MODULE
  4. BaseModel's members are shown under Configuration, ConfA and ConfB

Additional info

  • pdoc3==0.11.1
  • pydantic==2.6.1
  • pydantic_core==2.16.2
@kernc
Copy link
Member

kernc commented Aug 2, 2024

For resolving such edge-case problems, I'm leaning to complicating end-user rather than project code. Unless you want to contrib it?

ignore the BaseModel's methods from the documentation

You can achieve this with:

__pdoc__ = dict(
    (f'{cls.__name__}.{method}', False)
    for method in (BaseModel., 'model_computed_fields', 'model_config')
    for cls in (BaseModel, *BaseModel.__subclasses__())
)

or

__pdoc__ = dict(
    (f'{cls.__name__}.{method}', False)
    for method, _ in inspect.getmembers(BaseModel, inspect.isfunction)
    for cls in (BaseModel, *BaseModel.__subclasses__())
)

Note that BaseModel.__subclasses__() only returns expected result after the extending classes have been created.

@kernc kernc added the enhancement New feature or request label Aug 2, 2024
@kernc kernc changed the title Make it possible to ignore class members with the same name from all classes Make it possible to ignore class members based on wildcard strings Aug 2, 2024
@Aedial
Copy link

Aedial commented Oct 2, 2024

I've had a similar issue with PySide6 inserting a staticMetaObject into every widget class (in a private project).

It's easily solved by fnmatch'ing anything with a * in the _is_whitelisted()/_is_blacklisted() function. There will need to be an added check for the "key does not exist" warning, but I'm tempted to just skip the warning if the name has a wildcard (without doing an extensive check in the self._context.blacklisted).

So far, it seems to work quite fine, but I do not know if it won't break something somewhere else (beside having an overly greedy matching).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

3 participants