python config_error ai_generated true

AttributeError: 类型对象 'User' 没有属性 'Config'。您是想找 'model_config' 吗?

AttributeError: type object 'User' has no attribute 'Config'. Did you mean: 'model_config'?

ID: python/pydantic-model-config-attribute-error

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-02-18首次发现

版本兼容性

版本状态引入弃用备注
2.x active — — —

根因分析

将 Pydantic v1 中使用内部 class Config 的代码复制到了 v2 项目中。v2 中配置通过 model_config 类属性和 ConfigDict 设置。

English

Pydantic v1 code using an inner class Config was copied into a v2 project. In v2 the configuration is set via the model_config class attribute with ConfigDict.

generic

解决方案

  1. 95% 成功率
    from pydantic import BaseModel, ConfigDict
    
    class User(BaseModel):
        model_config = ConfigDict(extra='forbid', frozen=True)
        name: str
  2. 85% 成功率
    pip install bump-pydantic
    bump-pydantic ./src

无效尝试

常见但无效的做法:

  1. 80% 失败

    Pydantic v2 ignores the inner Config class entirely; settings silently have no effect and behavior diverges.

  2. 65% 失败

    Breaks other v2-only dependencies and pydantic-core bindings; causes cascading import errors across the project.