Skip to content

Support fnmatch wildcards in __pdoc__ keys - #478

Open
ChrisJr404 wants to merge 1 commit into
pdoc3:masterfrom
ChrisJr404:wildcard-pdoc-keys
Open

ChrisJr404 wants to merge 1 commit into
pdoc3:masterfrom
ChrisJr404:wildcard-pdoc-keys

Conversation

@ChrisJr404

Copy link
Copy Markdown

This lets a __pdoc__ key contain a wildcard (*, ?, [) that is matched against member refnames with fnmatch, which addresses #449 — hiding members a base class contributes to many subclasses without listing each one.

Concretely, __pdoc__ = {'*.model_fields': False} now hides model_fields from every class in the module (the Pydantic case from the issue), and a truish value whitelists matching members the same way. As you noted there, the alternative was to complicate the end user's code with a comprehension over __subclasses__(), which only works after the subclasses are created; a wildcard key avoids that ordering trap.

The matching happens in _is_whitelisted()/_is_blacklisted(), so a class's own matching members are filtered out at construction just like an exact key. Members that a subclass only has through inheritance are dropped while inheritance is linked. Exact keys behave exactly as before, including still warning when they don't resolve; a wildcard key isn't expected to name one specific member, so it stays quiet instead of raising the spurious "key does not exist" warning from the issue.

Added tests covering inherited-member exclusion, a whole-class B.* pattern, wildcard whitelisting, and the no-warning behavior, plus a short note in the __pdoc__ docs. unittest, flake8, and mypy -p pdoc all pass.

A __pdoc__ key containing `*`, `?`, or `[` is now matched against member
refnames with fnmatch. This makes it possible to exclude (or whitelist)
members contributed by a base class from every subclass at once, e.g.
`__pdoc__ = {'*.model_fields': False}` to hide a Pydantic field from all
models in a module.

Wildcard keys are resolved in `_is_whitelisted()`/`_is_blacklisted()`,
and inherited members matching a falsey pattern are dropped during
inheritance linking. Since a pattern isn't expected to name one specific
member, a wildcard key no longer raises the "key does not exist" warning.

Fixes pdoc3#449

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant