Repository: sciunto-org/python-bibtexparser
Pull request #443 (2024-01-18 19:49:48)

:recycle: Don't require middleware key on block middleware extensions


Patch:
diff --git a/bibtexparser/middlewares/enclosing.py b/bibtexparser/middlewares/enclosing.py
index f4a72b2..8aa0c32 100644
--- a/bibtexparser/middlewares/enclosing.py
+++ b/bibtexparser/middlewares/enclosing.py
@@ -37,8 +37,8 @@ class RemoveEnclosingMiddleware(BlockMiddleware):
         )
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return REMOVED_ENCLOSING_KEY
 
     @staticmethod
@@ -109,8 +109,8 @@ class AddEnclosingMiddleware(BlockMiddleware):
         self._enclose_integers = enclose_integers
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "remove_enclosing"
 
     def _enclose(
diff --git a/bibtexparser/middlewares/interpolate.py b/bibtexparser/middlewares/interpolate.py
index 62e9b70..d9e1076 100644
--- a/bibtexparser/middlewares/interpolate.py
+++ b/bibtexparser/middlewares/interpolate.py
@@ -28,8 +28,8 @@ class ResolveStringReferencesMiddleware(LibraryMiddleware):
         super().__init__(allow_inplace_modification)
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "ResolveStringReferences"
 
     # docstr-coverage: inherited
diff --git a/bibtexparser/middlewares/middleware.py b/bibtexparser/middlewares/middleware.py
index 2329eb9..1f0f2c5 100644
--- a/bibtexparser/middlewares/middleware.py
+++ b/bibtexparser/middlewares/middleware.py
@@ -62,14 +62,12 @@ class BlockMiddleware(Middleware, abc.ABC):
     except if `allow_inplace_modification` is true.
     """
 
-    @staticmethod
-    @abc.abstractmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         """Identifier of the middleware.
-
         This key is used to identify the middleware in a blocks metadata.
         """
-        pass
+        return cls.__name__
 
     # docstr-coverage: inherited
     def transform(self, library: "Library") -> "Library":
diff --git a/bibtexparser/middlewares/month.py b/bibtexparser/middlewares/month.py
index 16fba7b..0dbd621 100644
--- a/bibtexparser/middlewares/month.py
+++ b/bibtexparser/middlewares/month.py
@@ -80,8 +80,8 @@ class MonthLongStringMiddleware(_MonthInterpolator):
     and unenclosed."""
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "MonthLongStringMiddleware"
 
     # docstr-coverage: inherited
@@ -124,8 +124,8 @@ class MonthAbbreviationMiddleware(_MonthInterpolator):
     The created abbreviations are always lowercase and unenclosed."""
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "MonthAbbreviationMiddleware"
 
     # docstr-coverage: inherited
@@ -159,8 +159,8 @@ class MonthIntMiddleware(_MonthInterpolator):
     The created int-months are always integers and unenclosed."""
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "MonthIntMiddleware"
 
     # docstr-coverage: inherited
diff --git a/bibtexparser/middlewares/names.py b/bibtexparser/middlewares/names.py
index 9b6d308..02e74c2 100644
--- a/bibtexparser/middlewares/names.py
+++ b/bibtexparser/middlewares/names.py
@@ -62,8 +62,8 @@ class SeparateCoAuthors(_NameTransformerMiddleware):
     """Middleware to separate multi-person fields (e.g. co-authors, co-editors)."""
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "separate_coauthors"
 
     # docstr-coverage: inherited
@@ -75,8 +75,8 @@ class MergeCoAuthors(_NameTransformerMiddleware):
     """Middleware to merge multi-person-list fields (e.g. co-authors, co-editors)."""
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "merge_coauthors"
 
     # docstr-coverage: inherited
@@ -123,8 +123,8 @@ class SplitNameParts(_NameTransformerMiddleware):
     """
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "split_name_parts"
 
     def _transform_field_value(self, name) -> List[NameParts]:
@@ -145,8 +145,8 @@ class MergeNameParts(_NameTransformerMiddleware):
     """
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "merge_name_parts"
 
     def _transform_field_value(self, name) -> List[str]:
diff --git a/bibtexparser/middlewares/sorting_entry_fields.py b/bibtexparser/middlewares/sorting_entry_fields.py
index 3995ee5..600924a 100644
--- a/bibtexparser/middlewares/sorting_entry_fields.py
+++ b/bibtexparser/middlewares/sorting_entry_fields.py
@@ -22,8 +22,8 @@ class SortFieldsAlphabeticallyMiddleware(BlockMiddleware):
         return entry
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "sorted_fields_alphabetically"
 
 
@@ -71,6 +71,6 @@ class SortFieldsCustomMiddleware(BlockMiddleware):
         return entry
 
     # docstr-coverage: inherited
-    @staticmethod
-    def metadata_key() -> str:
+    @classmethod
+    def metadata_key(cls) -> str:
         return "sorted_fields_custom"


Test patch:
diff --git a/tests/middleware_tests/test_block_middleware.py b/tests/middleware_tests/test_block_middleware.py
index eef9cce..9cfa3ed 100644
--- a/tests/middleware_tests/test_block_middleware.py
+++ b/tests/middleware_tests/test_block_middleware.py
@@ -30,9 +30,6 @@ class ConstantBlockMiddleware(BlockMiddleware):
     def transform_block(self, block, library):
         return self._const
 
-    def metadata_key():
-        return "ConstantBlockMiddleware"
-
 
 class LambdaBlockMiddleware(BlockMiddleware):
     """A middleware that applies a lambda to the input block"""
@@ -44,9 +41,6 @@ class LambdaBlockMiddleware(BlockMiddleware):
     def transform_block(self, block, library):
         return self._f(block)
 
-    def metadata_key():
-        return "LambdaBlockMiddleware"
-
 
 @pytest.mark.parametrize(
     ("middleware", "expected"),