8000 Attribute cannot have the same name as its type · Issue #117 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Attribute cannot have the same name as its type #117

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

Closed
catethos opened this issue Jan 13, 2018 · 2 comments
Closed

Attribute cannot have the same name as its type #117

catethos opened this issue Jan 13, 2018 · 2 comments

Comments

@catethos
Copy link

I am trying to make the class attribute having the same name as its type. The following code does not work and show the ConfigError

class A(BaseModel):
    pass

class B(BaseModel):
    A: A = None
ConfigError: unable to infer type for attribute "A"

However, if I make the attribute no default value then it works

class A(BaseModel):
    pass

class B(BaseModel):
    A: A 

Is this behavior intended?

@samuelcolvin
Copy link
Member
samuelcolvin commented Jan 13, 2018

This is a consequence of how python annotations work and not specific to pydantic:

In [1]: class Foo:
   ...:     pass
   ...: 

In [2]: class Bar:
   ...:     Foo: Foo = None
   ...:     spam: str = None
   ...:     

In [3]: Bar.__annotations__
Out[3]: {'Foo': None, 'spam': str}

I've no idea if this is intentional behaviour or a bug in the standard library but there's nothing pydantic can do about it since it's just consulting the __annotations__ dict and finding None.

@hb2638
Copy link
hb2638 commented Jan 26, 2023

This is related to python/mypy#1775
I ended up using an alias

so using your example

before

class A(BaseModel):
    pass

class B(BaseModel):
    A: A = None

after

class A(BaseModel):
    pass

_A = A

class B(BaseModel):
    A: _A = None

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
* check items() exists before calling it a mapping

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

No branches or pull requests

3 participants
0