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

SerializerMethodField can no longer be hinted with Optional types #385

Closed
chaosk opened this issue Jun 14, 2019 · 1 comment
Closed

SerializerMethodField can no longer be hinted with Optional types #385

chaosk opened this issue Jun 14, 2019 · 1 comment

Comments

@chaosk
Copy link

chaosk commented Jun 14, 2019

Hello!

It seems that a recent change in 753be1a made it so SerializerMethodField methods can no longer be annotated with typing.Optional.

Used versions:

  • python 3.7.2
  • django 2.2.2
  • djangorestframework 3.9.4
  • drf-yasg 1.16.0

Example code:

class PetSerializer(serializers.ModelSerializer):
    human_age = serializers.SerializerMethodField()

    def get_human_age(self, obj) -> Optional[int]:
        if obj.human_age_multiplier is None:
            return None
        return math.floor(obj.age * obj.human_age_multiplier)

    class Meta:
        model = Pet

Expected result:

human_age:
  title: Human age
  type: integer
  readOnly: true
  x-nullable: true

Actual result:

human_age:
  title: Human age
  type: string
  readOnly: true
@axnsan12
Copy link
Owner

Should be fixed by merged #428

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants