kvesteri / sqlalchemy-continuum

Versioning extension for SQLAlchemy.
BSD 3-Clause "New" or "Revised" License
568 stars 128 forks source link

Updates for SQLAlchemy >= 2.0.22 #339

Closed josecsotomorales closed 4 months ago

josecsotomorales commented 9 months ago

Seems like a breaking change was added to SQLAlchemy==2.0.22 as Continuum works ok on 2.0.21. Getting the following stack trace:

  File "/Users/Jose/Workspace/test-project/app/database.py", line 91, in initialize_continuum
    sa.orm.configure_mappers()
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 4168, in configure_mappers
    _configure_registries(_all_registries(), cascade=True)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 4202, in _configure_registries
    Mapper.dispatch._for_class(Mapper).after_configured()  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/event/attr.py", line 378, in __call__
    fn(*args, **kw)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/events.py", line 887, in wrap
    fn(*arg, **kw)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy_continuum/builder.py", line 23, in check_reentry
    handler(*args, **kwargs)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy_continuum/builder.py", line 184, in configure_versioned_classes
    self.build_models()
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy_continuum/builder.py", line 111, in build_models
    self.manager.plugins.after_build_models(self.manager)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy_continuum/plugins/base.py", line 70, in wrapper
    getattr(plugin, attr)(*args, **kwargs)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy_continuum/plugins/activity.py", line 323, in after_build_models
    self.activity_cls = ActivityFactory()(manager)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy_continuum/factory.py", line 12, in __call__
    return self.create_class(manager)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy_continuum/plugins/activity.py", line 224, in create_class
    class Activity(
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/decl_api.py", line 195, in __init__
    _as_declarative(reg, cls, dict_)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 247, in _as_declarative
    return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 328, in setup_mapping
    return _ClassScanMapperConfig(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 582, in __init__
    self._early_mapping(mapper_kw)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 369, in _early_mapping
    self.map(mapper_kw)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 1949, in map
    mapper_cls(self.cls, self.local_table, **self.mapper_args),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in __init__
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 851, in __init__
    self._configure_properties()
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 1748, in _configure_properties
    self._configure_property(
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 2230, in _configure_property
    prop.instrument_class(self)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy_utils/generic.py", line 173, in instrument_class
    attributes.register_attribute(
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 2588, in register_attribute
    register_attribute_impl(class_, key, **kw)
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 2620, in register_attribute_impl
    impl = cast("Type[WriteOnlyAttributeImpl]", impl_class)(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Jose/Workspace/test-project/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1217, in __init__
    super().__init__(*arg, **kw)
TypeError: AttributeImpl.__init__() missing 1 required positional argument: 'dispatch'
josecsotomorales commented 6 months ago

@marksteward any hints on this one? Happy to help with a PR

leberknecht commented 6 months ago

@josecsotomorales can you provide a minimal example that reproduces this? Or at least the model class?

josecsotomorales commented 6 months ago

@leberknecht can be reproduced in the unit tests, update the project to use SQLAlchemy >= 2.0.22 and 337 tests are going to fail, I'm looking at it currently but happy to get some help.

josecsotomorales commented 6 months ago

After some debugging this issue seems to affect a dependency, sqlalchemy-utils, here's the issue: https://github.com/kvesteri/sqlalchemy-utils/pull/725

josecsotomorales commented 6 months ago

Another issue: https://github.com/kvesteri/sqlalchemy-utils/issues/719

josecsotomorales commented 6 months ago

This doesn't seem related to the sqlalchemy-continuum codebase at all, will use this issue to track a fix.

marksteward commented 4 months ago

I missed this at the time, but I've pinned to SQLAlchemy < 2.0.22 for now.

josecsotomorales commented 4 months ago

SQLAlchemy-Utils 0.41.2 was released with a fix for SQLAlchemy >= 2.0.22, tested Continuum and it works on my project with SQLAlchemy 2.0.29. Will close this issue.

adrianschneider94 commented 2 months ago

@marksteward can the pin to <2.0.22 be removed?

josecsotomorales commented 2 months ago

I have a PR ready for this https://github.com/kvesteri/sqlalchemy-continuum/pull/353