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

Method __new__

tortoise/models.py:507–561  ·  view source on GitHub ↗
(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any])

Source from the content-addressed store, hash-verified

505 __slots__ = ()
506
507 def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any]) -> ModelMeta:
508 fields_db_projection: dict[str, str] = {}
509 meta_class: Model.Meta = attrs.get("Meta", type("Meta", (), {}))
510 pk_attr: str = "id"
511
512 # Start searching for fields in the base classes.
513 inherited_attrs: dict = {}
514 for base in bases:
515 cls._search_for_field_attributes(base, inherited_attrs)
516 if inherited_attrs:
517 # Ensure that the inherited fields are before the defined ones.
518 attrs = {**inherited_attrs, **attrs}
519 is_abstract = getattr(meta_class, "abstract", False)
520 if name != "Model":
521 attrs, pk_attr = cls._parse_custom_pk(attrs, pk_attr, name, is_abstract)
522 fields_map, filters, fk_fields, m2m_fields, o2o_fields = cls._dispatch_fields(
523 attrs, fields_db_projection, is_abstract
524 )
525 if name != "Model":
526 cls._check_field_name_conflicts(fields_map, name)
527
528 # Clean the class attributes
529 for slot in fields_map:
530 attrs.pop(slot, None)
531 attrs["_meta"] = meta = cls.build_meta(
532 meta_class,
533 fields_map,
534 fields_db_projection,
535 filters,
536 fk_fields,
537 o2o_fields,
538 m2m_fields,
539 pk_attr,
540 )
541
542 new_class = super().__new__(cls, name, bases, attrs)
543 for field in meta.fields_map.values():
544 field.model = new_class # type: ignore
545
546 if not attrs.get("_no_comments"):
547 for fname, comment in _get_comments(new_class).items(): # type: ignore
548 if fname in fields_map:
549 fields_map[fname].docstring = comment
550 if fields_map[fname].description is None:
551 fields_map[fname].description = comment.split("\n")[0]
552
553 if new_class.__doc__ and not meta.table_description:
554 meta.table_description = inspect.cleandoc(new_class.__doc__).split("\n")[0]
555 for value in attrs.values():
556 if isinstance(value, Manager):
557 value._model = new_class
558 meta._model = new_class # type: ignore
559 meta.manager._model = new_class
560 meta.finalise_fields()
561 return new_class
562
563 @classmethod
564 def _search_for_field_attributes(cls, base: type, attrs: dict) -> None:

Callers 3

_cloneMethod · 0.45
_init_from_dbMethod · 0.45
constructMethod · 0.45

Calls 10

_get_commentsFunction · 0.85
_parse_custom_pkMethod · 0.80
_dispatch_fieldsMethod · 0.80
build_metaMethod · 0.80
itemsMethod · 0.80
finalise_fieldsMethod · 0.80
getMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected