From: Federico Caselli Date: Thu, 24 Apr 2025 22:02:32 +0000 (-0400) Subject: refactor (orm): remove unused variables and simplify key lookups X-Git-Tag: rel_2_0_41~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6f850bd4e58e401d75c1b622a0f088c4eb9deb60;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git refactor (orm): remove unused variables and simplify key lookups Redundant variables and unnecessary conditions were removed across several modules. Improved readability and reduced code complexity without changing functionality. Closes: #12537 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12537 Pull-request-sha: ab53f8c3487e8cfb4d4a0235c27d8a5b8557d193 Change-Id: I910d65729fdbc96933f9822c553924d37e89e201 (cherry picked from commit 29895487915b8858deb2f8ac4a88d92917641c55) --- diff --git a/lib/sqlalchemy/orm/clsregistry.py b/lib/sqlalchemy/orm/clsregistry.py index 70307ec767..fd4828e855 100644 --- a/lib/sqlalchemy/orm/clsregistry.py +++ b/lib/sqlalchemy/orm/clsregistry.py @@ -72,7 +72,7 @@ def add_class( # class already exists. existing = decl_class_registry[classname] if not isinstance(existing, _MultipleClassMarker): - existing = decl_class_registry[classname] = _MultipleClassMarker( + decl_class_registry[classname] = _MultipleClassMarker( [cls, cast("Type[Any]", existing)] ) else: @@ -317,7 +317,7 @@ class _ModuleMarker(ClsRegistryToken): else: raise else: - existing = self.contents[name] = _MultipleClassMarker( + self.contents[name] = _MultipleClassMarker( [cls], on_remove=lambda: self._remove_item(name) ) diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py index b04d6d48c2..d5ed61de53 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -231,7 +231,7 @@ class AbstractORMCompileState(CompileState): if compiler is None: # this is the legacy / testing only ORM _compile_state() use case. # there is no need to apply criteria options for this. - self.global_attributes = ga = {} + self.global_attributes = {} assert toplevel return else: @@ -1864,8 +1864,6 @@ class ORMSelectCompileState(ORMCompileState, SelectState): "selectable/table as join target" ) - of_type = None - if isinstance(onclause, interfaces.PropComparator): # descriptor/property given (or determined); this tells us # explicitly what the expected "left" side of the join is. diff --git a/lib/sqlalchemy/orm/decl_base.py b/lib/sqlalchemy/orm/decl_base.py index f17717b53c..1176b50418 100644 --- a/lib/sqlalchemy/orm/decl_base.py +++ b/lib/sqlalchemy/orm/decl_base.py @@ -1296,8 +1296,6 @@ class _ClassScanMapperConfig(_MapperConfig): or isinstance(attr_value, _MappedAttribute) ) ) - else: - is_dataclass_field = False is_dataclass_field = False extracted = _extract_mapped_subtype( diff --git a/lib/sqlalchemy/orm/dependency.py b/lib/sqlalchemy/orm/dependency.py index 5953062459..b055240a35 100644 --- a/lib/sqlalchemy/orm/dependency.py +++ b/lib/sqlalchemy/orm/dependency.py @@ -1054,7 +1054,7 @@ class ManyToManyDP(DependencyProcessor): # so that prop_has_changes() returns True for state in states: if self._pks_changed(uowcommit, state): - history = uowcommit.get_attribute_history( + uowcommit.get_attribute_history( state, self.key, attributes.PASSIVE_OFF ) diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 218285cab8..75ad5b1ca0 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -847,8 +847,6 @@ class MappedColumn( ) if sqltype._isnull and not self.column.foreign_keys: - new_sqltype = None - checks: List[Any] if our_type_is_pep593: checks = [our_type, raw_pep_593_type] diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 0d0bc70894..eae00338f1 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -1758,8 +1758,6 @@ class RelationshipProperty( extracted_mapped_annotation: Optional[_AnnotationScanType], is_dataclass_field: bool, ) -> None: - argument = extracted_mapped_annotation - if extracted_mapped_annotation is None: if self.argument is None: self._raise_for_required(key, cls) @@ -2912,9 +2910,6 @@ class JoinCondition: ) -> None: """Check the foreign key columns collected and emit error messages.""" - - can_sync = False - foreign_cols = self._gather_columns_with_annotation( join_condition, "foreign" ) diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index f8ad6fa6a4..ca7b2c2b59 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -4014,14 +4014,7 @@ class Session(_SessionClassMethods, EventTarget): else: key_is_persistent = True - if key in self.identity_map: - try: - merged = self.identity_map[key] - except KeyError: - # object was GC'ed right as we checked for it - merged = None - else: - merged = None + merged = self.identity_map.get(key) if merged is None: if key_is_persistent and key in _resolve_conflict_map: diff --git a/lib/sqlalchemy/orm/strategies.py b/lib/sqlalchemy/orm/strategies.py index f2d165145a..d9eaa2b388 100644 --- a/lib/sqlalchemy/orm/strategies.py +++ b/lib/sqlalchemy/orm/strategies.py @@ -1435,7 +1435,6 @@ class ImmediateLoader(PostLoader): alternate_effective_path = path._truncate_recursive() extra_options = (new_opt,) else: - new_opt = None alternate_effective_path = path extra_options = () @@ -2165,8 +2164,6 @@ class JoinedLoader(AbstractRelationshipLoader): path = path[self.parent_property] - with_polymorphic = None - user_defined_adapter = ( self._init_user_defined_eager_proc( loadopt, compile_state, compile_state.attributes diff --git a/lib/sqlalchemy/orm/strategy_options.py b/lib/sqlalchemy/orm/strategy_options.py index f4f292ee7e..f2e6948a7b 100644 --- a/lib/sqlalchemy/orm/strategy_options.py +++ b/lib/sqlalchemy/orm/strategy_options.py @@ -1103,7 +1103,6 @@ class Load(_AbstractLoad): """ path = self.path - ezero = None for ent in mapper_entities: ezero = ent.entity_zero if ezero and orm_util._entity_corresponds_to(