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

Default None for uuid property raises AttributeError (name collision) #6646

Closed
1 task done
hofrob opened this issue Jul 13, 2023 · 2 comments
Closed
1 task done

Default None for uuid property raises AttributeError (name collision) #6646

hofrob opened this issue Jul 13, 2023 · 2 comments
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@hofrob
Copy link

hofrob commented Jul 13, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Declaring a pydantic model with a property named uuid and type uuid.UUID | None and a default value of None raises AttributeError: 'NoneType' object has no attribute 'UUID'.

This is probably true for other import/property name collisions as well.

Work arounds:

  • rename the property
  • remove the default value
  • change the import to from uuid import UUID

Example Code

import uuid

import pydantic


class Foo(pydantic.BaseModel):
    uuid: uuid.UUID | None = None

Python, Pydantic & OS Version

pydantic version: 2.0.2
        pydantic-core version: 2.1.2 release build profile
                 install path: /project/path/venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.4 (main, Jul 11 2023, 22:38:37) [GCC 11.3.0]
                     platform: Linux-5.19.0-46-generic-x86_64-with-glibc2.35
     optional deps. installed: ['typing-extensions']

Selected Assignee: @adriangb

@hofrob hofrob added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Jul 13, 2023
@Viicos
Copy link
Contributor

Viicos commented Jul 13, 2023

This isn't related to Pydantic, it has to do with how Python finds references to the type hints you are using. Foo has its own class scope, and when you are using the type hint uuid.UUID, it tries to resolve uuid in this class scope, which is set to None with the default value.

Either use from uuid import UUID or uuid: "uuid.UUID | None" = None

@samuelcolvin
Copy link
Member

This isn't related to Pydantic, it has to do with how Python finds references to the type hints you are using. Foo has its own class scope, and when you are using the type hint uuid.UUID, it tries to resolve uuid in this class scope, which is set to None with the default value.

Either use from uuid import UUID or uuid: "uuid.UUID | None" = None

Exactly 👍 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

No branches or pull requests

4 participants