Wrapper that automatically generates a docstring for classes based on their attributes and methods.
(cls, custom_intro=None, custom_args=None, checkpoint=None)
| 4172 | |
| 4173 | |
| 4174 | def auto_class_docstring(cls, custom_intro=None, custom_args=None, checkpoint=None): |
| 4175 | """ |
| 4176 | Wrapper that automatically generates a docstring for classes based on their attributes and methods. |
| 4177 | """ |
| 4178 | # import here to avoid circular import |
| 4179 | from transformers.models import auto as auto_module |
| 4180 | |
| 4181 | is_dataclass = False |
| 4182 | is_processor = False |
| 4183 | is_config = False |
| 4184 | is_image_processor = False |
| 4185 | docstring_init = "" |
| 4186 | docstring_args = "" |
| 4187 | if "PreTrainedModel" in (x.__name__ for x in cls.__mro__): |
| 4188 | docstring_init = auto_method_docstring( |
| 4189 | cls.__init__, parent_class=cls, custom_args=custom_args, checkpoint=checkpoint |
| 4190 | ).__doc__.replace("Args:", "Parameters:") |
| 4191 | elif "ProcessorMixin" in (x.__name__ for x in cls.__mro__): |
| 4192 | is_processor = True |
| 4193 | docstring_init = auto_method_docstring( |
| 4194 | cls.__init__, |
| 4195 | parent_class=cls, |
| 4196 | custom_args=custom_args, |
| 4197 | checkpoint=checkpoint, |
| 4198 | source_args_dict=get_args_doc_from_source([ModelArgs, ImageProcessorArgs, ProcessorArgs]), |
| 4199 | ).__doc__.replace("Args:", "Parameters:") |
| 4200 | elif "ModelOutput" in (x.__name__ for x in cls.__mro__): |
| 4201 | # We have a data class |
| 4202 | is_dataclass = True |
| 4203 | doc_class = cls.__doc__ |
| 4204 | if custom_args is None and doc_class: |
| 4205 | custom_args = doc_class |
| 4206 | |
| 4207 | # Pass over docs from the direct parent, if it is a class from `modeling_outputs.py` |
| 4208 | direct_ancestor = cls.__mro__[1] |
| 4209 | if direct_ancestor.__name__ != "ModelOutput" and direct_ancestor.__doc__: |
| 4210 | custom_args = "" if custom_args is None else custom_args |
| 4211 | custom_args = "\n" + set_min_indent(direct_ancestor.__doc__.strip("\n"), 0) + "\n" + custom_args |
| 4212 | |
| 4213 | docstring_args = auto_method_docstring( |
| 4214 | cls.__init__, |
| 4215 | parent_class=cls, |
| 4216 | custom_args=custom_args, |
| 4217 | checkpoint=checkpoint, |
| 4218 | source_args_dict=get_args_doc_from_source(ModelOutputArgs), |
| 4219 | ).__doc__ |
| 4220 | elif any("BaseImageProcessor" in x.__name__ for x in cls.__mro__): |
| 4221 | is_image_processor = True |
| 4222 | docstring_init = auto_method_docstring( |
| 4223 | cls.__init__, |
| 4224 | parent_class=cls, |
| 4225 | custom_args=custom_args, |
| 4226 | checkpoint=checkpoint, |
| 4227 | source_args_dict=get_args_doc_from_source(ImageProcessorArgs), |
| 4228 | ).__doc__ |
| 4229 | elif "PreTrainedConfig" in (x.__name__ for x in cls.__mro__): |
| 4230 | is_config = True |
| 4231 | doc_class = cls.__doc__ |
no test coverage detected