Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
uriyyo opened this issue Nov 10, 2021 · 3 comments · Fixed by #3403
Closed
3 tasks done

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

uriyyo opened this issue Nov 10, 2021 · 3 comments · Fixed by #3403
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@uriyyo
Copy link
Contributor

uriyyo commented Nov 10, 2021

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
@uriyyo uriyyo added the bug V1 Bug related to Pydantic V1.X label Nov 10, 2021
@layday
Copy link
Contributor

layday commented Nov 10, 2021

For some additional context, this happens with Python 3.9.8 and 3.10.1, released 5 Nov. ClassVars are unusable under these two versions.

rra added a commit to lsst/daf_butler that referenced this issue Nov 11, 2021
As of Python 3.9.8, this does not work with Pydantic BaseModel, and
does not work in a way that causes the whole module to fail to
import.  See pydantic/pydantic#3401.

In general, Pydantic does not work well with string types as enabled
by from __future__ import annotations.
rra added a commit to lsst/daf_butler that referenced this issue Nov 11, 2021
As of Python 3.9.8, this does not work with Pydantic BaseModel, and
does not work in a way that causes the whole module to fail to
import.  See pydantic/pydantic#3401.

In general, Pydantic does not work well with string types as enabled
by from __future__ import annotations, which is why making that
feature the default has been delayed.  See
https://lwn.net/Articles/858576/ for more information.
rra added a commit to lsst/daf_butler that referenced this issue Nov 11, 2021
As of Python 3.9.8, this does not work with Pydantic BaseModel, and
does not work in a way that causes the whole module to fail to
import.  See pydantic/pydantic#3401.

In general, Pydantic does not work well with string types as enabled
by from __future__ import annotations, which is why making that
feature the default has been delayed.  See
https://lwn.net/Articles/858576/ for more information.

Fix this by removing from __future__ import annotations from the
files using ClassVar and explicitly quote the type arguments that
contain forward references (all of which are outside the scope of
what Pydantic cares about).
@hellocoldworld
Copy link

Also running into this.

@levrik
Copy link

levrik commented Nov 15, 2021

Also running into this. It's quite severe as base Docker images automatically updated to Python 3.9.8 and there's no easy way to go back and we basically can't build any new releases.

JacobHayes added a commit to artigraph/artigraph that referenced this issue Nov 16, 2021
JacobHayes added a commit to artigraph/artigraph that referenced this issue Nov 16, 2021
JacobHayes added a commit to artigraph/artigraph that referenced this issue Nov 16, 2021
JacobHayes added a commit to artigraph/artigraph that referenced this issue Nov 17, 2021
andyshinn added a commit to andyshinn/wbld that referenced this issue Apr 2, 2022
andyshinn added a commit to andyshinn/wbld that referenced this issue Apr 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants