mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Refs #32074 -- Backported Enum.__repr__() from Python 3.10.
Enum.__repr__() was changed in [1], we should use the same format in Python < 3.10. [1] https://bugs.python.org/issue40066
This commit is contained in:
		| @@ -4,6 +4,7 @@ from django.db.models.expressions import ExpressionList, F | ||||
| from django.db.models.indexes import IndexExpression | ||||
| from django.db.models.query_utils import Q | ||||
| from django.db.models.sql.query import Query | ||||
| from django.utils.version import PY310 | ||||
|  | ||||
| __all__ = ['CheckConstraint', 'Deferrable', 'UniqueConstraint'] | ||||
|  | ||||
| @@ -85,6 +86,11 @@ class Deferrable(Enum): | ||||
|     DEFERRED = 'deferred' | ||||
|     IMMEDIATE = 'immediate' | ||||
|  | ||||
|     # A similar format is used in Python 3.10+. | ||||
|     if not PY310: | ||||
|         def __repr__(self): | ||||
|             return '%s.%s' % (self.__class__.__qualname__, self._name_) | ||||
|  | ||||
|  | ||||
| class UniqueConstraint(BaseConstraint): | ||||
|     def __init__( | ||||
| @@ -218,7 +224,7 @@ class UniqueConstraint(BaseConstraint): | ||||
|             '' if not self.expressions else ' expressions=%s' % repr(self.expressions), | ||||
|             ' name=%s' % repr(self.name), | ||||
|             '' if self.condition is None else ' condition=%s' % self.condition, | ||||
|             '' if self.deferrable is None else ' deferrable=%s' % self.deferrable, | ||||
|             '' if self.deferrable is None else ' deferrable=%r' % self.deferrable, | ||||
|             '' if not self.include else ' include=%s' % repr(self.include), | ||||
|             '' if not self.opclasses else ' opclasses=%s' % repr(self.opclasses), | ||||
|         ) | ||||
|   | ||||
| @@ -2,6 +2,7 @@ import enum | ||||
| from types import DynamicClassAttribute | ||||
|  | ||||
| from django.utils.functional import Promise | ||||
| from django.utils.version import PY310 | ||||
|  | ||||
| __all__ = ['Choices', 'IntegerChoices', 'TextChoices'] | ||||
|  | ||||
| @@ -74,6 +75,11 @@ class Choices(enum.Enum, metaclass=ChoicesMeta): | ||||
|         """ | ||||
|         return str(self.value) | ||||
|  | ||||
|     # A similar format is used in Python 3.10+. | ||||
|     if not PY310: | ||||
|         def __repr__(self): | ||||
|             return '%s.%s' % (self.__class__.__qualname__, self._name_) | ||||
|  | ||||
|  | ||||
| class IntegerChoices(int, Choices): | ||||
|     """Class for creating enumerated integer choices.""" | ||||
|   | ||||
| @@ -48,7 +48,7 @@ class ChoicesTests(SimpleTestCase): | ||||
|         self.assertEqual(Suit.values, [1, 2, 3, 4]) | ||||
|         self.assertEqual(Suit.names, ['DIAMOND', 'SPADE', 'HEART', 'CLUB']) | ||||
|  | ||||
|         self.assertEqual(repr(Suit.DIAMOND), '<Suit.DIAMOND: 1>') | ||||
|         self.assertEqual(repr(Suit.DIAMOND), 'Suit.DIAMOND') | ||||
|         self.assertEqual(Suit.DIAMOND.label, 'Diamond') | ||||
|         self.assertEqual(Suit.DIAMOND.value, 1) | ||||
|         self.assertEqual(Suit['DIAMOND'], Suit.DIAMOND) | ||||
| @@ -89,7 +89,7 @@ class ChoicesTests(SimpleTestCase): | ||||
|         self.assertEqual(YearInSchool.values, ['FR', 'SO', 'JR', 'SR', 'GR']) | ||||
|         self.assertEqual(YearInSchool.names, ['FRESHMAN', 'SOPHOMORE', 'JUNIOR', 'SENIOR', 'GRADUATE']) | ||||
|  | ||||
|         self.assertEqual(repr(YearInSchool.FRESHMAN), "<YearInSchool.FRESHMAN: 'FR'>") | ||||
|         self.assertEqual(repr(YearInSchool.FRESHMAN), 'YearInSchool.FRESHMAN') | ||||
|         self.assertEqual(YearInSchool.FRESHMAN.label, 'Freshman') | ||||
|         self.assertEqual(YearInSchool.FRESHMAN.value, 'FR') | ||||
|         self.assertEqual(YearInSchool['FRESHMAN'], YearInSchool.FRESHMAN) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user