9

I recently came across this code from a stackoverflow question:

@unique
class NetlistKind(IntEnum):
  Unknown = 0
  LatticeNetlist = 1
  QuartusNetlist = 2
  XSTNetlist = 4
  CoreGenNetlist = 8
  All = 15

What does the @unique decorator do, and what is its purpose in this code snippet?

Alec
  • 6,521
  • 7
  • 23
  • 48

1 Answers1

15

From the documentation:

[@unique is] a class decorator specifically for enumerations. It searches an enumeration’s members gathering any aliases it finds; if any are found ValueError is raised with the details

Basically, it raises an error if there are any duplicate enumeration values.

This code

 class Mistake(Enum):
     ONE = 1
     TWO = 2
     THREE = 3
     FOUR = 3

Produces this error:

ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
Alec
  • 6,521
  • 7
  • 23
  • 48