feat(flow-producer): add return type hints to methods [python] - #4088
feat(flow-producer): add return type hints to methods [python]#4088yogeshwaran-c wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves the Python BullMQ API ergonomics by adding missing parameter/return type hints to FlowProducer methods, improving IDE autocomplete and static analysis without changing runtime behavior.
Changes:
- Add
Any/Optionalimports and annotatepipeandqueues_optswhereNoneis actually passed. - Add return type hints for
queueFromNode,addChildren,addNodes,addNode,add, andaddBulk. - Tighten parameter annotations for
queueFromNodeto useQueueKeysand returnMinimalQueue.
Comments suppressed due to low confidence (1)
python/bullmq/flow_producer.py:137
- With the new
-> list[dict]return annotation, initializingresult = Noneand later returningresultwill cause stricter type checkers (e.g., mypy) to inferresultasOptional[list[dict]]and flag the return as incompatible. Prefer returningjob_treesdirectly (or explicitly typeresultaslist[dict]).
result = None
async with self.redisConnection.conn.pipeline(transaction=True) as pipe:
job_trees = await self.addNodes(flows, pipe)
await pipe.execute()
result = job_trees
return result
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| async def add(self, flow: dict, opts: dict = {}) -> dict: | ||
| parent_opts = flow.get("opts", {}).get("parent", None) |
There was a problem hiding this comment.
opts: dict = {} uses a mutable default argument. Even if currently treated as read-only, this can lead to shared state across calls and is discouraged. Prefer opts: Optional[dict] = None (or similar) and initialize an empty dict inside the method.
| @@ -127,7 +127,7 @@ async def add(self, flow: dict, opts: dict = {}): | |||
|
|
|||
| return result | |||
There was a problem hiding this comment.
With the new -> dict return annotation, initializing result = None and later returning result will cause stricter type checkers (e.g., mypy) to infer result as Optional[dict] and flag the return as incompatible. Prefer returning jobs_tree directly (or explicitly type result as dict).
…ift [python]
- Replace 'opts: dict = {}' with 'opts: dict | None = None' (PEP 604) to avoid mutable default
- Tighten dict-returning methods so the return value is never inferred as Optional[dict]
|
Addressed Copilot review feedback in 12a0b11:
|
Summary
Adds missing parameter and return type hints to the Python
FlowProducermethods inpython/bullmq/flow_producer.pyto improve type clarity and IDE support.Methods updated
queueFromNode— typednode: dict,queue_keys: QueueKeys, return-> MinimalQueueaddChildren— typednodes: list[dict],parent: dict,queues_opts: Optional[dict],pipe: Any, return-> list[dict]addNodes— typedpipe: Any, return-> list[dict]addNode— typedqueues_opts: Optional[dict](wasdict, butNoneis passed fromaddNodes),pipe: Any, return-> dictadd— return-> dictaddBulk— return-> list[dict]Also imports
AnyandOptionalfromtyping.Return types are derived directly from the actual return values observed in the code (e.g.
addNodereturns{"job": job, "children": children}or{"job": job}). No runtime behavior changes.Test plan
ast.parse)