python-betterproto
python-betterproto copied to clipboard
Add warnings import for deprecated messages
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
- Modified the code generator to always include
import warningswhen deprecated features are detected - 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, ...)