]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
refactor (orm): remove unused variables and simplify key lookups
authorFederico Caselli <cfederico87@gmail.com>
Thu, 24 Apr 2025 22:02:32 +0000 (18:02 -0400)
committerFederico Caselli <cfederico87@gmail.com>
Sun, 27 Apr 2025 18:04:55 +0000 (20:04 +0200)
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

lib/sqlalchemy/orm/clsregistry.py
lib/sqlalchemy/orm/context.py
lib/sqlalchemy/orm/decl_base.py
lib/sqlalchemy/orm/dependency.py
lib/sqlalchemy/orm/properties.py
lib/sqlalchemy/orm/relationships.py
lib/sqlalchemy/orm/session.py
lib/sqlalchemy/orm/strategies.py
lib/sqlalchemy/orm/strategy_options.py

index 9dd2ab954a2387652ec854d0262187568f3ac62a..54353f3631b7394259008cfaf0810a7ba9dc7f58 100644 (file)
@@ -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)
             )
 
index 9d01886388fe03f5526a137a11394ed5fe49805c..f00691fbc8956bcec2faa0b531018eaef24a47c9 100644 (file)
@@ -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.
index 020c8492579c6e7de2d3a58303dc0c5b51f737be..55f5236ce3c32391556ebbee21d89fa19efaf690 100644 (file)
@@ -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(
index 88413485c4cf2956b0bbb75a9225148f1d3bccb4..288d74f1c853098c57d19457791f5049e0387cae 100644 (file)
@@ -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
                     )
 
index 6e4f1cf8470c86c6782cff72799225db904e5592..81d6d8fd12345c17aba01899039f522ca09e6120 100644 (file)
@@ -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]
index 3c46d26502af952e14858a772aafbe431c476432..b6c4cc57727cf3e37d6985a3fc08e9b0fb7f4060 100644 (file)
@@ -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"
         )
index bb64bbc3f7674754da4b390c3d5e4a433e362563..99b7e60125281b06a118e2bdefe726035e255ad6 100644 (file)
@@ -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:
index 44718689115c4df9dc5677100bd846da4676bae7..2a226788706e019085ab4814a90d702d4e866004 100644 (file)
@@ -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
index 154f8430a91470056e8fd6f1e4c24bb12a106005..c2a44e899e8cd487be6e37a6cd02264d4a057d3f 100644 (file)
@@ -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(