]> 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:06:04 +0000 (20:06 +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
(cherry picked from commit 29895487915b8858deb2f8ac4a88d92917641c55)

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 70307ec76791a6c2fe5387bf87994fac41af8ee6..fd4828e8559614049d38ba01f1cbd973b12ac286 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 b04d6d48c28f7523b0357e2d6a86c0d5df58d2f3..d5ed61de53f41334b42c3c6d144aec286f1b987e 100644 (file)
@@ -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.
index f17717b53cc1302ebb46e049097f080307afc287..1176b504186f5f1ab66afb5aa488534106b3089e 100644 (file)
@@ -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(
index 5953062459ec3c89c1ec5d56c4464b490dcec728..b055240a353f46c3a719a0b8964084999b9c27f0 100644 (file)
@@ -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
                     )
 
index 218285cab88c4178cf19f350716e06772ae23fd1..75ad5b1ca0eb908a4555dd59001f46ed23fb9a6b 100644 (file)
@@ -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]
index 0d0bc708941bb20aa50be13303b6a951c5b3fd07..eae00338f10db62b2d76b0a47ad1de2be4537812 100644 (file)
@@ -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"
         )
index f8ad6fa6a4b3e1cde291fb5f093aa8a09403c253..ca7b2c2b59fdcfb0a7137817839d477ec646c1bd 100644 (file)
@@ -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:
index f2d165145a159386ef073013e6e7a774252ddf27..d9eaa2b388e3ac58edfefade944d200e966737e3 100644 (file)
@@ -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
index f4f292ee7ec069823a20919e256192ce886c355a..f2e6948a7ba11f612d50f495d5e1873842b800bd 100644 (file)
@@ -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(