New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add warning when set "alias" in low level annontated field #9170
base: main
Are you sure you want to change the base?
Add warning when set "alias" in low level annontated field #9170
Conversation
CodSpeed Performance ReportMerging #9170 will not alter performanceComparing Summary
|
please review. |
if hasattr(ann_type, '__args__'): | ||
for anno_arg in ann_type.__args__: | ||
if _typing_extra.is_annotated(anno_arg): | ||
for anno_type_arg in _typing_extra.get_args(anno_arg): | ||
if isinstance(anno_type_arg, FieldInfo) and anno_type_arg.alias is not None: | ||
warnings.warn( | ||
f'Field "{ann_name}" has `alias` should be set at higher level to have affect', | ||
UserWarning, | ||
) | ||
break | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if hasattr(ann_type, '__args__'): | |
for anno_arg in ann_type.__args__: | |
if _typing_extra.is_annotated(anno_arg): | |
for anno_type_arg in _typing_extra.get_args(anno_arg): | |
if isinstance(anno_type_arg, FieldInfo) and anno_type_arg.alias is not None: | |
warnings.warn( | |
f'Field "{ann_name}" has `alias` should be set at higher level to have affect', | |
UserWarning, | |
) | |
break | |
break | |
for anno_arg in getattr(ann_type, '__args__', ()): | |
if not _typing_extra.is_annotated(anno_arg): | |
continue | |
for anno_type_arg in _typing_extra.get_args(anno_arg): | |
if isinstance(anno_type_arg, FieldInfo) and anno_type_arg.alias is not None: | |
warnings.warn( | |
f'Field "{ann_name}" has `alias` should be set at higher level to have affect', | |
UserWarning, | |
) | |
break |
Note: I removed the second break, and it seems to me like including it is problematic — if there are multiple annotated args it looks to me like it won't hit them.
I would prefer if we broke this out into a separate function (private in this module) to get this check moved out-of-line instead of making this method bigger and more complicated than it already is. (In that case, the break
can just become return
). And if it makes sense to change the code in a different way than suggested above after moving it into its own function, that's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmontagu Thank for the suggestions.
As my understand and please correct me if i'm wrong, warning on the first annotated args that has alias
set or on all annotated args that have it is the same things !?.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change Summary
Add warning message when user setup annotated field with
alias
kwarg at low level. #7828.Related issue number
Checklist
Selected Reviewer: @adriangb