ソースを参照

style: prefer type() over __class__ (#4512)

benedikt-bartscher 5 ヶ月 前
コミット
3ef7106e0e

+ 1 - 1
reflex/app.py

@@ -1157,7 +1157,7 @@ class App(MiddlewareMixin, LifespanMixin):
             if hasattr(handler_fn, "__name__"):
                 _fn_name = handler_fn.__name__
             else:
-                _fn_name = handler_fn.__class__.__name__
+                _fn_name = type(handler_fn).__name__
 
             if isinstance(handler_fn, functools.partial):
                 raise ValueError(

+ 2 - 2
reflex/components/component.py

@@ -161,7 +161,7 @@ class ComponentNamespace(SimpleNamespace):
         Returns:
             The hash of the namespace.
         """
-        return hash(self.__class__.__name__)
+        return hash(type(self).__name__)
 
 
 def evaluate_style_namespaces(style: ComponentStyle) -> dict:
@@ -2565,7 +2565,7 @@ class LiteralComponentVar(CachedVarOperation, LiteralVar, ComponentVar):
         Returns:
             The hash of the var.
         """
-        return hash((self.__class__.__name__, self._js_expr))
+        return hash((type(self).__name__, self._js_expr))
 
     @classmethod
     def create(

+ 2 - 2
reflex/components/core/cond.py

@@ -49,9 +49,9 @@ class Cond(MemoizationLeaf):
             The conditional component.
         """
         # Wrap everything in fragments.
-        if comp1.__class__.__name__ != "Fragment":
+        if type(comp1).__name__ != "Fragment":
             comp1 = Fragment.create(comp1)
-        if comp2 is None or comp2.__class__.__name__ != "Fragment":
+        if comp2 is None or type(comp2).__name__ != "Fragment":
             comp2 = Fragment.create(comp2) if comp2 else Fragment.create()
         return Fragment.create(
             cls(

+ 1 - 3
reflex/components/radix/themes/base.py

@@ -139,9 +139,7 @@ class RadixThemesComponent(Component):
         component = super().create(*children, **props)
         if component.library is None:
             component.library = RadixThemesComponent.__fields__["library"].default
-        component.alias = "RadixThemes" + (
-            component.tag or component.__class__.__name__
-        )
+        component.alias = "RadixThemes" + (component.tag or type(component).__name__)
         # value = props.get("value")
         # if value is not None and component.alias == "RadixThemesSelect.Root":
         #     lv = LiteralVar.create(value)

+ 2 - 2
reflex/event.py

@@ -1556,7 +1556,7 @@ class LiteralEventVar(VarOperationCall, LiteralVar, EventVar):
         Returns:
             The hash of the var.
         """
-        return hash((self.__class__.__name__, self._js_expr))
+        return hash((type(self).__name__, self._js_expr))
 
     @classmethod
     def create(
@@ -1620,7 +1620,7 @@ class LiteralEventChainVar(ArgsFunctionOperationBuilder, LiteralVar, EventChainV
         Returns:
             The hash of the var.
         """
-        return hash((self.__class__.__name__, self._js_expr))
+        return hash((type(self).__name__, self._js_expr))
 
     @classmethod
     def create(

+ 2 - 2
reflex/state.py

@@ -438,7 +438,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
         Returns:
             The string representation of the state.
         """
-        return f"{self.__class__.__name__}({self.dict()})"
+        return f"{type(self).__name__}({self.dict()})"
 
     @classmethod
     def _get_computed_vars(cls) -> list[ComputedVar]:
@@ -3618,7 +3618,7 @@ class MutableProxy(wrapt.ObjectProxy):
         Returns:
             The representation of the wrapped object.
         """
-        return f"{self.__class__.__name__}({self.__wrapped__})"
+        return f"{type(self).__name__}({self.__wrapped__})"
 
     def _mark_dirty(
         self,

+ 3 - 3
reflex/vars/base.py

@@ -1569,7 +1569,7 @@ class CachedVarOperation:
         if name == "_js_expr":
             return self._cached_var_name
 
-        parent_classes = inspect.getmro(self.__class__)
+        parent_classes = inspect.getmro(type(self))
 
         next_class = parent_classes[parent_classes.index(CachedVarOperation) + 1]
 
@@ -1611,7 +1611,7 @@ class CachedVarOperation:
         """
         return hash(
             (
-                self.__class__.__name__,
+                type(self).__name__,
                 *[
                     getattr(self, field.name)
                     for field in dataclasses.fields(self)  # type: ignore
@@ -1733,7 +1733,7 @@ class CallableVar(Var):
         Returns:
             The hash of the object.
         """
-        return hash((self.__class__.__name__, self.original_var))
+        return hash((type(self).__name__, self.original_var))
 
 
 RETURN_TYPE = TypeVar("RETURN_TYPE")

+ 2 - 2
reflex/vars/number.py

@@ -1012,7 +1012,7 @@ class LiteralNumberVar(LiteralVar, NumberVar):
         Returns:
             int: The hash value of the object.
         """
-        return hash((self.__class__.__name__, self._var_value))
+        return hash((type(self).__name__, self._var_value))
 
     @classmethod
     def create(cls, value: float | int, _var_data: VarData | None = None):
@@ -1064,7 +1064,7 @@ class LiteralBooleanVar(LiteralVar, BooleanVar):
         Returns:
             int: The hash value of the object.
         """
-        return hash((self.__class__.__name__, self._var_value))
+        return hash((type(self).__name__, self._var_value))
 
     @classmethod
     def create(cls, value: bool, _var_data: VarData | None = None):

+ 1 - 1
reflex/vars/object.py

@@ -362,7 +362,7 @@ class LiteralObjectVar(CachedVarOperation, ObjectVar[OBJECT_TYPE], LiteralVar):
         Returns:
             The hash of the var.
         """
-        return hash((self.__class__.__name__, self._js_expr))
+        return hash((type(self).__name__, self._js_expr))
 
     @cached_property_no_lock
     def _cached_get_all_var_data(self) -> VarData | None:

+ 1 - 1
reflex/vars/sequence.py

@@ -667,7 +667,7 @@ class LiteralStringVar(LiteralVar, StringVar[str]):
         Returns:
             The hash of the var.
         """
-        return hash((self.__class__.__name__, self._var_value))
+        return hash((type(self).__name__, self._var_value))
 
     def json(self) -> str:
         """Get the JSON representation of the var.

+ 1 - 1
tests/integration/test_state_inheritance.py

@@ -73,7 +73,7 @@ def StateInheritance():
         def on_click_other_mixin(self):
             self.other_mixin_clicks += 1
             self.other_mixin = (
-                f"{self.__class__.__name__}.clicked.{self.other_mixin_clicks}"
+                f"{type(self).__name__}.clicked.{self.other_mixin_clicks}"
             )
 
     class Base1(Mixin, rx.State):

+ 2 - 2
tests/units/test_model.py

@@ -46,7 +46,7 @@ def test_default_primary_key(model_default_primary: Model):
     Args:
         model_default_primary: Fixture.
     """
-    assert "id" in model_default_primary.__class__.__fields__
+    assert "id" in type(model_default_primary).__fields__
 
 
 def test_custom_primary_key(model_custom_primary: Model):
@@ -55,7 +55,7 @@ def test_custom_primary_key(model_custom_primary: Model):
     Args:
         model_custom_primary: Fixture.
     """
-    assert "id" not in model_custom_primary.__class__.__fields__
+    assert "id" not in type(model_custom_primary).__fields__
 
 
 @pytest.mark.filterwarnings(

+ 1 - 1
tests/units/test_state.py

@@ -1698,7 +1698,7 @@ async def test_state_manager_modify_state(
         assert not state_manager._states_locks[token].locked()
 
         # separate instances should NOT share locks
-        sm2 = state_manager.__class__(state=TestState)
+        sm2 = type(state_manager)(state=TestState)
         assert sm2._state_manager_lock is state_manager._state_manager_lock
         assert not sm2._states_locks
         if state_manager._states_locks: