GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||
GeezerVison is showing github.com — Use `__new__()` instead of `__init__()` in serializers by Viicos · Pull Request #315 · typeddjango/djangorestframework-stubs Conversation
|
|
This has been in the back of my mind also. It would be great if this works. AFAIK But I think it's possible to convince mypy developers to change this requirement. Please add a test case demonstrating whether this works or not. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
The CI errors will be fixed in #316. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Well EDIT: This is not the case for So I've added a |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ok this seems to work fine when using IntBaseSerializer = serializers.BaseSerializer[int] test_serializer = IntBaseSerializer([1, 2], many=True) reveal_type(test_serializer) # N: Revealed type is "rest_framework.serializers.ListSerializer[builtins.int]" But when defining a custom serializer: class TestSerializer(serializers.Serializer[int]): pass test_serializer = TestSerializer(instance=[1, 2], many=True) reveal_type(test_serializer) # N: Revealed type is "main.TestSerializer" We get the wrong type, possibly because mypy isn't using the I did some tests locally, and it seems that this issue is specific to mypy: from __future__ import annotations class A: def __new__(cls) -> B: return B() class B(A): pass class C(A): pass a = C() reveal_type(a) # Revealed type is "C", but got B with pyright (Pylance) I'll open a ticket |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
overload definition is subject to change, as it is missing some arguments.
|
I found there's already an open mypy issue about this: python/mypy#8330 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I think this is now unblocked via python/mypy#21441 as of mypy 2.1 or 2.2 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
# Conflicts: # rest_framework-stubs/serializers.pyi # tests/typecheck/test_serializers.yml
# Conflicts: # rest_framework-stubs/serializers.pyi
There was an error while loading. Please reload this page.
mypy_primerCompared DRF stubs and plugin at d7ece7f with its target-branch parent. Differences are advisory. Python 3.11 No diagnostic differences. Python 3.12 No diagnostic differences. Python 3.13 No diagnostic differences. Python 3.14 No diagnostic differences. Python 3.15 No diagnostic differences. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
__new__ instead of __init__ in serializers__new__() instead of __init__() in serializers
There was an error while loading. Please reload this page.
|
We unfortunately still need to work around Mypy not following the spec, but the workaround is straightforward. I'm not sure if this is safe to get, this may create regressions for users subclassing the serializers and overridding |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I just merged CI pyright checking to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Incompatible type withmany=True
There was an error while loading. Please reload this page.