python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

Add warnings import for deprecated messages

Open MakonnenMak opened this issue 7 months ago • 0 comments

Summary

Issue reported here - https://github.com/danielgtaylor/python-betterproto/issues/239

Fix Missing Warnings Import for Deprecated Protobuf Messages

Issue

When using deprecated protobuf messages or fields, betterproto generates code that calls warnings.warn() but was missing the required import warnings statement. This caused NameError: name 'warnings' is not defined when deprecated features were used.

Root Cause

The code generator properly added deprecation warnings but assumed the warnings module would be available without explicitly importing it.

Solution

  1. Modified the code generator to always include import warnings when deprecated features are detected
  2. Added test case (test_warnings_import_for_deprecated_message()) to verify:
    • Warnings module is properly imported in generated code
    • Works for both message-level and field-level deprecation

Testing

  • Added new test case that specifically verifies the import exists
  • Existing deprecation tests continue to pass
  • Manually verified deprecated messages/fields now work without NameError

Impact

Fixes runtime errors when using deprecated protobuf definitions while maintaining all existing warning behavior.

Checklist

  • [ ] If code changes were made then they have been tested.
    • [ ] I have updated the documentation to reflect the changes.
  • [ ] This PR fixes an issue.
  • [ ] This PR adds something new (e.g. new method or parameters).
    • [ ] This change has an associated test.
  • [ ] This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • [ ] This PR is not a code change (e.g. documentation, README, ...)

MakonnenMak avatar Jun 11 '25 04:06 MakonnenMak