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

main #792

Closed
wants to merge 88 commits into from
Closed

main #792

wants to merge 88 commits into from

Conversation

TonyRippy
Copy link

Previously the types required a mypy plugin to handle the generics, now
we're using the builtin typing functionality. Probably more verbose, but
it should work with type checkers besides mypy.

Also remove `objects: BaseManager[Any]` from the base `Model` so
subclasses will be required to explicitly declare it. Avoids uses having
`.filter().first()` returning `Optional[Any]` instead of `Optional[T]`.
Bunch of misc changes:
- Support for PG fields
- Basic Django project to type check model fields
- Nullability for most fields via the `null` argument.
Add basic psycopg2 type stubs and type more of django's raw sql related
methods, e.g.:

```
with connection.cursor() as cur:
    cur.execute("select 1")
```

Add some other dev dependencies to make figuring out the types easier.
Param should probably be `Sequence[Any] | Dict[str, Any]` but I'm trying
to see how far I can get before I have to do that.
Key change is to also fix HttpHeaders to such that `.get()` returns `str | None` instead of `Any | None`.
Poetry defaults to the system version, 3.7 in this case, which was preventing
users of 3.6 from installing the package.
sbdchd and others added 28 commits July 16, 2021 13:14
* Do not recomment using django-stubs-ext anymore, it is confusing since
  it was created for django-stubs itself
* Add `ForeignKey` to the list of classes that needs to be patched
* Improve documentation for `ForeignKey` and `RelatedManager` typing
We may need to loosen these, but as a default they should work pretty
well. Previously we typed the global cache as `Any`, but `BaseCache`
should work and has the various methods defined as well.

sbdchd#46
With this change we should be able to get `Foo.objects` to return `Manager[Foo]` without having to manually type it.
Now that Kodiak is using the latest poetry using any of the other projects on the old version is a pain.

So upgrade time!
Newer version of poetry handles system links differently breaking
the build.

related: sbdchd/celery-types#40
Django 3.1 updated these db deletion errors to show the set of all objects that protected/restricted the deletion, as opposed to just the model of one object. 

As such, these stubs need to be updated. This matches how these errors are typed in `typeddjango`.
This was built on top of #62, so merging it should only keep this PR's change
I do not know if there was a particular reason for why it was not implemented before. But, I took the liberty to mimic what was done for similar fields on the upstream.

Essentially, The `__init__` was overloaded to distinguish between `null=True` and `null=False` to signal the resulting type, `Optional[date]` or `date` respectively.
This was built on top of sbdchd#73, so merging it should only keep this PR's change

Fix sbdchd#51
This was built on top of sbdchd#63, so merging it should only keep this PR's change

Note that even though the change looks big, it actually is pretty small, it is just that all fields need to define their own `__new__` (or `__init__` if their signature is different from the base field), and it is all the same, the difference is the classname and the generic field. I also added some documentation [here](https://github.com/sbdchd/django-types/pull/65/files#diff-876a5d116ef70c86039fbb8498623871324fa93fc1ae51c7c09ba2a0f7612e99R47) explaining the usage.

Fix sbdchd#34
django/django#12405 (comment)

QuerySet and BaseManager already have __class_getitem__, starting from Django 3.1
Just upgraded to 0.11.0 and notice some errors on my migrations. There's no need to set covariant/contravariant on _GT/_ST.

The tests are just there to make sure that there are no errors anymore. If the code is reverted the test would fail
I think that it was removed in one of the recent PRs for improving `ManyToManyField` typing.
…ny-to-many relationships.

From the docs for `add()`:
https://docs.djangoproject.com/en/4.0/ref/models/relations/#django.db.models.fields.related.RelatedManager.add

> For many-to-many relationships add() accepts either model instances or field values, normally primary keys, as the *objs argument.

Likewise for `remove()`:
https://docs.djangoproject.com/en/4.0/ref/models/relations/#django.db.models.fields.related.RelatedManager.remove

> For many-to-many relationships remove() accepts either model instances or field values, normally primary keys, as the *objs argument.
@TonyRippy
Copy link
Author

Whoa. Something is seriously messed up with this PR, my apologies.

@TonyRippy TonyRippy closed this Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.