A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pydantic/pydantic/issues/2411 below:

Nested Model Does Not Work in A Function Scope Or with `from __future__ import annotations` · Issue #2411 · pydantic/pydantic · GitHub

Checks Bug

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

       pydantic version: 1.7.3
      pydantic compiled: True
           install path: /home/cwu/.cache/pypoetry/virtualenvs/magic-GqpcoQ51-py3.8/lib/python3.8/site-packages/pydantic
         python version: 3.8.5 (default, Jul 28 2020, 12:59:40)  [GCC 9.3.0]
               platform: Linux-5.8.0-44-generic-x86_64-with-glibc2.29
optional deps. installed: ['typing-extensions', 'email-validator']

from __future__ import annotations

from pydantic import BaseModel, Field

def main():
  class Nested(BaseModel):
      name_nested: str = Field(alias='nameNested')

  class M(BaseModel):
      nested: Nested  # delay via 'Nested' does not help in any case

  m = M.parse_obj({'NamEStr': 'dd', 'nested': {'nameNested': ''}})
  print(m)

main()

This snippet outputs an error

pydantic.errors.ConfigError: field "nested" not yet prepared so type is still a ForwardRef, you might need to call M.update_forward_refs().

It is interesting that if I move the function body to the module scope, no error; If I remove the top line from __future__ annotations, no error.

I guess this is still a corner case of #1736 and #1668


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4