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

Weird bug that my english is not enough to describe this. #8777

Closed
1 task done
caner-cetin opened this issue Feb 10, 2024 · 6 comments
Closed
1 task done

Weird bug that my english is not enough to describe this. #8777

caner-cetin opened this issue Feb 10, 2024 · 6 comments

Comments

@caner-cetin
Copy link

caner-cetin commented Feb 10, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

so please fix the title after you got what I am saying

Example Code

from pydantic import BaseModel as Base

class Stash(Base):
    Name: str
    X: int
    Y: int

class Listing(Base):
    Method: str
    Indexed: datetime
    Stash: Stash
    Account: Account
    Price: Price

okay you see that stash? It is all okay. In the current state. Pyright doesnt bitch about anything, I can actually use this class.

lets dive into my mind for the past 1 hour

oh an error

Result.7.Listing.Stash
  Field required [type=missing, input_value={'Method': 'forum', 'Inde..., 'Currency': 'mirror'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.6/v/missing

I need to make this field optional. Alright, easy. Lets add Optional tag here...

Result.5.Listing.Stash
  Field required [type=missing, input_value={'Method': 'forum', 'Inde..., 'Currency': 'mirror'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.6/v/missing

grrrr.... Lets add Field(default=None)

class Listing(Base):
    Method: str
    Indexed: datetime
    Stash: Optional[Stash] = Field(default=None)
    Account: Account
    Price: Price

oh shit what the fuck

Type of "Stash" could not be determined because it refers to itself PylancereportGeneralTypeIssues
Variable not allowed in type expression PylancereportInvalidTypeForm
(variable) Stash: Unknown

uhhhh is the code running is it false warning

  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 82, in __repr__
    return f'{self.__repr_name__()}({self.__repr_str__(", ")})'
                                     ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 55, in __repr_str__
    return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 55, in <genexpr>
    return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__())
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/fields.py", line 559, in __repr_args__
    yield 'annotation', _repr.PlainRepr(_repr.display_as_type(self.annotation))
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 103, in display_as_type
    args = ', '.join(map(display_as_type, typing_extensions.get_args(obj)))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 95, in display_as_type
    return repr(obj)
           ^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 82, in __repr__
    return f'{self.__repr_name__()}({self.__repr_str__(", ")})'
                                     ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 55, in __repr_str__
    return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 55, in <genexpr>
    return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__())
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/fields.py", line 559, in __repr_args__
    yield 'annotation', _repr.PlainRepr(_repr.display_as_type(self.annotation))
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 103, in display_as_type
    args = ', '.join(map(display_as_type, typing_extensions.get_args(obj)))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic/_internal/_repr.py", line 95, in display_as_type
    return repr(obj)
           ^^^^^^^^^
RecursionError: maximum recursion depth exceeded while getting the repr of an object

AAAAAAAAA

Python, Pydantic & OS Version

pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=true
                 install path: /Users/canercetin/Library/Caches/pypoetry/virtualenvs/zurna-backend-VoMrHkaz-py3.11/lib/python3.11/site-packages/pydantic
               python version: 3.11.7 (main, Dec  4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)]
                     platform: macOS-14.2.1-arm64-arm-64bit
             related packages: typing_extensions-4.9.0 fastapi-0.109.1
                       commit: unknown
@caner-cetin caner-cetin added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 10, 2024
@caner-cetin
Copy link
Author

StashRepr: Optional[Stash] = Field(default=None, alias="Stash")

I know that I can just fix this by adding alias but come on. Writing Stash everywhere would be hella cool to do.

@sydney-runkle
Copy link
Member

@caner-cetin,

Right - we implemented this warning here: #8243, so this behavior is expected and intentional.

A field name can't be the same as the type used for the field. Using an alias here is a good workaround, if you really need the field to be named Stash!

@sydney-runkle sydney-runkle added question duplicate and removed bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 12, 2024
@sydney-runkle
Copy link
Member

Duplicate of #8240

@sydney-runkle sydney-runkle marked this as a duplicate of #8240 Feb 12, 2024
@caner-cetin
Copy link
Author

but come oooon, think how cool it would be to do without alias @sydney-runkle

@sydney-runkle
Copy link
Member

@caner-cetin,

See an in depth explanation of this issue here: #7327

@caner-cetin
Copy link
Author

everyone is against the cool things :(

jokes aside, thanks for reply and references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants