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

The underscore_attrs_are_private config flag turns __doc__ attribute into a ModelPrivateAttr #2090

Closed
3 tasks done
jstavrak opened this issue Nov 4, 2020 · 2 comments · Fixed by #2091
Closed
3 tasks done
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@jstavrak
Copy link

jstavrak commented Nov 4, 2020

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.7.2
            pydantic compiled: False
                 install path: D:\dev\Miniconda3\envs\geoenv\Lib\site-packages\pydantic
               python version: 3.8.6 | packaged by conda-forge | (default, Oct  7 2020, 18:22:52) [MSC v.1916 64 bit (AMD64)]
                     platform: Windows-10-10.0.19041-SP0
     optional deps. installed: ['typing-extensions']

Pydantic v1.7 introduced the private attributes. But when the config flag underscore_attrs_are_private is set to True, the model's __doc__ attribute also becomes a private attribute. Example:

>>> from pydantic import BaseModel
>>> class MyModel(BaseModel):
...     """MyModel docsting."""
...     class Config:
...         underscore_attrs_are_private = True
...
>>> MyModel.__doc__
<member '__doc__' of 'MyModel' objects>

My current workaround is to define a new metaclass to handle the issue:

>>> from pydantic import BaseModel
>>> from pydantic.main import ModelMetaclass
>>> from pydantic.fields import ModelPrivateAttr
>>> class DocPreservingModelMetaclass(ModelMetaclass):
...     def __new__(mcs, name, bases, namespace, **kwargs):
...         cls = super().__new__(mcs, name, bases, namespace, **kwargs)
...         if cls.__config__.underscore_attrs_are_private:
...             doc = cls.__private_attributes__.pop('__doc__', None)
...             if isinstance(doc, ModelPrivateAttr):
...                 cls.__doc__ = doc.default
...         return cls
...
>>> class MyModel(BaseModel, metaclass=DocPreservingModelMetaclass):
...     """MyModel docsting."""
...     class Config:
...         underscore_attrs_are_private = True
...
>>> MyModel.__doc__
'MyModel docsting.'

But maybe it would be best if __doc__ was excluded via pydantic.utils.is_valid_private_name, much like the other standard properties are excluded through this method. However, I am not sure if the latter will have other implication in the current codebase.

@jstavrak jstavrak added the bug V1 Bug related to Pydantic V1.X label Nov 4, 2020
PrettyWood added a commit to PrettyWood/pydantic that referenced this issue Nov 4, 2020
@PrettyWood
Copy link
Member

Hi @jstavrak
Thanks for reporting! You're absolutely right I just made a quick fix. In the meantime your workaround seems good though the new metaclass could be simplified a bit

class DocPreservingModelMetaclass(ModelMetaclass):
     def __new__(mcs, name, bases, namespace, **kwargs):
         doc = namespace.pop('__doc__', None)
         cls = super().__new__(mcs, name, bases, namespace, **kwargs)
         cls.__doc__ = doc
         return cls

Have a good day

@jstavrak
Copy link
Author

jstavrak commented Nov 5, 2020

Hi guys,

Thanks for the immediate reply, fix, and simplification of the metaclass offered!

Have a nice day!

samuelcolvin added a commit that referenced this issue Jan 1, 2021
* fix: ignore `__doc__` as valid private attribute (#2091)

closes #2090

* Fixes a regression where Enum fields would not propagate keyword arguments to the schema (#2109)

fix #2108

* Fix schema extra not being included when field type is Enum

* Code format

* More code format

* Add changes file

Co-authored-by: Ben Martineau <b.martineau@iwoca.co.uk>

* fix: update all modified field values in `root_validator` when `validate_assignment` is on (#2119)

* fix: update all modified field values in `root_validator` when `validate_assignment` is on

closes #2116

* chore: update root_validator name

Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>

Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>

* fix: support `underscore_attrs_are_private` with generic models (#2139)

closes #2138

* fix: set right default value for required (optional) fields (#2143)

closes #2142

* Fix #2111: support pickle for built-in dataclasses (#2114)

* 2111: support pickle for built-in dataclasses

* 2111: add changes

* 2111: simplify test

* return original name + handle similar names

* add additional check

* fix a misspell

* remove useless f-string

* cleanup test

Co-authored-by: Samuel Colvin <s@muelcolvin.com>

* uprev and add history

* tempoarily pin pip

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Ben Martineau <bm424@cam.ac.uk>
Co-authored-by: Ben Martineau <b.martineau@iwoca.co.uk>
Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>
Co-authored-by: aimestereo <aimestereo@gmail.com>
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

Successfully merging a pull request may close this issue.

2 participants