2

Given this model

from django.db import models

class Olympian(models.Model):
    MedalType = models.TextChoices('MedalType', 'GOLD SILVER BRONZE')
    medal = models.CharField(max_length=6, choices=MedalType.choices, default=MedalType.GOLD)

and this function which takes in the CharField as a param

fn_with_type_hint(olympian.medal)

How can I type hint the param more strictly without hard-coding like this?

def fn_with_type_hint(medal: Literal['Gold', 'Silver', 'Bronze']):
    pass

What I tried

I tried Olympian.medal but it's just a string

medal: Olympian.medal

I also tried variations of this to no avail

medal: Literal[*Olympian.MedalType.values]

I also can't use this solution because I don't start with a list of strings

https://stackoverflow.com/a/64522240/8479344

Fullchee Zhang
  • 489
  • 5
  • 13

0 Answers0