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

Fix Django 5.0 with PyPy 3.10 #708

Closed
wants to merge 1 commit into from
Closed

Conversation

ddoroshev
Copy link

@ddoroshev ddoroshev commented Jan 3, 2024

This PR fixes the problem of generating redundant migrations in PyPy 3.10/Django 5.0 environment.

In Django 5.0 they changed the way of normalizing choices, using match.

CPython works fine with case Iterable(), so the choices always go through this list comprehension, being converted from tuples to lists every time.

PyPy 3.10-7.3.13 has a bug, it can't match case Iterable(), but works with case Iterable instead (which is wrong). So the test was failing, because the rendered schema contained choices=[..., ...], while schema generated from the models containted choices=(..., ...), which caused a redundant auto generated migration.

PyPy fixed it in 7.3.14 though.

Changelog:

Python 3.10

<...>

  • Pattern matching classes now use the full isinstance machinery, calling __instancecheck__ too. (#4036)

(The issue itself is unavailable, probably because they're moving to GitHub.)

So this small change of converting choices to lists fixes the problem.

Related issues and PRs:

(DAYS, _('Day')),
(HOURS, _('Hour')),
(MINUTES, _('Minute')),
(SECONDS, _('Second')),
(MICROSECONDS, _('Microsecond')),
)
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice work!!

Can we please change this only on PyPy and in a way that allows it to be removed after PyPy v7.3.14 is released?

Tuples take less memory and signal to the reader that these values should normally not be modified at runtime. Also, given comments in the first few episodes of the core.py podcast, tuples should perform better in the multi-GIL world of Python 3.13+.

Something like...

import platform

if platform.python_implementation() == "PyPy":  # Workaround for PyPy < v7.3.14
    PERIOD_CHOICES = list(PERIOD_CHOICES)
    SINGULAR_PERIODS = list(SINGULAR_PERIODS)

Copy link
Author

Choose a reason for hiding this comment

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

It doesn't make sense, since Django always converts them to lists under the hood

Copy link
Contributor

Choose a reason for hiding this comment

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

Then why is there a bug at all?

Copy link
Author

Choose a reason for hiding this comment

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

It's all about test_models_match_migrations that was failing in your PR https://github.com/celery/django-celery-beat/actions/runs/7267638477/job/19801878411?pr=705#step:6:2073

I think the PR description should cover the problem pretty well. If it's unclear, let me know and I'll fix it.

Copy link
Contributor

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

@auvipy auvipy closed this in #705 Jan 4, 2024
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.

2 participants