Skip to content

Dataclasses and schema attribute #408

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
blfpd opened this issue Feb 26, 2019 · 1 comment
Closed

Dataclasses and schema attribute #408

blfpd opened this issue Feb 26, 2019 · 1 comment
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@blfpd
Copy link

blfpd commented Feb 26, 2019

When creating a model with the dataclasses backport, the model doesn’t have the schema() method, but it’s still forbidden to create a model with this attribute name. Is this a desired behaviour?

>>> from pydantic.dataclasses import dataclass
>>> 
>>> @dataclass
... class Card:
...     number: str
... 
>>> card = Card(number="1234")
>>> card.schema()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    card.schema()
AttributeError: 'Card' object has no attribute 'schema'
>>> card.__pydantic_model__.schema()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    card.__pydantic_model__.schema()
  File "~/env/lib/python3.6/site-packages/pydantic/main.py", line 402, in schema
    s = model_schema(cls, by_alias=by_alias)
  File "~/env/lib/python3.6/site-packages/pydantic/schema.py", line 204, in model_schema
    model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
  File "~/env/lib/python3.6/site-packages/pydantic/schema.py", line 488, in model_process_schema
    model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
  File "~/env/lib/python3.6/site-packages/pydantic/schema.py", line 514, in model_type_schema
    f, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
  File "~/env/lib/python3.6/site-packages/pydantic/schema.py", line 243, in field_schema
    s['default'] = encode_default(field.default)
  File "~/env/lib/python3.6/site-packages/pydantic/schema.py", line 693, in encode_default
    return pydantic_encoder(dft)
  File "~/env/lib/python3.6/site-packages/pydantic/json.py", line 40, in pydantic_encoder
    raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
TypeError: Object of type '_MISSING_TYPE' is not JSON serializable
>>> 
>>> @dataclass
... class Card:
...     schema: str
... 
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    @dataclass
  File "~/env/lib/python3.6/site-packages/pydantic/dataclasses.py", line 118, in dataclass
    return wrap(_cls)
  File "~/env/lib/python3.6/site-packages/pydantic/dataclasses.py", line 113, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, config)
  File "~/env/lib/python3.6/site-packages/pydantic/dataclasses.py", line 77, in _process_class
    cls.__pydantic_model__ = create_model(cls.__name__, __config__=config, __module__=_cls.__module__, **fields)
  File "~/env/lib/python3.6/site-packages/pydantic/main.py", line 562, in create_model
    return type(model_name, (__base__,), namespace)  # type: ignore
  File "~/env/lib/python3.6/site-packages/pydantic/main.py", line 184, in __new__
    validate_field_name(bases, var_name)
  File "~/env/lib/python3.6/site-packages/pydantic/utils.py", line 194, in validate_field_name
    f'Field name "{field_name}" shadows a BaseModel attribute; '
NameError: Field name "schema" shadows a BaseModel attribute; use a different field name with "alias='schema'".
>>> 
@samuelcolvin
Copy link
Member

card.__pydantic_model__.schema() should work, I'm not sure why it doesn't. PR welcome to fix that.

Regarding Field name "schema" shadows a BaseModel attribute; use a different field name with "alias='schema' that's harder to fix since the underlying model __pydantic_model__ is a normal pydantic model, so won't play nicely with schema as a field. Maybe we should change the error message, to make more sense in this context.

As mentioned before, perhaps all those methods should have had other names, but it's too late now.

@samuelcolvin samuelcolvin added the bug V1 Bug related to Pydantic V1.X label Feb 26, 2019
@pilosus pilosus mentioned this issue Mar 28, 2019
4 tasks
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
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

No branches or pull requests

2 participants