Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test type annotations with mypy #668

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

terencehonles
Copy link
Contributor

@terencehonles terencehonles commented Aug 29, 2024

I was going to contribute my typing for the @job decorator that was missing from #656, but I then realized there was no way to check if the annotations were correct, so I added mypy to the testing matrix

Comment on lines +37 to +39
else:
if connection is None:
connection = queue.connection
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this was an oversight in the previous code?


RQ = getattr(settings, 'RQ', {})
default_result_ttl = RQ.get('DEFAULT_RESULT_TTL')
if default_result_ttl is not None:
kwargs.setdefault('result_ttl', default_result_ttl)

decorator = _rq_job(queue, connection=connection, *args, **kwargs)
kwargs['connection'] = connection
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just for mypy (python/mypy#6799) and rather than adding a type ignore I figured I'd update the kwargs, since it would never contain the connection based on the function's definition

@@ -22,9 +22,7 @@ def handle(self, *args, **options):
Queues the function given with the first argument with the
parameters given with the rest of the argument list.
"""
verbosity = int(options.get('verbosity', 1))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These will all exist, and using indexed access will tell mypy they are not None

- name: Set up Python 3.8
uses: actions/[email protected]
with:
python-version: "3.8"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Picking an old supported Python version since it doesn't it's tested otherwise, and this will make sure the type annotations don't break the older Pythons

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Export type annotations

@@ -722,7 +718,7 @@ def test_commandline_verbosity_affects_logging_level(self, setup_loghandlers_moc
setup_loghandlers_mock.assert_called_once_with(expected_level[verbosity])

@override_settings(RQ={'SCHEDULER_CLASS': 'django_rq.tests.fixtures.DummyScheduler'})
def test_scheduler_default_timeout(self):
def test_scheduler_default(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was overridden by the one that comes after it (thanks mypy!)

"""Fetch jobs in bulk from Redis.
1. If job data is not present in Redis, discard the result
2. If `registry` argument is supplied, delete empty jobs from registry
"""
jobs = Job.fetch_many(job_ids, connection=queue.connection, serializer=queue.serializer)
jobs = cast(
List[Optional[Job]], Job.fetch_many(job_ids, connection=queue.connection, serializer=queue.serializer)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a bug in fetch_many and mypy will complain that job is None can't happen otherwise

@@ -403,7 +404,12 @@ def clear_queue(request, queue_index):
queue.empty()
messages.info(request, 'You have successfully cleared the queue %s' % queue.name)
except ResponseError as e:
if 'EVALSHA' in e.message:
try:
suppress = 'EVALSHA' in e.message # type: ignore[attr-defined]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't find this ever existing, but it seems to have worked for the PR author?

[mypy]
allow_redefinition = true
check_untyped_defs = true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

treat untyped code as Any to make sure everything is checked even if it ends up not being very useful

warn_redundant_casts = true
warn_unused_ignores = true
warn_unreachable = true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

warn in case mypy thinks this case can never happen (in case it actually can and the type checking is not checking a path that should be checked)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant