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-Url: http://git.ipfire.org/?a=commitdiff_plain;h=29895487915b8858deb2f8ac4a88d92917641c55;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 --- diff --git a/lib/sqlalchemy/orm/clsregistry.py b/lib/sqlalchemy/orm/clsregistry.py index 9dd2ab954a..54353f3631 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 9d01886388..f00691fbc8 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -240,7 +240,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: @@ -1890,8 +1890,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 020c849257..55f5236ce3 100644 --- a/lib/sqlalchemy/orm/decl_base.py +++ b/lib/sqlalchemy/orm/decl_base.py @@ -1277,8 +1277,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 88413485c4..288d74f1c8 100644 --- a/lib/sqlalchemy/orm/dependency.py +++ b/lib/sqlalchemy/orm/dependency.py @@ -1058,7 +1058,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 6e4f1cf847..81d6d8fd12 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -872,8 +872,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 3c46d26502..b6c4cc5772 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -1811,8 +1811,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) @@ -2968,9 +2966,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 bb64bbc3f7..99b7e60125 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -4061,14 +4061,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 4471868911..2a22678870 100644 --- a/lib/sqlalchemy/orm/strategies.py +++ b/lib/sqlalchemy/orm/strategies.py @@ -1447,7 +1447,6 @@ class _ImmediateLoader(_PostLoader): alternate_effective_path = path._truncate_recursive() extra_options = (new_opt,) else: - new_opt = None alternate_effective_path = path extra_options = () @@ -2177,8 +2176,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 154f8430a9..c2a44e899e 100644 --- a/lib/sqlalchemy/orm/strategy_options.py +++ b/lib/sqlalchemy/orm/strategy_options.py @@ -1098,7 +1098,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(