Support fnmatch wildcards in __pdoc__ keys - #478
Open
ChrisJr404 wants to merge 1 commit into
Open
ChrisJr404 wants to merge 1 commit into
ChrisJr404 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 hidesmodel_fieldsfrom 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, andmypy -p pdocall pass.