MCPcopy
hub / github.com/tortoise/tortoise-orm / _get_comments

Function _get_comments

tortoise/models.py:148–176  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

146
147
148def _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
179class MetaInfo:

Callers 1

__new__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…