|
1 | 1 | import ipaddress |
2 | 2 | import uuid |
| 3 | +import warnings |
3 | 4 | import weakref |
4 | 5 | from datetime import date, datetime, time, timedelta |
5 | 6 | from decimal import Decimal |
@@ -539,21 +540,33 @@ def __new__( |
539 | 540 | config_kwargs = { |
540 | 541 | key: kwargs[key] for key in kwargs.keys() & allowed_config_kwargs |
541 | 542 | } |
| 543 | + is_polymorphic = False |
542 | 544 | if IS_PYDANTIC_V2: |
543 | 545 | base_fields = {} |
544 | 546 | base_annotations = {} |
545 | 547 | for base in bases[::-1]: |
546 | 548 | if issubclass(base, BaseModel): |
547 | 549 | base_fields.update(get_model_fields(base)) |
548 | 550 | base_annotations.update(base.__annotations__) |
| 551 | + if hasattr(base, "__tablename__"): |
| 552 | + is_polymorphic = True |
549 | 553 | # use base_fields overwriting the ones from the class for inherit |
550 | 554 | # if base is a sqlalchemy model, it's attributes will be an InstrumentedAttribute |
551 | 555 | # thus pydantic will use the value of the attribute as the default value |
552 | 556 | base_annotations.update(dict_used["__annotations__"]) |
553 | 557 | dict_used["__annotations__"] = base_annotations |
554 | 558 | base_fields.update(dict_used) |
555 | 559 | dict_used = base_fields |
556 | | - new_cls = super().__new__(cls, name, bases, dict_used, **config_kwargs) |
| 560 | + # if is_polymorphic, disable pydantic `shadows an attribute` warning |
| 561 | + if is_polymorphic: |
| 562 | + with warnings.catch_warnings(): |
| 563 | + warnings.filterwarnings( |
| 564 | + "ignore", |
| 565 | + message="Field name .+ shadows an attribute in parent.+", |
| 566 | + ) |
| 567 | + new_cls = super().__new__(cls, name, bases, dict_used, **config_kwargs) |
| 568 | + else: |
| 569 | + new_cls = super().__new__(cls, name, bases, dict_used, **config_kwargs) |
557 | 570 | new_cls.__annotations__ = { |
558 | 571 | **relationship_annotations, |
559 | 572 | **pydantic_annotations, |
|
0 commit comments