From: Shamil Date: Fri, 18 Apr 2025 09:51:29 +0000 (+0300) Subject: refactor: remove unused variables and simplify key lookups X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7c9be40485e90bd377cc74723ccce6b35ea8b56;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git refactor: 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. --- diff --git a/lib/sqlalchemy/orm/clsregistry.py b/lib/sqlalchemy/orm/clsregistry.py index 9dd2ab954a..4c9e38b6bf 100644 --- a/lib/sqlalchemy/orm/clsregistry.py +++ b/lib/sqlalchemy/orm/clsregistry.py @@ -69,11 +69,11 @@ def _add_class( """ if classname in decl_class_registry: - # class already exists. - existing = decl_class_registry[classname] - if not isinstance(existing, _MultipleClassMarker): - existing = decl_class_registry[classname] = _MultipleClassMarker( - [cls, cast("Type[Any]", existing)] + if not isinstance( + decl_class_registry[classname], _MultipleClassMarker + ): + decl_class_registry[classname] = _MultipleClassMarker( + [cls, cast("Type[Any]", decl_class_registry[classname])] ) else: decl_class_registry[classname] = cls @@ -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/identity.py b/lib/sqlalchemy/orm/identity.py index fe1164d57c..60dbfa4052 100644 --- a/lib/sqlalchemy/orm/identity.py +++ b/lib/sqlalchemy/orm/identity.py @@ -160,19 +160,14 @@ class _WeakInstanceDict(IdentityMap): self, state: InstanceState[Any] ) -> Optional[InstanceState[Any]]: assert state.key is not None - if state.key in self._dict: - try: - existing = existing_non_none = self._dict[state.key] - except KeyError: - # catch gc removed the key after we just checked for it - existing = None - else: - if existing_non_none is not state: - self._manage_removed_state(existing_non_none) - else: - return None - else: + try: + existing = self._dict[state.key] + except KeyError: existing = None + else: + if existing is state: + return None + self._manage_removed_state(existing) self._dict[state.key] = state self._manage_incoming_state(state) 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(