Get comments exactly before attributes It can be multiline comment. The placeholder "{model}" will be replaced with the name of the model class. We require that the comments are in #: (with a colon) format, so you can differentiate between private and public comments. :param c
(cls: type[Model])
| 146 | |
| 147 | |
| 148 | def _get_comments(cls: type[Model]) -> dict[str, str]: |
| 149 | """ |
| 150 | Get comments exactly before attributes |
| 151 | |
| 152 | It can be multiline comment. The placeholder "{model}" will be replaced with the name of the |
| 153 | model class. We require that the comments are in #: (with a colon) format, so you can |
| 154 | differentiate between private and public comments. |
| 155 | |
| 156 | :param cls: The class we need to extract comments from its source. |
| 157 | :return: The dictionary of comments by field name |
| 158 | """ |
| 159 | try: |
| 160 | source = inspect.getsource(cls) |
| 161 | except (AttributeError, TypeError, OSError): # pragma: nocoverage |
| 162 | return {} |
| 163 | comments = {} |
| 164 | |
| 165 | for cls_ in reversed(cls.__mro__): |
| 166 | if cls_ is object: |
| 167 | continue |
| 168 | matches = re.findall(r"((?:(?!\n|^)[^\w\n]*#:.*?\n)+?)[^\w\n]*(\w+)\s*[:=]", source) |
| 169 | for match in matches: |
| 170 | field_name = match[1] |
| 171 | # Extract text |
| 172 | comment = re.sub(r"(^\s*#:\s*|\s*$)", "", match[0], flags=re.MULTILINE) |
| 173 | # Class name template |
| 174 | comments[field_name] = comment.replace("{model}", cls_.__name__) |
| 175 | |
| 176 | return comments |
| 177 | |
| 178 | |
| 179 | class MetaInfo: |
no outgoing calls
no test coverage detected
searching dependent graphs…