Skip to content

Commit

Permalink
fix: Don't process class aliases, as real classes are processed at so…
Browse files Browse the repository at this point in the history
…me point anyway

Processing the alias skips the real class later, which prevents the extension from seeing model fields.

Issue-8: #8
PR-7: #7
Co-authored-by: Timothée Mazzucotelli <[email protected]>
  • Loading branch information
SimonBoothroyd and pawamoy authored Nov 3, 2024
1 parent 3156504 commit 24a10f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/griffe_pydantic/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ def process_module(
processed.add(mod.canonical_path)

for cls in mod.classes.values():
process_class(cls, processed=processed, schema=schema)
# Don't process aliases, real classes will be processed at some point anyway.
if not cls.is_alias:
process_class(cls, processed=processed, schema=schema)

for submodule in mod.modules.values():
process_module(submodule, processed=processed, schema=schema)
14 changes: 14 additions & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ def test_extension() -> None:

schema = package.classes["ExampleModel"].extra["griffe_pydantic"]["schema"]
assert schema.startswith('{\n "description"')


def test_imported_models() -> None:
"""Test the extension with imported models."""
with temporary_visited_package(
"package",
modules={
"__init__.py": "from ._private import MyModel\n\n__all__ = ['MyModel']",
"_private.py": "from pydantic import BaseModel\n\nclass MyModel(BaseModel):\n field1: str\n '''Some field.'''\n",
},
extensions=Extensions(PydanticExtension(schema=True)),
) as package:
assert package["MyModel"].labels == {"pydantic-model"}
assert package["MyModel.field1"].labels == {"pydantic-field"}

0 comments on commit 24a10f7

Please sign in to comment.