andreahlert commented on code in PR #668:
URL: https://github.com/apache/burr/pull/668#discussion_r2955143893


##########
burr/integrations/pydantic.py:
##########
@@ -51,8 +51,9 @@
 
 def model_to_dict(model: pydantic.BaseModel, include: Optional[List[str]] = 
None) -> dict:
     """Utility function to convert a pydantic model to a dictionary."""
-    keys = model.model_fields.keys()

Review Comment:
   This guard is unnecessary. `model_to_dict` has the signature `model: 
pydantic.BaseModel` - it always receives an **instance**, never a class. No 
call site in the entire codebase passes a class here.
   
   Simpler and equivalent:
   ```python
   keys = type(model).model_fields.keys()
   ```
   The `isinstance(model, type)` branch is dead code.



##########
burr/integrations/pydantic.py:
##########
@@ -76,7 +77,8 @@ def subset_model(
     """
     new_fields = {}
 

Review Comment:
   Same pattern, same problem. `model_from_state` takes `model: 
Type[ModelType]` - always a class. The original code 
`model.model_fields.keys()` was already correct here. No deprecation warning is 
emitted when accessing `model_fields` on a class.
   
   This change adds unnecessary indirection without fixing anything.



##########
burr/integrations/pydantic.py:
##########
@@ -51,8 +51,9 @@
 
 def model_to_dict(model: pydantic.BaseModel, include: Optional[List[str]] = 
None) -> dict:
     """Utility function to convert a pydantic model to a dictionary."""
-    keys = model.model_fields.keys()
-    keys = keys if include is None else [item for item in include if item in 
model.model_fields]
+    model_cls = model if isinstance(model, type) else type(model) # Handles 
the possibility that sometimes model is a class not instance
+    keys = model_cls.model_fields.keys()
+    keys = keys if include is None else [item for item in include if item in 
model_cls.model_fields]
     return {key: getattr(model, key) for key in keys}
 

Review Comment:
   Same issue. `subset_model` has the signature `model: Type[ModelType]` - it 
**always** receives a class, never an instance. The `isinstance(model, type)` 
guard will always be `True`, making `type(model)` never execute.
   
   This is dead code that adds confusion. The original `model.model_fields` on 
a class was already correct and never triggered the deprecation warning - 
there's nothing to fix here.
   
   Verified locally:
   ```python
   >>> TestModel.model_fields  # class access, no warning
   >>> type(TestModel).model_fields  # goes to ModelMetaclass, AttributeError!
   ```
   
   The guard accidentally prevents the `type()` path from executing, which 
would actually **crash** since `type(SomeBaseModel)` is `ModelMetaclass` and 
has no `model_fields`.



##########
pyproject.toml:
##########
@@ -89,7 +89,7 @@ tests = [
   "langchain_core",
   "langchain_community",
   "pandas",
-  "pydantic[email]",
+  "pydantic[email]>=2.11",

Review Comment:
   This version bump is a **breaking change** that isn't justified by the code 
fix.
   
   `type(model).model_fields` works on all pydantic 2.x versions - it's not a 
new API. The deprecation warning only appears in >=2.11 when accessing 
`model_fields` on instances, and the fix removes that access pattern entirely.
   
   Bumping the minimum to `>=2.11` breaks users on pydantic 2.0-2.10 who 
currently work fine and would continue to work fine after this fix.
   
   If a version pin is desired (as @skrawcz mentioned), it should be the 
**minimum already supported** pydantic 2.x version, not 2.11. Or it should be 
handled separately in PR #654 with proper justification.



##########
tests/integrations/test_burr_pydantic.py:
##########
@@ -133,6 +134,12 @@ class MyModelWithConfig(pydantic.BaseModel):
     assert SubsetModel.__name__ == "MyModelWithConfigSubset"

Review Comment:
   This test checks the installed pydantic version, not behavior. It will fail 
on any CI or user environment running pydantic 2.0-2.10, which currently work 
perfectly.
   
   A better approach: test that `model_to_dict` does **not** emit 
`DeprecationWarning`:
   ```python
   import warnings
   
   def test_model_to_dict_no_deprecation_warning():
       model = OriginalModel(foo=1, bar="bar", 
nested=NestedModel(nested_field1=1))
       with warnings.catch_warnings():
           warnings.simplefilter("error")
           result = model_to_dict(model)
       assert "foo" in result
   ```
   This tests actual behavior regardless of pydantic version.



##########
tests/integrations/test_burr_pydantic.py:
##########
@@ -22,6 +22,7 @@
 import pytest
 from pydantic import BaseModel, ConfigDict, EmailStr, Field
 from pydantic.fields import FieldInfo

Review Comment:
   `import warnings` is added but never used in any test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to