summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util/langhelpers.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-11-09 18:41:54 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2022-11-10 12:19:02 -0500
commite3a8d198917f4246365e09fa975d55c64082cd2e (patch)
tree19c47960c4ab212a59528e0f8dedf6d3a77b557f /lib/sqlalchemy/util/langhelpers.py
parentebb54e80a5a52d0cce4cba1abc21c707a42c2c73 (diff)
downloadsqlalchemy-e3a8d198917f4246365e09fa975d55c64082cd2e.tar.gz
work around Python 3.11 IntEnum issue; update FastIntFlag
in [1], Python 3.11 seems to have changed the behavior of IntEnum. We didn't notice this because we have our own workaround class already, but typing did. Ensure we remain compatible with IntFlag. This change also modifies FastIntFlag to no longer use global symbols; this is unnecessary as we assign FastIntFlag members explicitly. Use of ``symbol()`` should probably be phased out. [1] https://github.com/python/cpython/issues/99304 Fixes: #8783 Change-Id: I8ae2e871ff1467ae5ca1f63e66b5dae45d4a6c93
Diffstat (limited to 'lib/sqlalchemy/util/langhelpers.py')
-rw-r--r--lib/sqlalchemy/util/langhelpers.py25
1 files changed, 19 insertions, 6 deletions
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py
index d8d39f56c..051a8c89e 100644
--- a/lib/sqlalchemy/util/langhelpers.py
+++ b/lib/sqlalchemy/util/langhelpers.py
@@ -1603,11 +1603,6 @@ class symbol(int):
Repeated calls of symbol('name') will all return the same instance.
- In SQLAlchemy 2.0, symbol() is used for the implementation of
- ``_FastIntFlag``, but otherwise should be mostly replaced by
- ``enum.Enum`` and variants.
-
-
"""
name: str
@@ -1632,7 +1627,17 @@ class symbol(int):
if doc:
sym.__doc__ = doc
+ # NOTE: we should ultimately get rid of this global thing,
+ # however, currently it is to support pickling. The best
+ # change would be when we are on py3.11 at a minimum, we
+ # switch to stdlib enum.IntFlag.
cls.symbols[name] = sym
+ else:
+ if canonical and canonical != sym:
+ raise TypeError(
+ f"Can't replace canonical symbol for {name} "
+ f"with new int value {canonical}"
+ )
return sym
def __reduce__(self):
@@ -1665,8 +1670,16 @@ class _IntFlagMeta(type):
setattr(cls, k, sym)
items.append(sym)
+ cls.__members__ = _collections.immutabledict(
+ {sym.name: sym for sym in items}
+ )
+
def __iter__(self) -> Iterator[symbol]:
- return iter(self._items)
+ raise NotImplementedError(
+ "iter not implemented to ensure compatibility with "
+ "Python 3.11 IntFlag. Please use __members__. See "
+ "https://github.com/python/cpython/issues/99304"
+ )
class _FastIntFlag(metaclass=_IntFlagMeta):