Skip to content

Commit 85ffc91

Browse files
authored
Document intentional discrepancies in allowlist & code comments (#833)
A few type hints are deliberately not aligned with implementation signatures. Document why this is.
1 parent 89df910 commit 85ffc91

File tree

6 files changed

+18
-6
lines changed

6 files changed

+18
-6
lines changed

rest_framework-stubs/fields.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ from typing_extensions import Self
1919
class _Empty(Enum):
2020
sentinel = 0
2121

22+
# DISREPANCY: `empty` hinted as enum, to work correctly in unions:
23+
# https://github.com/typeddjango/djangorestframework-stubs/issues/42
2224
empty: Final = _Empty.sentinel
2325

2426
class BuiltinSignatureError(Exception): ...

rest_framework-stubs/relations.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class SlugRelatedField(RelatedField[_MT, str, str]):
138138
slug_field: str
139139
def __init__(
140140
self,
141+
# DISCREPANCY: signature defaults `slug_field=None`, but actually crashes when `None` is provided.
141142
slug_field: str,
142143
*,
143144
many: bool = ...,
@@ -169,7 +170,8 @@ class ManyRelatedField(Field[Sequence[Any], Sequence[Any], list[Any], Any]):
169170
allow_empty: bool
170171
def __init__(
171172
self,
172-
child_relation: RelatedField = ...,
173+
# DISCREPANCY: signature defaults `child_relation=None`, but actually crashes when `None` is provided.
174+
child_relation: RelatedField,
173175
*,
174176
read_only: bool = ...,
175177
write_only: bool = ...,

rest_framework-stubs/renderers.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ from rest_framework.views import APIView
1313
def zero_as_none(value: Any) -> Any | None: ...
1414

1515
class BaseRenderer:
16+
# DISCREPANCY: `media_type`, `format` cannot be None.
17+
# None is a placeholder, but all subclasses must override this to `str`.
1618
media_type: str
1719
format: str
1820
charset: str | None

rest_framework-stubs/validators.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ProhibitSurrogateCharactersValidator:
5959
def __call__(self, value: Any) -> None: ...
6060

6161
class BaseUniqueForValidator:
62+
# DISCREPANCY: `message` cannot be None -- None is a placeholder, but subclasses must override this to StrOrPromise
6263
message: StrOrPromise
6364
missing_message: StrOrPromise
6465
requires_context: bool

scripts/stubtest/allowlist.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ rest_framework.relations.SlugRelatedField.__init__
1111
rest_framework.serializers.ManyRelatedField.__init__
1212
rest_framework.serializers.SlugRelatedField.__init__
1313

14+
# None is used as a placeholder, but not valid in actual usage
15+
rest_framework.renderers.BaseRenderer.format
16+
rest_framework.renderers.BaseRenderer.media_type
17+
rest_framework.validators.BaseUniqueForValidator.message
18+
1419
# Migrations are omitted
1520
rest_framework.authtoken.migrations
1621
rest_framework.authtoken.migrations.0001_initial
1722
rest_framework.authtoken.migrations.0002_auto_20160226_1747
1823
rest_framework.authtoken.migrations.0003_tokenproxy
1924
rest_framework.authtoken.migrations.0004_alter_tokenproxy_options
2025

26+
# `empty` hinted as enum, to work correctly in unions:
27+
# https://github.com/typeddjango/djangorestframework-stubs/issues/42
28+
rest_framework.fields.empty
29+
rest_framework.serializers.empty
30+
2131
# Mypy doesn't like default value `empty`
2232
rest_framework.fields.DateField.__init__
2333
rest_framework.fields.DateTimeField.__init__

scripts/stubtest/allowlist_todo.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ rest_framework.fields.Option
4141
rest_framework.fields.REGEX_TYPE
4242
rest_framework.fields.SupportsToPython
4343
rest_framework.fields.TimeField.to_internal_value
44-
rest_framework.fields.empty
4544
rest_framework.generics.BaseFilterProtocol
4645
rest_framework.generics.UsesQuerySet
4746
rest_framework.negotiation.DefaultContentNegotiation.settings
@@ -51,8 +50,6 @@ rest_framework.parsers.BaseParser.media_type
5150
rest_framework.parsers.FileUploadParser.get_encoded_filename
5251
rest_framework.relations.ManyRelatedField.initial
5352
rest_framework.relations.ManyRelatedField.to_representation
54-
rest_framework.renderers.BaseRenderer.format
55-
rest_framework.renderers.BaseRenderer.media_type
5653
rest_framework.renderers.BrowsableAPIRenderer.get_extra_actions
5754
rest_framework.renderers.CoreJSONRenderer.render
5855
rest_framework.renderers._BaseOpenAPIRenderer.render
@@ -91,15 +88,13 @@ rest_framework.serializers.PermissionDenied
9188
rest_framework.serializers.Throttled
9289
rest_framework.serializers.TimeField.to_internal_value
9390
rest_framework.serializers.UnsupportedMediaType
94-
rest_framework.serializers.empty
9591
rest_framework.settings.DefaultsSettings
9692
rest_framework.settings.api_settings
9793
rest_framework.test.CoreAPIClient
9894
rest_framework.test.APIClient.options
9995
rest_framework.test.RequestsClient.__init__
10096
rest_framework.throttling.SimpleRateThrottle.cache
10197
rest_framework.utils.encoders.JSONEncoder.default
102-
rest_framework.validators.BaseUniqueForValidator.message
10398
rest_framework.validators.ContextValidator
10499
rest_framework.validators.Validator
105100
rest_framework.views.APIView.metadata_class

0 commit comments

Comments
 (0)