koxudaxi / pydantic-pycharm-plugin

PyCharm plugin for pydantic. This plugin provides autocompletion, inspection, type-checking, inserting unfilled argument, and more.
https://koxudaxi.github.io/pydantic-pycharm-plugin/
MIT License
451 stars 15 forks source link

Pydantic v2: Unexpected argument warning for `model_*` attributes #788

Open Jasonnor opened 1 year ago

Jasonnor commented 1 year ago

Describe the bug

I get unexpected argument warning for model_* attributes, and attribute names not start with model_ correctly.

I set protected_namespaces to empty to avoid Pydantic v2 warning.

But it seems that pydantic-pycharm-plugin will automatically filter out the attributes starting with model_*, in order to avoid display the original members of BaseModel. Do not know any better solution? If there is no other way, I can only consider renaming the attribute.

Thanks!

To Reproduce

Here is an example:

class ModelDetail(BaseModel):
    id: str = Field(
        ...,
        description='Model unique id',
        example='627490893523cd3e2617819e',
    )
    model_type: str = Field(
        ...,
        description='Model type',
        example='detector',
    )
    model_ver: str = Field(
        ...,
        description='Model version',
        example='1.0.0',
    )
    model_config = ConfigDict(
        protected_namespaces=()
    )

# Here PyCharm shows unexpected argument warning
ModelDetail(
    id='1',
    model_type='detector',
    model_ver='1.0.0',
)

Expected behavior Without warning.

Screenshots image

Environments (please complete the following information):

Additional context Add any other context about the problem here.

Funding

Fund with Polar

davidgilbertson commented 3 weeks ago

I'm finding the same thing, I can't believe this was already logged! I'm getting this using the LangChain package which deals with LLMs, so has lots of model_* arguments.

You could maybe just exclude from this list explicitly.

>>> [x for x in dir(BaseModel) if x.startswith('model_')]
['model_computed_fields',
 'model_config',
 'model_construct',
 'model_copy',
 'model_dump',
 'model_dump_json',
 'model_extra',
 'model_fields',
 'model_fields_set',
 'model_json_schema',
 'model_parametrized_name',
 'model_post_init',
 'model_rebuild',
 'model_validate',
 'model_validate_json',
 'model_validate_strings']

But I don't fully understand why one would focus just on the model_* stuff, shouldn't the same logic be applied to anything returned from dir(BaseModel)?

(I never realised Pydantic had such a polluted namespace before looking into this!)