Skip to content

fix(job): use Optional for parameters defaulting to None [python] - #4908

Merged
roggervalf merged 1 commit into
taskforcesh:masterfrom
rangarajan19:fix/python-optional-annotations
Oct 10, 2026
Merged

roggervalf merged 1 commit into
taskforcesh:masterfrom
rangarajan19:fix/python-optional-annotations

Conversation

@rangarajan19

Copy link
Copy Markdown
Contributor

Port Impact Checklist

  • Python – does this change need to be ported or documented in the Python library?
  • Elixir – does this change need to be ported or documented in the Elixir library?
  • PHP – does this change need to be ported or documented in the PHP library?
  • Rust – does this change need to be ported or documented in the Rust library?
  • .NET – does this change need to be ported or documented in the .NET library?

Why

Parameters annotated str (or dict/Redis) but defaulting to None are implicit Optional, which PEP 484 and the current typing spec prohibit. When bullmq is used as an external package, type checkers therefore pick job_id: str and hide that None is accepted, so Job(queue, name, data, job_id=None) is reported as an error and Job.id is typed str.

Fixes #4905

How

Annotated these as Optional[...], matching the style already used in the package (e.g. Optional[str] = None in redis_connection.py):

  • Job.__init__(job_id) and Job.token
  • Queue.upsertJobScheduler(job_name, opts, producer_id)
  • Scripts.addJobScheduler and Scripts.updateJobSchedulerNextMillis (producer_id)
  • Scripts.extendLock(client)

An AST scan of python/bullmq finds no remaining implicit-Optional parameters.

Additional Notes (Optional)

  • Annotations only; no runtime behaviour change apart from the added Optional import.
  • Checked with a consumer script type-checked by mypy: before, Job(..., job_id=None) and upsertJobScheduler(job_name=None, opts=None, producer_id=None) produced 4 errors and Job.id was str; after, 0 errors and Job.id is str | None.
  • Side effect: Job.id is now honestly Optional[str], so mypy --no-implicit-optional reports a few new "str | None passed where str expected" errors where id is forwarded (e.g. scripts.toKey, moveToFinished, LockManager.track_job). These were latent before; CI installs mypy but does not run it. Happy to follow up with narrowing there if wanted.
  • I could not run the pytest suite locally (no Redis available); relying on CI for that. The flake8 gate (E9,F63,F7,F82) passes and the changed files compile.
  • Tooling: written with the help of an AI assistant (Claude Code); I reviewed the diff.

🤖 Generated with Claude Code

Parameters annotated as `str`/`dict`/`Redis` but defaulting to `None`
are implicit Optional, which PEP 484 and the current typing spec
prohibit. Type checkers analysing bullmq as an external package pick
`job_id: str`, hiding that `None` is accepted.

Annotate them as Optional: Job.job_id and Job.token, the
upsertJobScheduler job_name/opts/producer_id arguments, the
producer_id arguments of Scripts.addJobScheduler and
Scripts.updateJobSchedulerNextMillis, and Scripts.extendLock's client.

Fixes taskforcesh#4905

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The annotations accurately reflect existing nullable behavior, and an AST check found no remaining implicit-Optional parameters.

0 open findings

What changed in this PR

Corrects Python type annotations for parameters and fields that accept None.

Changes:

  • Marks nullable job IDs, tokens, scheduler options, producer IDs, and Redis clients as Optional.
  • Adds required Optional imports without changing runtime behavior.
File Description
python/​bullmq/​job.py Corrects nullable job ID and token types.
python/​bullmq/​queue.py Corrects scheduler API parameter types.
python/​bullmq/​scripts.py Corrects nullable script parameter types.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@roggervalf
roggervalf merged commit 5232e19 into taskforcesh:master Oct 10, 2026
31 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 3.3.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 6.3.13 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.3.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.2.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Python job_id param of Job has almost-illegal combination of type annotation and default value

3 participants