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

Support introspecting Pydantic model attributes #221

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ruff = "^0.5.2"
pytest = "^7.4.2"
pytest-cov = "^4.1.0"
pytest-mock = "^3.11.1"
pydantic = "^2.9"

[tool.coverage.paths]
source = ["src"]
Expand Down
4 changes: 3 additions & 1 deletion src/pykka/_introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def introspect_attrs(

attr = get_attr_from_parent(root, attr_path)

if attr == proxy:
# Keep `proxy` first to use it's `__eq__` method instead of `attr`'s
# unknown implementation.
if proxy == attr:
logger.warning(
f"{root} attribute {'.'.join(attr_path)!r} "
f"is a proxy to itself. "
Expand Down
9 changes: 9 additions & 0 deletions tests/proxy/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING

import pydantic
import pytest

import pykka
Expand All @@ -18,10 +19,17 @@ class NestedObject:
pass


class PydanticModel(pydantic.BaseModel):
foo: str = "bar"


class ActorForProxying(Actor):
a_nested_object = pykka.traversable(NestedObject())
a_class_attr = "class_attr"

# Include a pydantic model to test that it doesn't break introspection.
a_pydantic_model = PydanticModel()

def __init__(self) -> None:
super().__init__()
self.an_instance_attr = "an_instance_attr"
Expand Down Expand Up @@ -108,6 +116,7 @@ def test_dir_on_proxy_lists_attributes_of_the_actor(
assert "a_class_attr" in result
assert "an_instance_attr" in result
assert "a_method" in result
assert "a_pydantic_model" in result


def test_dir_on_proxy_lists_private_attributes_of_the_proxy(
Expand Down