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

Question: How to use a reserved BaseModel attribute? #395

Closed
batisteo opened this issue Feb 13, 2019 · 7 comments
Closed

Question: How to use a reserved BaseModel attribute? #395

batisteo opened this issue Feb 13, 2019 · 7 comments
Labels

Comments

@batisteo
Copy link

batisteo commented Feb 13, 2019

Question

  • OS: GNU/Linux Fedora 29
  • Python version: 3.6.8
  • Pydantic version: 0.19

I’d like to migrate from attr.s to Pydantic, but I’ve got a couple of models that have the name schema as attribute. Ideally, I would like to not change other parts of the code base except from the model itself.

from pydantic import BaseModel


class Card(BaseModel):
    card_schema: tuple

    class Config:
        fields = {"card_schema": "schema"}


class Transaction(BaseModel):
    card: Card


card = Card(schema=("abc", "xyz"))

card.schema
# <bound method BaseModel.schema of <class '__main__.Card'>>

card.schema()
# {'title': 'Card', 'type': 'object', 'properties': {'schema': {'title': 'Schema', 'type': 'array'}}, 'required': ['schema']}

card.card_schema
# ('abc', 'xyz')


transaction = Transaction(card=card)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    transaction = Transaction(card=card)
  File "~/proj/.venv/lib64/python3.6/site-packages/pydantic/main.py", line 214, in __init__
    self.__setstate__(self._process_values(data))
  File "~/proj/.venv/lib64/python3.6/site-packages/pydantic/main.py", line 386, in _process_values
    return validate_model(self, input_data)  # type: ignore
  File "~/proj/.venv/lib64/python3.6/site-packages/pydantic/main.py", line 551, in validate_model
    raise ValidationError(errors)
pydantic.error_wrappers.ValidationError: 1 validation error
card -> schema
  field required (type=value_error.missing)

Having a look at #243 it seems that the following names for model attributes are not allowed:

Config
construct
copy
dict
fields
json
parse_file
parse_obj
parse_raw
schema
schema_json
to_string
update_forward_ref
validate

Would it be possible to consider renaming some of these methods with underscore, like _schema or __schema__?

Maybe something like what django-meta is doing.

I know it would be a breaking change, that’s what the 0.* is for, right? 😬

@samuelcolvin
Copy link
Member

Good news! This was fixed yesterday in release 0.20a1, please check that and let me know if you still have problems (you'll need to use field aliases, aka Config.fields).

I'll make that alpha release a proper release in a few days unless we find any problems.

@batisteo
Copy link
Author

It’s indeed fixing the ValidationError. Thanks!

@batisteo
Copy link
Author

Would it be possible to consider renaming some of these methods with underscore, like _schema or __schema__?

Or allowing shadowing the way does Django with defaulting Model.objects to Model._default_manager?

https://stackoverflow.com/questions/34427817/django-difference-between-base-manager-and-objects

@samuelcolvin
Copy link
Member

I see your point on renaming, perhaps we should have started out by naming all those methods _... eg. _dict, this is what named_tuple does for example.

However changing all those models now would break a lot of code, SemVar or not this would take me alone hours to fix across all the projects using pydantic. It would also upset mypy and IDEs when people access "private" properties.

I think the best solution at this stage is aliases.

@batisteo
Copy link
Author

I know 😔

@batisteo
Copy link
Author

It would also upset mypy and IDEs when people access "private" properties.

Do you get warnings from IDE when you access, for example, _make() or _asdict() of a NamedTuple?

@samuelcolvin
Copy link
Member

In pycharm yes.

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
Projects
None yet
Development

No branches or pull requests

2 participants