t | class AnnoDoc(type): | t | class AnnoDoc(type): |
| | | |
| def __new__(cls, name, bases, dct): | | def __new__(cls, name, bases, dct): |
| class_doc = dct.get('__doc__', '') | | class_doc = dct.get('__doc__', '') |
| annotations = dct.get('__annotations__', {}) | | annotations = dct.get('__annotations__', {}) |
| new_annotations = {} | | new_annotations = {} |
| doc_lines = [] if not class_doc else [class_doc.strip()] | | doc_lines = [] if not class_doc else [class_doc.strip()] |
| for attr, annotation in annotations.items(): | | for attr, annotation in annotations.items(): |
| if isinstance(annotation, str): | | if isinstance(annotation, str): |
| doc_lines.append(f'{attr}: {annotation}') | | doc_lines.append(f'{attr}: {annotation}') |
| if attr in dct: | | if attr in dct: |
| new_annotations[attr] = type(dct[attr]) | | new_annotations[attr] = type(dct[attr]) |
| else: | | else: |
| new_annotations[attr] = annotation | | new_annotations[attr] = annotation |
| if not doc_lines: | | if not doc_lines: |
| dct['__doc__'] = None | | dct['__doc__'] = None |
| else: | | else: |
| dct['__doc__'] = '\n'.join(doc_lines) | | dct['__doc__'] = '\n'.join(doc_lines) |
| if not new_annotations: | | if not new_annotations: |
| dct['__annotations__'] = {} | | dct['__annotations__'] = {} |
| else: | | else: |
| dct['__annotations__'] = new_annotations | | dct['__annotations__'] = new_annotations |
| return super().__new__(cls, name, bases, dct) | | return super().__new__(cls, name, bases, dct) |