python config_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-02-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 65% fail

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