Skip to content

Commit f981261

Browse files
gmrclaude
andcommitted
Rename Message.message_type to Message.type to match AMQP
The AMQP property is `type`, and pika uses `properties.type`. Rename the Pydantic field to match, eliminating the awkward `message_type` indirection. Consumer.message_type property is unchanged for backward compatibility. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cc6e847 commit f981261

7 files changed

Lines changed: 17 additions & 19 deletions

File tree

docs/migrating.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ Key differences:
208208

209209
- Properties are top-level fields on `Message`, not nested under
210210
`msg.properties`
211-
- The AMQP `type` property is accessed as `msg.message_type` (not `msg.type`)
211+
- The AMQP `type` property is accessed as `msg.type` (matching the AMQP
212+
property name)
212213
- `Message` is a Pydantic `BaseModel` with validation
213214
- `body` starts as raw bytes; the `Codec` class decodes it asynchronously
214215
before the consumer sees it

rejected/consumer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,19 @@ def _pre_execute(
155155
msg.correlation_id or msg.message_id or str(uuid.uuid4())
156156
)
157157

158-
if msg.message_type:
159-
self.set_sentry_context('type', msg.message_type)
158+
if msg.type:
159+
self.set_sentry_context('type', msg.type)
160160

161161
# Validate message type
162162
if self.MESSAGE_TYPE:
163163
expected = self.MESSAGE_TYPE
164164
if isinstance(expected, (tuple, list, set)):
165-
supported = msg.message_type in expected
165+
supported = msg.type in expected
166166
else:
167-
supported = msg.message_type == expected
167+
supported = msg.type == expected
168168
if not supported:
169169
self.logger.warning(
170-
'Received unsupported message type: %s', msg.message_type
170+
'Received unsupported message type: %s', msg.type
171171
)
172172
if self._drop_invalid:
173173
if self._drop_exchange:
@@ -619,7 +619,7 @@ def message_id(self) -> str | None:
619619
def message_type(self) -> str | None:
620620
if not self._context:
621621
return None
622-
return self._context.message.message_type
622+
return self._context.message.type
623623

624624
@property
625625
def priority(self) -> int | None:

rejected/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class Message(pydantic.BaseModel):
142142
str, bool | dict[str, typing.Any] | float | int | str | bytes
143143
]
144144
message_id: str | None
145-
message_type: str | None
145+
type: str | None
146146
priority: int | None
147147
redelivered: bool
148148
reply_to: str | None

rejected/process.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,7 @@ async def invoke_consumer(self, ctx: models.ProcessingContext) -> None:
291291
msg = ctx.message
292292
try:
293293
msg.body = await self.codec.decode(
294-
msg.body,
295-
msg.content_type,
296-
msg.content_encoding,
297-
msg.message_type,
294+
msg.body, msg.content_type, msg.content_encoding, msg.type
298295
)
299296
except codecs.DecodeError as error:
300297
LOGGER.error('Failed to decode message body: %s', error)
@@ -451,7 +448,7 @@ def on_message(
451448
dict(properties.headers) if properties.headers else {}
452449
),
453450
message_id=properties.message_id,
454-
message_type=properties.type,
451+
type=properties.type,
455452
priority=properties.priority,
456453
redelivered=redelivered,
457454
reply_to=properties.reply_to,

rejected/testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def create_context(
162162
expiration=properties.get('expiration'),
163163
headers=properties.get('headers', {}),
164164
message_id=properties.get('message_id', str(uuid.uuid4())),
165-
message_type=properties.get('type'),
165+
type=properties.get('type'),
166166
priority=properties.get('priority'),
167167
redelivered=False,
168168
reply_to=properties.get('reply_to'),

tests/test_consumer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _make_message(**kwargs: typing.Any) -> models.Message:
2424
'expiration': '32768',
2525
'headers': {'foo': 'bar'},
2626
'message_id': 'mid123',
27-
'message_type': 'test',
27+
'type': 'test',
2828
'priority': 5,
2929
'redelivered': False,
3030
'reply_to': 'rtrk',
@@ -123,7 +123,7 @@ async def process(self):
123123
pass
124124

125125
obj = TypedConsumer(config_module.Settings({}), None)
126-
ctx = _make_ctx(_make_message(message_type='wrong'))
126+
ctx = _make_ctx(_make_message(type='wrong'))
127127
await obj.execute(ctx)
128128
self.assertEqual(ctx.result, models.Result.MESSAGE_DROP)
129129

@@ -135,7 +135,7 @@ async def process(self):
135135
pass
136136

137137
obj = TypedConsumer(config_module.Settings({}), None)
138-
ctx = _make_ctx(_make_message(message_type='wrong'))
138+
ctx = _make_ctx(_make_message(type='wrong'))
139139
await obj.execute(ctx)
140140
self.assertEqual(ctx.result, models.Result.MESSAGE_EXCEPTION)
141141

@@ -206,7 +206,7 @@ async def process(self):
206206
self.assertEqual(captured['expiration'], msg.expiration)
207207
self.assertEqual(captured['headers'], msg.headers)
208208
self.assertEqual(captured['message_id'], msg.message_id)
209-
self.assertEqual(captured['message_type'], msg.message_type)
209+
self.assertEqual(captured['message_type'], msg.type)
210210
self.assertEqual(captured['name'], 'PropConsumer')
211211
self.assertEqual(captured['priority'], msg.priority)
212212
self.assertEqual(captured['redelivered'], msg.redelivered)

tests/test_process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _make_message(
9090
expiration='32768',
9191
headers={'foo': 'bar'},
9292
message_id='mid123',
93-
message_type='test',
93+
type='test',
9494
priority=5,
9595
redelivered=redelivered,
9696
reply_to='rtrk',

0 commit comments

Comments
 (0)