Skip to content

Can not define Final class var in BaseModel #2766

@zen-xu

Description

@zen-xu

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.8.1
            pydantic compiled: False
                 install path: /Users/zen-xu/.pyenv/versions/3.9.1/lib/python3.9/site-packages/pydantic
               python version: 3.9.1 (default, Mar  3 2021, 21:05:29)  [Clang 12.0.0 (clang-1200.0.32.29)]
                     platform: macOS-11.2.3-arm64-arm-64bit
     optional deps. installed: ['email-validator', 'typing-extensions']
from typing import Final, ClassVar
from pydantic import BaseModel

class Template(BaseModel):
    kind: Final[ClassVar[str]] = "xxx"
    content: str

Will raise TypeError: typing.ClassVar[str] is not valid as type argument

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions