typeddjango / djangorestframework-stubs

PEP-484 stubs for django-rest-framework
MIT License
452 stars 118 forks source link

Export imported modules in serializers.pyi #599

Closed SukiCZ closed 7 months ago

SukiCZ commented 7 months ago

Bug report

What's wrong

When using validators in serializers, mypy raises an error

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        validators = [
            serializers.UniqueTogetherValidator(
                queryset=User.objects.all(),
                fields=["username"],
            ),
            serializers.UniqueForDateValidator(
                queryset=User.objects.all(),
                field="email",
                date_field="date_joined",
            ),
        ]
error: Module "rest_framework.serializers" does not explicitly export attribute "UniqueTogetherValidator"  [attr-defined]
error: Module has no attribute "UniqueForDateValidator"  [attr-defined]

How is that should be

According to PEP-484 Stub Files

Modules and variables imported into the stub are not considered exported from the stub unless the import uses the import ... as ... form or the equivalent from ... import ... as ... form.

The modules needs to be explicitly exported and the exports should be aligned with rest_framework/serielizers

from rest_framework.validators import UniqueTogetherValidator as UniqueTogetherValidator

I'll happily open a PR in a bit

System information

SukiCZ commented 7 months ago

I've got this part wrong

the exports should be aligned with rest_framework/serielizers

Only fields and relations should be exported as suggested by comment 5 lines below