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

Find a way to split types by Django version #1244

Open
adamchainz opened this issue Nov 9, 2022 · 6 comments
Open

Find a way to split types by Django version #1244

adamchainz opened this issue Nov 9, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@adamchainz
Copy link
Contributor

Mypy supports splitting types by Python versions by interpreting if sys.version_info ... checks. But we have no mechanism to conditionally split types by Django version. This has lead to lots of small issues where Django’s API has changed and we cannot have correct types for all versions we attempt to support.

To counter this problem, @sobolevn suggested doing releases per Django version, where we only officially support the latest version: #1095 (comment) . This is going to be the easiest for the time being, but if we can find a mechanism to split types by Django version, we could backtrack on this decision.

Related Mypy issue: python/typing#693

@ngnpope investigated a possibility with literal numerical types: #1160 (comment) . If we could supply the literal numerical type for the Django version from the MYpy plugin, then perhaps Mypy could use the type of that for conditions in stub files.

@intgr intgr added the enhancement New feature or request label Nov 9, 2022
@intgr
Copy link
Collaborator

intgr commented Nov 11, 2022

There was some earlier discussion about supporting multiple Django versions in #262 and draft PR in #288.

@adamchainz
Copy link
Contributor Author

Parsing the stubs into multiple versioned, committed files is an interesting approach. We may not even need something as complex as libcst to do it, a few choice regexes could work, or the Cog tool for processing text with inline Python.

@Viicos
Copy link
Contributor

Viicos commented Dec 15, 2023

Having conditional checks against the Django version is interesting but might be verbose e.g. for newly added arguments. Consider the new db_default parameter, it would require the following:

if DJANGO_5:
    def __init__(
            self,
            # All the other arguments
            db_default: type[NOT_PROVIDED] | Expression | _ST = ...,
    ): ...
else:
    # The same `__init__` method without `db_default`

And this for all Django fields.

As the django-stubs versioning seems to now be inline with the Django versions, maybe we could consider the 4.2.x releases to only support Django 4.2, and the next stubs release (presumably 5.x) to be inline with Django 5. For each addition on the main branch, we could backport changes to a 4.2.x branch if the addition isn't Django 5 only.

@intgr
Copy link
Collaborator

intgr commented Dec 17, 2023

For each addition on the main branch, we could backport changes to a 4.2.x branch if the addition isn't Django 5 only.

It's possible, but personally I have no interest in the maintenance work involved: backporting, maintaining old branches and doing twice the releases. If someone else were to handle it, maybe, but I suspect it would still affect the rest of maintainers in one way or another.

My projects using django-stubs are have been one Django release behind for a while and I don't think I ever had any typing issues due to differing APIs between Django versions. And I can only remember one issue reported to us about this. My impression is that Django public APIs change rarely enough that this tends not to be an issue.

With the example db_default=, Django 4.2 users can just as well use 5.0 stubs and simply avoid using db_default=. Maybe someone can bring up better examples, but this example doesn't seem worth the trouble of maintaining multiple versions.

@Viicos
Copy link
Contributor

Viicos commented Dec 18, 2023

My impression is that Django public APIs change rarely enough that this tends not to be an issue.

You're right, considering this it might not be worth maintaining two branches. Maybe in the future this would be a builtin feature when writing stubs.

@sobolevn
Copy link
Member

Looks like we follow DjangoVer now: https://www.b-list.org/weblog/2024/nov/18/djangover/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

4 participants