Skip to content

Pydantic fail to parse ClassVar annotation when it's a string #3401

@uriyyo

Description

@uriyyo

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.2
            pydantic compiled: False
                 install path: /Users/yuriikarabas/my-projects/pydantic/pydantic
               python version: 3.9.8 (tags/v3.9.8:bb3fdcfe95, Nov 10 2021, 14:54:52)  [Clang 13.0.0 (clang-1300.0.29.3)]
                     platform: macOS-12.0.1-x86_64-i386-64bit
     optional deps. installed: ['devtools', 'dotenv', 'email-validator', 'typing-extensions']
from typing import ClassVar

from pydantic import BaseModel


class Model(BaseModel):
    val: "ClassVar[int]" = 10
Traceback (most recent call last):
  File "/Users/yuriikarabas/my-projects/pydantic/temp.py", line 6, in <module>
    class Model(BaseModel):
  File "/Users/yuriikarabas/my-projects/pydantic/pydantic/main.py", line 187, in __new__
    annotations = resolve_annotations(namespace.get('__annotations__', {}), namespace.get('__module__', None))
  File "/Users/yuriikarabas/my-projects/pydantic/pydantic/typing.py", line 340, in resolve_annotations
    value = _eval_type(value, base_globals, None)
  File "/Users/yuriikarabas/my-projects/cpython/Lib/typing.py", line 292, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/Users/yuriikarabas/my-projects/cpython/Lib/typing.py", line 553, in _evaluate
    type_ = _type_check(
  File "/Users/yuriikarabas/my-projects/cpython/Lib/typing.py", line 158, in _type_check
    raise TypeError(f"{arg} is not valid as type argument")
TypeError: typing.ClassVar[int] 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