pydantic.mypy
This module includes classes and functions designed specifically for use with the mypy plugin.
ModelConfigData ¶
ModelConfigData(
forbid_extra=None,
frozen=None,
from_attributes=None,
populate_by_name=None,
has_alias_generator=None,
)
Pydantic mypy plugin model config class.
Source code in pydantic/mypy.py
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 |
|
get_values_dict ¶
get_values_dict()
Returns a dict of Pydantic model config names to their values.
It includes the config if config value is not None
.
Source code in pydantic/mypy.py
1025 1026 1027 1028 1029 1030 |
|
setdefault ¶
setdefault(key, value)
Set default value for Pydantic model config if config value is None
.
Source code in pydantic/mypy.py
1039 1040 1041 1042 |
|
update ¶
update(config)
Update Pydantic model config values.
Source code in pydantic/mypy.py
1032 1033 1034 1035 1036 1037 |
|
PydanticModelField ¶
PydanticModelField(
name,
alias,
has_dynamic_alias,
has_default,
line,
column,
type,
info,
)
Based on mypy.plugins.dataclasses.DataclassAttribute.
Source code in pydantic/mypy.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
deserialize
classmethod
¶
deserialize(info, data, api)
Based on mypy.plugins.dataclasses.DataclassAttribute.deserialize.
Source code in pydantic/mypy.py
379 380 381 382 383 384 |
|
expand_type ¶
expand_type(current_info)
Based on mypy.plugins.dataclasses.DataclassAttribute.expand_type.
Source code in pydantic/mypy.py
347 348 349 350 351 352 353 354 355 |
|
expand_typevar_from_subtype ¶
expand_typevar_from_subtype(sub_type)
Expands type vars in the context of a subtype when an attribute is inherited from a generic super type.
Source code in pydantic/mypy.py
386 387 388 389 390 391 |
|
serialize ¶
serialize()
Based on mypy.plugins.dataclasses.DataclassAttribute.serialize.
Source code in pydantic/mypy.py
366 367 368 369 370 371 372 373 374 375 376 377 |
|
to_argument ¶
to_argument(current_info, typed, force_optional, use_alias)
Based on mypy.plugins.dataclasses.DataclassAttribute.to_argument.
Source code in pydantic/mypy.py
338 339 340 341 342 343 344 345 |
|
to_var ¶
to_var(current_info, use_alias)
Based on mypy.plugins.dataclasses.DataclassAttribute.to_var.
Source code in pydantic/mypy.py
357 358 359 360 361 362 363 364 |
|
PydanticModelTransformer ¶
PydanticModelTransformer(cls, reason, api, plugin_config)
Transform the BaseModel subclass according to the plugin settings.
Attributes:
Name | Type | Description |
---|---|---|
tracked_config_fields |
set[str]
|
A set of field configs that the plugin has to track their value. |
Source code in pydantic/mypy.py
409 410 411 412 413 414 415 416 417 418 419 420 |
|
add_initializer ¶
add_initializer(fields, config, is_settings)
Adds a fields-aware __init__
method to the class.
The added __init__
will be annotated with types vs. all Any
depending on the plugin settings.
Source code in pydantic/mypy.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 |
|
add_model_construct_method ¶
add_model_construct_method(fields, config, is_settings)
Adds a fully typed model_construct
classmethod to the class.
Similar to the fields-aware init method, but always uses the field names (not aliases), and does not treat settings fields as optional.
Source code in pydantic/mypy.py
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
|
adjust_decorator_signatures ¶
adjust_decorator_signatures()
When we decorate a function f
with pydantic.validator(...)
, pydantic.field_validator
or pydantic.serializer(...)
, mypy sees f
as a regular method taking a self
instance,
even though pydantic internally wraps f
with classmethod
if necessary.
Teach mypy this by marking any function whose outermost decorator is a validator()
,
field_validator()
or serializer()
call as a classmethod
.
Source code in pydantic/mypy.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
|
collect_config ¶
collect_config()
Collects the values of the config attributes that are used by the plugin, accounting for parent classes.
Source code in pydantic/mypy.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
|
collect_field_from_stmt ¶
collect_field_from_stmt(stmt, model_config)
Get pydantic model field from statement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stmt |
AssignmentStmt
|
The statement. |
required |
model_config |
ModelConfigData
|
Configuration settings for the model. |
required |
Returns:
Type | Description |
---|---|
PydanticModelField | None
|
A pydantic model field if it could find the field in statement. Otherwise, |
Source code in pydantic/mypy.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
|
collect_fields ¶
collect_fields(model_config)
Collects the fields for the model, accounting for parent classes.
Source code in pydantic/mypy.py
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
|
get_alias_info
staticmethod
¶
get_alias_info(stmt)
Returns a pair (alias, has_dynamic_alias), extracted from the declaration of the field defined in stmt
.
has_dynamic_alias
is True if and only if an alias is provided, but not as a string literal.
If has_dynamic_alias
is True, alias
will be None.
Source code in pydantic/mypy.py
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 |
|
get_config_update ¶
get_config_update(name, arg)
Determines the config update due to a single kwarg in the ConfigDict definition.
Warns if a tracked config attribute is set to a value the plugin doesn't know how to interpret (e.g., an int)
Source code in pydantic/mypy.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 |
|
get_field_arguments ¶
get_field_arguments(
fields,
typed,
use_alias,
requires_dynamic_aliases,
is_settings,
)
Helper function used during the construction of the __init__
and model_construct
method signatures.
Returns a list of mypy Argument instances for use in the generated signatures.
Source code in pydantic/mypy.py
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 |
|
get_has_default
staticmethod
¶
get_has_default(stmt)
Returns a boolean indicating whether the field defined in stmt
is a required field.
Source code in pydantic/mypy.py
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
|
is_dynamic_alias_present
staticmethod
¶
is_dynamic_alias_present(fields, has_alias_generator)
Returns whether any fields on the model have a "dynamic alias", i.e., an alias that cannot be determined during static analysis.
Source code in pydantic/mypy.py
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
set_frozen ¶
set_frozen(fields, frozen)
Marks all fields as properties so that attempts to set them trigger mypy errors.
This is the same approach used by the attrs and dataclasses plugins.
Source code in pydantic/mypy.py
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
|
should_init_forbid_extra ¶
should_init_forbid_extra(fields, config)
Indicates whether the generated __init__
should get a **kwargs
at the end of its signature.
We disallow arbitrary kwargs if the extra config setting is "forbid", or if the plugin config says to, unless a required dynamic alias is present (since then we can't determine a valid signature).
Source code in pydantic/mypy.py
980 981 982 983 984 985 986 987 988 989 990 991 |
|
transform ¶
transform()
Configures the BaseModel subclass according to the plugin settings.
In particular:
- determines the model config and fields,
- adds a fields-aware signature for the initializer and construct methods
- freezes the class if frozen = True
- stores the fields, config, and if the class is settings in the mypy metadata for access by subclasses
Source code in pydantic/mypy.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
|
PydanticPlugin ¶
PydanticPlugin(options)
Bases: Plugin
The Pydantic mypy plugin.
Source code in pydantic/mypy.py
131 132 133 134 |
|
get_base_class_hook ¶
get_base_class_hook(fullname)
Update Pydantic model class.
Source code in pydantic/mypy.py
136 137 138 139 140 141 142 143 |
|
get_class_decorator_hook ¶
get_class_decorator_hook(fullname)
Mark pydantic.dataclasses as dataclass.
Mypy version 1.1.1 added support for @dataclass_transform
decorator.
Source code in pydantic/mypy.py
164 165 166 167 168 169 170 171 |
|
get_function_hook ¶
get_function_hook(fullname)
Adjust the return type of the Field
function.
Source code in pydantic/mypy.py
151 152 153 154 155 156 |
|
get_metaclass_hook ¶
get_metaclass_hook(fullname)
Update Pydantic ModelMetaclass
definition.
Source code in pydantic/mypy.py
145 146 147 148 149 |
|
get_method_hook ¶
get_method_hook(fullname)
Adjust return type of from_orm
method call.
Source code in pydantic/mypy.py
158 159 160 161 162 |
|
report_config_data ¶
report_config_data(ctx)
Return all plugin config data.
Used by mypy to determine if cache needs to be discarded.
Source code in pydantic/mypy.py
173 174 175 176 177 178 |
|
PydanticPluginConfig ¶
PydanticPluginConfig(options)
A Pydantic mypy plugin config holder.
Attributes:
Name | Type | Description |
---|---|---|
init_forbid_extra |
bool
|
Whether to add a |
init_typed |
bool
|
Whether to annotate fields in the generated |
warn_required_dynamic_aliases |
bool
|
Whether to raise required dynamic aliases error. |
debug_dataclass_transform |
bool
|
Whether to not reset |
Source code in pydantic/mypy.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
to_data ¶
to_data()
Returns a dict of config names to their values.
Source code in pydantic/mypy.py
287 288 289 |
|
add_method ¶
add_method(
api,
cls,
name,
args,
return_type,
self_type=None,
tvar_def=None,
is_classmethod=False,
)
Very closely related to mypy.plugins.common.add_method_to_class
, with a few pydantic-specific changes.
Source code in pydantic/mypy.py
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 |
|
error_default_and_default_factory_specified ¶
error_default_and_default_factory_specified(api, context)
Emits an error when Field
has both default
and default_factory
together.
Source code in pydantic/mypy.py
1087 1088 1089 |
|
error_from_attributes ¶
error_from_attributes(model_name, api, context)
Emits an error when the model does not have from_attributes=True
.
Source code in pydantic/mypy.py
1053 1054 1055 |
|
error_invalid_config_value ¶
error_invalid_config_value(name, api, context)
Emits an error when the config value is invalid.
Source code in pydantic/mypy.py
1058 1059 1060 |
|
error_required_dynamic_aliases ¶
error_required_dynamic_aliases(api, context)
Emits required dynamic aliases error.
This will be called when warn_required_dynamic_aliases=True
.
Source code in pydantic/mypy.py
1063 1064 1065 1066 1067 1068 |
|
error_unexpected_behavior ¶
error_unexpected_behavior(detail, api, context)
Emits unexpected behavior error.
Source code in pydantic/mypy.py
1071 1072 1073 1074 1075 1076 1077 1078 1079 |
|
error_untyped_fields ¶
error_untyped_fields(api, context)
Emits an error when there is an untyped field in the model.
Source code in pydantic/mypy.py
1082 1083 1084 |
|
from_attributes_callback ¶
from_attributes_callback(ctx)
Raise an error if from_attributes is not enabled.
Source code in pydantic/mypy.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
parse_toml ¶
parse_toml(config_file)
Returns a dict of config keys to values.
It reads configs from toml file and returns None
if the file is not a toml file.
Source code in pydantic/mypy.py
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 |
|
plugin ¶
plugin(version)
version
is the mypy version string.
We might want to use this to print a warning if the mypy version being used is newer, or especially older, than we expect (or need).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
version |
str
|
The mypy version string. |
required |
Return
The Pydantic mypy plugin type.
Source code in pydantic/mypy.py
113 114 115 116 117 118 119 120 121 122 123 124 125 |
|