dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
155 stars 16 forks source link

search_mode not inherited by child models #51

Closed Kxnr closed 1 year ago

Kxnr commented 1 year ago

The search_mode attribute, set on a base class, isn't inherited by child models. This may apply to additional settings with default values (currently only ns_attrs).

Minimal Example

from pydantic.utils import to_lower_camel
from pydantic_xml import BaseXmlModel, element

class CustomBaseModel(BaseXmlModel, search_mode="unordered", nsmap={"": "somenamespace"}):
    """Base class to configure options"""

    class Config:
        alias_generator = to_lower_camel

class ChildModel(CustomBaseModel):
    """Root 'real' model describing a document"""

    some_field: str = element()

Expected Behavior

I expect that ChildModel.__xml_search_mode__ == CustomBaseModel.__xml_search_mode__ == "unordered"

Observed Behavior

ChildModel.__xml_search_mode__ is set to the default of "strict" unless seach_mode is also passed to ChildModel.

I believe this is due to the default value in __init__subclass__ for search_mode. The check search_mode if search_mode is not None always evaluates True and will never fall back to the else condition here.


New to Open Source contributions so, let me know if there's anything else you need. I'm happy to pull together a PR if this diagnosis seems right. Thanks!

dapper91 commented 1 year ago

@Kxnr Hi. Thank you for the report. That's a bug. I will fix it in the next update.

Kxnr commented 1 year ago

Thanks!

Bolik commented 1 year ago

Is it possible to specify search_mode in model config? Like this,

class BaseXmlModel(XmlModel):
    class Config:
        anystr_strip_whitespace = True
        underscore_attrs_are_private = True
        search_mode='unordered'
dapper91 commented 1 year ago

@Bolik Hi. No, it is not possible now.